Unix kernel represents open files using three data structures: Descriptor table
, File table
, and v-node table
.
When a process opens a file twice, it gets two different descriptors in the descriptor table
, two entries in the file table
(so that they have different positions in the same file), and they both point to one entry in the v-node table
.
And child process inherits parent process's descriptor table
, so kernel maintains one descriptor table
for each process respectively. But two descriptor from different processes point to the same entry in open file table
.
So
fork
on same file? That means two processes share a position(offset) information on the same file.One process can have multiple file descriptors point to the same entry (e.g., as a result of a call to dup() ) Multiple processes (e.g., a parent and child) can have file descriptors that point to the same entry.
A case you must be familiar with are file descriptors 0, 1 and 2 which for each process are the standard input, standard output and standard error, pointing wherever these were redirected to. A process can open the same file more than once.
For the most part, if two mv processes attempt to move the same file at the same time, they'll both copy the data: the instance first to start will create a file, the second instance will delete that file and create a new one. However, if you're unlucky, it is possible to lose data.
At least one file descriptor exists for every open file on the system.
When child process does some read on the file, would the offset of the same file change in parent process?
Yes, since the offset is stored system-wide file table. You could get a similar effect using dup
or dup2
.
If 1 is true, for two processes, is there a convenient way that I can get the same effect of
fork
on same file? That means two processes share a position(offset) information on the same file.
There is a technique called "passing the file descriptor" using Unix domain sockets. Look for "ancillary" data in sendmsg
.
Is there a way to fork so that both processes have totally unrelated tables, like two unrelated processes only that they opened same files.
You have to open
the file again to achieve this. Although it doesn't do what you want, you should also look for the FD_CLOEXEC
flag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With