Independence of file descriptor flags across forks (or lack thereof)

Ronald F. Guilmette rfg at tristatelogic.com
Thu Jan 9 05:30:09 UTC 2020


In message <20200108161544.76f64dd6eb0d1edb134a5b0f at sohara.org>, 
Steve O'Hara-Smith <steve at sohara.org> wrote:

>	OK I can explain this:
>
>	fd1 = open ("/some/file", O_RDONLY);
>	fd2 = open ("/some/file", O_RDWR);
>
>	Now I have two file descriptors referring to the same file with
>different file access modes. I can set flags on fd1 and fd2 independently.
>That is what the precisely phrased paragraph above is referring to. Each
>file descriptor (with values like 3) refers to a table which identifies the
>kernel data structure representing the open file. It is that kernel data
>structure that holds such things as the file position, mode, flags etc.
>
>	Now if I fork this process then the table of open file descriptors
>gets copied so now two processes are sharing the same kernel data
>structures.

All I can say is that the kernel implementors of both FreeBSD and also
Linux seem to agree with your viewpoint.  I myself am not persuaded
that the two file descriptors that exist after a fork should behave any
differently from the two in your example above, but they do, so I guess
I just have to deal with it.


Regards,
rfg


More information about the freebsd-questions mailing list