Unix system file tables

I am confused about Unix system file tables.

  • When two or more processes open a file for reading, does the system file table create separate entries for each process or one entry?

  • If a single entry is created for several processes that open the same file, will their file offsets be the same?

  • If process 1 opens file1.txt for reading and process 2 opens the same file1.txt for writing, will the system file table create one or two records?

+11
source share
2 answers

There are three “system file tables”: There is a file descriptor table that maps file descriptors (small integers) to entries in the open file table. Each entry in the open file table contains (among other things) the file offset and a pointer to the inode table in memory. Here is the picture:
(source: rich on www.cs.ucsb.edu )

Thus, there is not a single entry in the file table for an open file, and there is not only one entry per process ... there is one entry for calling open() , and it is shared if the file descriptor is dup() ed or fork() ed,

Answering your questions:

1) When two or more processes open a file for reading, an entry for each opening appears in the open file table. There is even an entry for each opening if one process opens the file several times.

2) One record is not created in the table of open files for different processes that open the same file (but in the table inode there is only one record in memory).

3) If the file1.txt file is opened twice, in the same or two different processes, there are two different entries in the open files table (but only one entry in the inode table in memory).

+27
source

The same file can be opened simultaneously by several processes and even the same process (which leads to several file descriptors for the same file) depending on the file organization and file system. Operations on descriptors, such as moving the file pointer or closing, are independent (they do not affect other descriptors for the same file). File operations (for example, writing) can be seen from operations with other descriptors (subsequent reading can read the written data).

This is from the open (system call) wiki page

0
source

All Articles