Signals: Advanced Features 461
If the first argument is supplied, the main program blocks all signals, and then
sleeps for the number of seconds specified by this argument. During this time, we
can queue multiple realtime signals to the process and observe what happens when
the signals are unblocked. The second argument specifies the number of seconds
that the signal handler should sleep before returning. Specifying a nonzero value
(the default is 1 second) is useful for slowing down the program so that we can
more easily see what is happening when multiple signals are handled.
We can use the program in Listing 22-3, along with the program in Listing 22-2
(t_sigqueue.c) to explore the behavior of realtime signals, as shown in the following
shell session log:
$ ./catch_rtsigs 60 &
[1] 12842
$ ./catch_rtsigs: PID is 12842 Shell prompt mixed with program output
./catch_rtsigs: signals blocked - sleeping 60 seconds
Press Enter to see next shell prompt
$ ./t_sigqueue 12842 54 100 3 Send signal three times
./t_sigqueue: PID is 12843, UID is 1000
$ ./t_sigqueue 12842 43 200
./t_sigqueue: PID is 12844, UID is 1000
$ ./t_sigqueue 12842 40 300
./t_sigqueue: PID is 12845, UID is 1000
Eventually, the catch_rtsigs program completes sleeping, and displays messages as
the signal handler catches various signals. (We see a shell prompt mixed with the
next line of the program’s output because the catch_rtsigs program is writing output
from the background.) We first observe that realtime signals are delivered lowest-
numbered signal first, and that the siginfo_t structure passed to the handler
includes the process ID and user ID of the process that sent the signal:
$ ./catch_rtsigs: sleep complete
caught signal 40
si_signo=40, si_code=-1 (SI_QUEUE), si_value=300
si_pid=12845, si_uid=1000
caught signal 43
si_signo=43, si_code=-1 (SI_QUEUE), si_value=200
si_pid=12844, si_uid=1000
The remaining output is produced by the three instances of the same realtime signal.
Looking at the si_value values, we can see that these signals were delivered in the
order they were sent:
caught signal 54
si_signo=54, si_code=-1 (SI_QUEUE), si_value=100
si_pid=12843, si_uid=1000
caught signal 54
si_signo=54, si_code=-1 (SI_QUEUE), si_value=101
si_pid=12843, si_uid=1000
caught signal 54
si_signo=54, si_code=-1 (SI_QUEUE), si_value=102
si_pid=12843, si_uid=1000