ptg10805159
232 Process Control Chapter 8
parent process table entry
...
fd 0:
fd 1:
fd 2:
flagsfd pointerfile
child process table entry
...
fd 0:
fd 1:
fd 2:
flagsfd pointerfile
file status flags
current file offset
v-node pointer
file status flags
current file offset
v-node pointer
file status flags
current file offset
v-node pointer
file table
v-node information
v_data
i-node information
current file size
i_vnode
v-node information
v_data
i-node information
current file size
i_vnode
v-node information
v_data
i-node information
current file size
i_vnode
v-node table
Figure 8.2 Sharing of open files between parent and child afterfork
If both parent and child write to the same descriptor,without any form of
synchronization, such as having the parentwaitfor the child, their output will be
intermixed (assuming it’s a descriptor that was open beforethefork). Although this is
possible — we saw it in Figure8.2 — it’s not the normal mode of operation.
Thereare two normal cases for handling the descriptors after afork.
- The parent waits for the child to complete. In this case, the parent does not need
to do anything with its descriptors. When the child terminates, any of the
shared descriptors that the child read from or wrote to will have their file offsets
updated accordingly. - Both the parent and the child go their own ways. Here, after thefork,the
parent closes the descriptors that it doesn’t need, and the child does the same
thing. This way,neither interferes with the other’s open descriptors. This
scenario is often found with network servers.