This page describes the guaranteed behavior of the functions readdir
and readdir_r
and contains the following sentence:
After a call to fork(), either the parent or child (but not both) may continue processing the directory stream using readdir(), rewinddir() or seekdir(). If both the parent and child processes use these functions, the result is undefined.
I'm writing a fork-safe wrapper around the DIR*
-related family of functions as an exercise and I'm wondering whether it's safe to closedir
the directory stream in both the parent and the child simultaneously or whether the process that doesn't continue reading from the directory stream isn't supposed to interact with the directory stream at all.
fork
handler and force the user to pick whenopendir
ing the directory whether the parent or child inherits it on fork. My wrapper library that doesn't exist yet will promise nothing if you use the unwrapped library calls in addition to the wrapper.ptrace
here, the fds are shared becauseopendir
was called beforefork
. The relevant scenario isfork
withoutexec
, so the execution context is identical in the parent and child.