520 Chapter 24
Figure 24-2: Duplication of file descriptors during fork(), and closing of unused descriptors
24.2.2 Memory Semantics of fork()...................................................................
Conceptually, we can consider fork() as creating copies of the parent’s text, data,
heap, and stack segments. (Indeed, in some early UNIX implementations, such
duplication was literally performed: a new process image was created by copying the
parent’s memory to swap space, and making that swapped-out image the child pro-
cess while the parent kept its own memory.) However, actually performing a simple
copy of the parent’s virtual memory pages into the new child process would be
wasteful for a number of reasons—one being that a fork() is often followed by an
immediate exec(), which replaces the process’s text with a new program and reinitializes
descriptor y
descriptor x
Parent file descriptors
(close-on-exec flag)
Open file table
(file offset, status flags)
Parent file descriptors Open file table
Child file descriptors
Child file descriptors
Parent file descriptors Open file table
a) Descriptors and open
file table entries
before fork()
b) Descriptors after
fork()
c) After closing unused
descriptors in parent
(y) and child (x)
Descriptors
duplicated
in child
descriptor y
descriptor x
descriptor y
descriptor x
descriptor y
descriptor x
descriptory
descriptor x
OFT entry m
OFT entry n
OFT entry m
OFT entry n
OFT entry m
OFT entry n