Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

234 Process Control Chapter 8


Some operating systems combine the operations from step 2—aforkfollowed by an
exec—into a single operation called aspawn.The UNIX System separates the two, as
thereare numerous cases where it is useful toforkwithout doing anexec.Also,
separating the two operations allows the child to change the per-process attributes
between theforkand theexec,such as I/O redirection, user ID, signal disposition,
and so on.We’ll see numerous examples of this in Chapter 15.

The Single UNIX Specification does includespawninterfaces in the advanced real-time option
group. These interfaces arenot intended to be replacements forforkandexec,however.
They areintended to support systems that have difficulty implementingforkefficiently,
especially systems without hardwaresupport for memory management.

8.4 vforkFunction


The functionvforkhas the same calling sequence and same return values asfork,but
the semantics of the two functions differ.

Thevforkfunction originated with 2.9BSD. Some consider the function a blemish, but all the
platforms covered in this book support it. In fact, the BSD developers removed it from the
4.4BSD release, but all the open source BSD distributions that derive from 4.4BSD added
support for it back into their own releases. Thevforkfunction was marked as an obsolescent
interface in Version 3 of the Single UNIX Specification and was removed entirely in Version 4.
We include it herefor historical reasons only.Portable applications should not use it.

Thevforkfunction was intended to create a new process for the purpose of
executing a new program (step 2 at the end of the previous section), similar to the
method used by the bare-bones shell from Figure1.7. Thevforkfunction creates the
new process, just likefork,without copying the address space of the parent into the
child, as the child won’t reference that address space; the child simply callsexec(or
exit)right after thevfork.Instead, the child runs in the address space of the parent
until it calls eitherexec or exit.This optimization is moreefficient on some
implementations of the UNIX System, but leads to undefined results if the child
modifies any data (except the variable used to hold the return value fromvfork),
makes function calls, or returns without callingexecorexit.(As we mentioned in the
previous section, implementations use copy-on-write to improve the efficiency of a
forkfollowed by anexec,but no copying is still faster than some copying.)
Another difference between the two functions is thatvforkguarantees that the
child runs first, until the child callsexecorexit.When the child calls either of these
functions, the parent resumes. (This can lead to deadlock if the child depends on
further actions of the parent beforecalling either of these two functions.)

Example


The program in Figure8.3 is a modified version of the program from Figure8.1. We’ve
replaced the call toforkwithvforkand removed thewriteto standardoutput.
Also, we don’t need to have the parent callsleep, as we’reguaranteed that it is put to
sleep by the kernel until the child calls eitherexecorexit.
Free download pdf