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

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


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.