ptg10805159232 Process Control Chapter 8
parent process table entry...
fd 0:
fd 1:
fd 2:flagsfd pointerfilechild process table entry...
fd 0:
fd 1:
fd 2:flagsfd pointerfilefile status flags
current file offset
v-node pointerfile status flags
current file offset
v-node pointerfile status flags
current file offset
v-node pointerfile table
v-node information
v_datai-node information
current file size
i_vnodev-node information
v_datai-node information
current file size
i_vnodev-node information
v_datai-node information
current file size
i_vnodev-node tableFigure 8.2 Sharing of open files between parent and child afterforkIf 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.