Linux file sharing implementations

Beijing two days ago to go with the teacher opened a meeting, not a long time to learn, just go back to school today, the bitterness will not say. To text:

1. What is file sharing

(1), that is, file sharing the same file (a file with an inode refers to the same, the same pathname) is a plurality of separate read and write body (almost be understood that a plurality of file descriptors) to simultaneously (an open yet while closed, to another operation).

(2) There are many file sharing meaning: For example, we could be achieved through file-sharing operations with multiple threads at the same time a large file to reduce the file read and write time, improve efficiency.

2, three kinds of file-sharing implementation

(1), file sharing is the core of how it comes out multiple file descriptors point to the same file.

(2), a common situation three file sharing:

             The first is the same process used multiple times open to open the same file, the return value is not the same as when using multiple open; (respectively read / write)

             The second is to use in different processes are open to open the same file (fd at this time because the two different processes, so two digital fd may be the same or different); (respectively read / write)

             The third case is a linux system provides two dup and dup2 API to make the process of copying a file descriptor. (Continuation Read / Write)

3, and then on the file descriptor

(1) the nature of the file descriptor is a number that is an entry in the process table file descriptor table on the digital nature, the process of going through the file descriptor index as the index look-up table to get the file pointer, and then indirectly get access this file corresponds to the file table.

(2), this number is to call the internal file descriptor open automatically assigned by the operating system, the operating system assigns this time fd, is not randomly distributed, but also comply with certain rules, and now we are going to study this law.

(3), operating system requirements, fd increases from 0 in order. fd also has a maximum limit, fd in earlier versions of linux (0.11) maximum of 20, so when a process allows up to 20 files. linux in the file descriptor table is an array (not a chain), so this file descriptor table is actually an array, fd is the index, the file pointer table is value.

(4), when we went to open, the kernel will choose a smallest number from unused file descriptor table for us to return. This means that if fd is already filled before the 0-9, then open the next time we get must be 10. (However, if a fd get is 9, not necessarily the next 10, it is probably because in front of a smaller fd has been released close out).

(5), fd is already in default 0,1,2 occupied by the system, so minimal user fd 3 is a process to get.

(6), linux kernel occupies 0,1,2 three fd is useful when we run a program to get a process internal to these three files by default, these three files corresponding fd is 0 1,2. These three files are called stdin, stdout, stderr. That is the standard input, standard output, standard error.

(7), generally corresponding to the standard keyboard input (which can be understood as: fd 0 corresponding to this keyboard device file)

             Standard output is typically an LCD display (can be understood as: LCD device file corresponding to 1)

(8), printf function is actually the default output to the standard output stdout. stdio There is also a function called fprintf, this function can specify the output to which a file descriptor.

Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11166157.html