Two file descriptors from different points in the process to the same entry in the open file table

The 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 receives two different descriptors in the Descriptor table , two entries in the File table (so that they have different positions in the same file), and both of them point to the same entry in the v-node table .
And the child process inherits the parent Descriptor table process, so the kernel supports one Descriptor table for each process, respectively. But two descriptors from different processes point to the same entry in the open file table .
so

  • When a child process reads in a file, will there be an offset of the same file in the parent process?
  • If 1 is true, is there a convenient way for two processes to get the same effect from fork in the same file? This means that two processes use location (offset) information in the same file.
  • Is there a way to unlock so that both processes have completely unrelated tables, for example two unrelated processes, only they open the same files.
+6
source share
1 answer

When a child process reads in a file, will there be an offset of the same file in the parent process?

Yes, since the offset is stored in the system file table. You can get a similar effect using dup or dup2 .

If 1 is true, is there a convenient way for two processes to get the same effect from fork in the same file? This means that two processes use location (offset) information in the same file.

There is a method called “file descriptor transfer” using Unix domain sockets. Find the "helper" data in sendmsg .

Is there a way to unlock so that both processes have completely unrelated tables, for example two unrelated processes, they just opened the same files.

To do this, you need to open save the file. Although it does not do what you want, you should also look for the FD_CLOEXEC flag.

+8
source

Source: https://habr.com/ru/post/919652/


All Articles