anon
anon

Reputation:

Are file descriptors shared when fork()ing?

Let's say I open a file with open(). Then I fork() my program.

Will father and child now share the same offset for the file descriptor?

I mean if I do a write in my father, the offset will be changed in child too?

Or will the offsets be independent after the fork()?

Upvotes: 27

Views: 14041

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799110

From fork(2):

  *  The child inherits copies of the parent’s set of open file  descrip-
     tors.   Each  file  descriptor  in the child refers to the same open
     file description (see open(2)) as the corresponding file  descriptor
     in  the parent.  This means that the two descriptors share open file
     status flags, current file offset, and signal-driven I/O  attributes
     (see the description of F_SETOWN and F_SETSIG in fcntl(2)).

Upvotes: 33

Martin v. Löwis
Martin v. Löwis

Reputation: 127527

They do share the same offset.

Upvotes: 6

Related Questions