ptg10805159
922 Solutions to Selected Exercises Appendix C
tovforkand does a return fromf1.The return information is often stored in the
stack frame, and that information has probably been modified by the child. After
the parent resumes, what happens with this example depends on many
implementation features of your UNIX system (whereinthe stack frame the
return information is stored, what information in the stack frame is wiped out
when the automatic variables aremodified, and so on). The normal result is a
corefile, but your results may differ.
stack frame
formain
stack frame
forf1
bottom of stack
direction of
stack growth
Figure C.9 Stack frames whenvforkis called
8.4 In Figure8.13, we have the parent write its output first. When the parent is done,
the child writes its output, but we let the parent terminate. Whether the parent
terminates or whether the child finishes its output first depends on the kernel’s
scheduling of the two processes (another race condition). When the parent
terminates, the shell starts up the next program, and this next program can
interferewith the output from the previous child.
We can prevent this from happening by not letting the parent terminate until the
child has also finished its output. Replace the code following theforkwith the
following:
else if (pid == 0) {
WAIT_PARENT(); /* parent goes first */
charatatime("output from child\n");
TELL_PARENT(getppid()); /* tell parent we’re done */
}else {
charatatime("output from parent\n");
TELL_CHILD(pid); /* tell child we’re done */
WAIT_CHILD(); /* wait for child to finish */
}
We won’t see this happen if we let the child go first, since the shell doesn’t start
the next program until the parent terminates.
8.5 The same value (/home/sar/bin/testinterp)isprinted forargv[2].The
reason is thatexeclpends up callingexecvewith the samepathnameas when
we callexecldirectly.Recall Figure8.15.