Development Diary -20,190,827 keyword study notes "Unix-level programming environment (Second Edition)" DAY 3

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_31433709/article/details/100086439

Referred Blogs

File descriptor flags, file status flags
to understand the Linux file descriptor fd and file pointer flip of
Linux programming - file descriptor fd


File Sharing

Unix support to share files between different processes.
The kernel uses three data structures, their relationship determines the impact of file sharing in a process to another process that may arise.
(1) Each process has a record entry in the process table, each record file descriptor table entry has a punch, which can be considered as a vector, each one per descriptor. Associated with each file descriptor is associated with:
(A) the file descriptor flags.
(B) a pointer to a file entry.

(2) the kernel to maintain all open files with a file table. Each file entry includes:
(A) the file status flags (read, write, by writing, synchronization, non-blocking, etc.).
(B) the amount of displacement of the current file.
(C) a pointer to the file entry node v.

(3) each open file (or device) has a v-node structure. v-node contains the file type of this file and the various operations of the function of the pointer information. For most belonging to the file, v i node further comprises a node of the file (inode). This information is read into memory from the disk when the file is opened, all the information about a file is available for quick use. Eg, i node contains the owner of the file, the file length, the actual data block pointer device file exists, points to the file on the disk or the like is used.

  • After completion of each write, the amount of displacement current file in the file table entry, i.e., increase in the number of bytes written. If this causes the amount of displacement of the current file exceeds the length of the current file, the current file size in the i-node table entry It is set to the amount of displacement of the current file (the file that is lengthened)

  • If a file open O_APPEND flag, the corresponding flag is set in the file status flag in the file table entry every time such data having file execution flag Tianxie write operation, the current file in the file table entry first displacement amount is set to the file size in the i-node table entry. this makes each time data is written to the current file at the trailing end.

  • lseek function only modify the amount of displacement of the current file in the file table entry, without any IO operation

  • If a file is located lseek trailing end to the current file, the current displacement file table entry is set to the current length of the file table entry of node i

There may be multiple entry points to a file descriptor with a file entry. The same situation also occurred after the fork, when the parent, the child process for each open file descriptors share the same file entries.

Note that the file descriptor flags and status flags file scope in terms of the difference, the former is used only for a descriptor of a process, while the latter applies to all the given points in any process descriptor table entry in the file.

For all the above-mentioned multiple processes read the consent document will work correctly. Each process has its own file table entry, which also has its own displacement of the current file. However, when multiple processes to write the same file, you may produce unanticipated results. in order to illustrate how to avoid this, it is necessary to understand the concept of the atomic operation.

Consider a process, which you want to add data to the end of a file does not support earlier versions of UNIX O_APPEND open the option, so the program is written into the following form:

if(lseek(fd,0L,2)<0)err_sys("lseek error");
if(write(fd,buff,100)!=100)err_sys("write error");

For a single process, this program can work, but if multiple processes, problems can occur.

Assuming two separate processes A and B, both add operation on the same file. Each process is to open the file, but the O_APPEND flag. In this case the relationship between the data structure shown in Figure 3-2 as shown in each process has its own file entries, but share a v-node entries. a process is assumed calls lseek, it will set 1500 bytes for the current displacement of the file of the process a (current file trailing end). then the process that the kernel running process B handover. process B performed lseek, which will be the amount of displacement current is set to 1500 bytes of the file (the current end of file). B then calls Write, it B the amount of displacement of the current file of the file increased since 1600. the length of the file has increased, so the kernel of the current file length v node is updated to 1600 ,. kernel and then carry out the process of switching the process a reply operation. when a when you call write, it is at its current displacement (1500) writes data file. in this way there is a substitution process B just written to the data in the file.

The problem here lies in the logic operation "to locate the file at the tail end, then write" uses two separate function calls. Solution to the problem is to make these two operations carried out for other purposes to be an atomic operation. Any requirement more than one operating function call will not be made atomic operations, because between the two function calls, the kernel is likely to temporarily suspend the process.

UNIX provides a way to make this operation to be atomic operations, which is to open in the file is set O_APPEND flag. As mentioned in the previous section, this is the core of each file before writing this, the current process will to set the amount of displacement at the end of the file, so each time before writing no longer need to call lseek.


fcntl function

fcntl function can change the nature of the file has been opened

 int fcntl(int filedes, int cmd, ...);

In the examples in this section, the third parameter is always an integer, the function shown in the comment section above prototype corresponds However record locking Description Section 12.3, the third argument is a pointer to a structure the same pointer.


These are the "Unix-level programming environment" related records on file access and control of specific content and, in fact, to see when the total feeling of missing a little something, they still tend to search online content search some point to add blog content to these little withered content charging inflated, let it become plump.
Well, the game's second act release it.


I still feel in the form of questions to clarify ideas would be better:
Q1: What is the file descriptor, file descriptor flags, file status flags?
Contents written in the book only gives these names, not even to the concept itself of these names, I feel these are not enough, or should a clearer understanding of these concepts will be better.
specialized piece of content posted it, or too chaotic.
What is the file descriptor, file descriptor flags, file status flags

Guess you like

Origin blog.csdn.net/qq_31433709/article/details/100086439