I am not sure about these three items when reading a book. Well, I will write a blog to summarize them.
File descriptor (process level ):
1) there is a record item in each progress table. The record item contains an open file descriptor table, which can be inspected as a vector (array) and each descriptor occupies one item. Associated with each file descriptor is:
(A), file descriptor flag.
(B) pointer to a file table item. (Different file descriptors can point to the same file table item. For example, you can use the dup function to copy the file descriptor to get a new file descriptor, But they point to the same file table item, therefore, they also point to the same V node table .)
File Table items (global, all processes share? Own YY)
2) The kernel maintains a file table for all opened files (the same file can be opened multiple times, so the same file can occupy multiple files in the file table ). Each file table item includes:
(A), file status signs (read, write, add write, synchronization and non-blocking)
(B) Current file offset (the V nodes of different file table items point to the same V node table, that is, to the same real physical file. Because a file table item contains the current file offset, the offset of the same file can be different for different file table items, this arrangement allows each process to have its own current offset to the file .)
(C) pointer to the V node table entry of the file.
V node table item (global level? All processes are shared? Own YY)
(3) Each open file (or device) has a V node structure. The V node contains the file type and pointer to the functions that perform various operations on the file. For most files, the V Node also contains the I node of the file.
------------------------------------------------ Split ---------------------------------------------------------------------
Then, let's briefly summarize their corresponding relationships:
A file descriptor corresponds to a file table item, and a file table item corresponds to a V node table item. Different file descriptors can point to the same file table item (dup can be used to copy the file descriptor, and so on) and point to the same V node table item. Similar to this, different file table items can also point to the same V node table item. For example, fd1 = open (pathname ,...), fd2 = open (pathname ....), because they open their own files, although they are the same file, they still have different file descriptors, pointing to different file table items (as mentioned earlier, the kernel maintains a file table for all opened files (the same file can be opened multiple times, so the same file can occupy multiple files in the file table )). However, because they are the same file, the V node pointer in their file table items points to the same V node table item.
In a simple summary, the correspondence between a file descriptor and a file table is one-to-one or multiple-to-one. The relationship between file table items and V node table items is:
One-to-one or multiple-to-one.
PS: The above part is copied to "Advanced Programming in Unix environment", and some belong to their own YY. The YY part does not verify its correctness at all. If something goes wrong, ask the elders and friends to point out ~~~ Thank you.