[cs631apue] question about signal
Jan Schaumann
jschauma at stevens.edu
Mon Oct 28 16:12:37 EDT 2013
hao wu <hwu9 at stevens.edu> wrote:
> Hit ^\ , then Hit ^C immediately, will got:
>
> => Establishing initial signal hander via signal(3).
> ^\sig_quit: caught SIGQUIT (1), now sleeping
> ^Csig_int: caught SIGINT (2), returning immediately
> sig_quit: exiting (2)
>
> => Time for a second interruption.
>
> My question is why hit ^C, the sig_quit will exit immediately, rather than
> sleep 3 seconds?
When you hit ^\, you are immediately transferred into the sig_quit
handler. This function prints a statement and then sleeps. When you
then hit ^C, the SIGINT signal is immediately delivered to your program,
interrupting the sleep and transferring execution into the sig_int
handler. sig_int only prints something and you then are returned into
the function you came from.
At that point, your sleep(3) was interrupted, so it will return
immediately. As a return value, it will yield the number of seconds
remaining.
You can observe this by changing
sleep(SLEEP);
to
fprintf(stderr, "%d seconds of sleep left\n", sleep(SLEEP));
Hope this clears things up.
-Jan
More information about the cs631apue
mailing list