From my understanding, the second (child) process inherits its parent's file descriptor f. Therefore, both f in parent and child point to the same open-file structure (according to UNIX book). I thought that kernel reclaims an open-file structure only when it's not pointed anymore by descriptors? The way it works: open(2) opens a file and returns a file descriptor. The use count on that file is incremented. fork(2) replicates the file descriptors such that the parent and child now have their own, and the use count is incremented. At some point, the unlink(2) system call is called. This removes the file name from the specified directory and decrements the use count. Assuming
On Wednesday 29 March 2006 2:08 am, Verdi March wrote:
the file only had a single hard link, the use count is now 2.
Parent closes file. Use count is decremented.
The child can still write.
The child closes file, and the use count becomes zero, and the OS physically
deletes the file.
Note that the system calls are atomic.
--
Jerry Feldman