Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two file descriptor from different process point to the same entry in open file table

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

  1. When child process does some read on the file, would the offset of the same file change in parent process?
  2. 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.
  3. Is there a way to fork so that both processes have totally unrelated tables, like two unrelated processes only that they opened same files.
like image 781
WiSaGaN Avatar asked Jul 04 '12 08:07

WiSaGaN


People also ask

Can two file descriptors point to 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.

Can you have the same file descriptor ID for the same file open by two processes?

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.

Can two processes have the same file open?

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.

How many entries file descriptor in the per process table of open files?

At least one file descriptor exists for every open file on the system.


1 Answers

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.

like image 177
cnicutar Avatar answered Oct 21 '22 20:10

cnicutar