Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Chapter 10 Exercises 381


10.23 Summary


Signals areused in most nontrivial applications. An understanding of the hows and
whys of signal handling is essential to advanced UNIX System programming. This
chapter has taken a long and thorough look at UNIX System signals. We started by
looking at the warts in previous implementations of signals and how they manifest
themselves. Wethen proceeded to the POSIX.1 reliable-signal concept and all the
related functions. Once we covered all these details, we wereable to provide
implementations of the POSIX.1abort,system,andsleepfunctions. Wefinished
with a look at the job-control signals and the ways that we can convert between signal
names and signal numbers.

Exercises


10.1 In Figure10.2, remove thefor (;;)statement. What happens and why?
10.2 Implement thesig2strfunction described in Section 10.22.
10.3 Draw pictures of the stack frames when we run the program from Figure10.9.
10.4 In Figure10.11, we showed a technique that’s often used to set a timeout on an I/O
operation usingsetjmpandlongjmp.The following code has also been seen:
signal(SIGALRM, sig_alrm);
alarm(60);
if (setjmp(env_alrm) != 0) {
/* handle timeout */
...
}
...
What else is wrong with this sequence of code?
10.5 Using only a single timer (eitheralarmor the higher-precisionsetitimer), provide a set
of functions that allows a process to set any number of timers.
10.6 Write the following program to test the parent–child synchronization functions in
Figure10.24. The process creates a file and writes the integer 0 to the file. The process then
callsfork,and the parent and child alternate incrementing the counter in the file. Each
time the counter is incremented, print which process (parent or child) is doing the
increment.
10.7 In the function shown in Figure10.25, if the caller catchesSIGABRTand returns from the
signal handler,why do we go to the trouble of resetting the disposition to its default and
callkillthe second time, instead of simply calling_exit?
10.8 Why do you think thesiginfostructure(Section 10.14) includes the real user ID, instead
of the effective user ID, in thesi_uidfield?
10.9 Rewrite the function in Figure10.14 to handle all the signals from Figure10.1. The
function should consist of a single loop that iterates once for every signal in the current
signal mask (not once for every possible signal).
Free download pdf