Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

358 Signals Chapter 10


main
signal()
signal()
pr_mask()
sigsetjmp()
pause()
SIGUSR1delivered sig_usr1

pr_mask()
alarm()
time()
time()
time()

SIGALRMdelivered sig_alrm

pr_mask()
return from signal handler return()

pr_mask()
sigsetjmp() siglongjmp()
pr_mask()
exit()

Figure 10.21Timeline for example program handling two signals

Let’s examine the output when the program in Figure10.20 is executed:
$./a.out & start process in background
starting main:
[1] 531 the job-control shell prints its process ID
$kill -USR1 531 send the processSIGUSR1
starting sig_usr1: SIGUSR1
$ in sig_alrm: SIGUSR1 SIGALRM
finishing sig_usr1: SIGUSR1
ending main:
just press RETURN
[1] + Done ./a.out &
The output is what we expect: when a signal handler is invoked, the signal being caught
is added to the current signal mask of the process. The original mask is restored when
the signal handler returns. Also,siglongjmprestores the signal mask that was saved
bysigsetjmp.
If we change the program in Figure10.20 so that the calls tosigsetjmpand
siglongjmparereplaced with calls tosetjmpandlongjmpon Linux (or_setjmp
and_longjmpon FreeBSD), the final line of output becomes
ending main: SIGUSR1
This means that themainfunction is executing with theSIGUSR1signal blocked, after
the call tosetjmp.This probably isn’t what we want.
Free download pdf