I am following the APUE textbook on siglongjump(), and there is this piece of code which is confusing me.
pr_mask()is simply a function which prints the signals which are masked.- According to this, if
USR1signal comes, it gets added to the process mask and this is the handler.static void sig_usr1(int signo) { time_t starttime;if (canjump == 0) return; /* unexpected signal, ignore */ pr_mask("starting sig_usr1: "); alarm(3); /* SIGALRM in 3 seconds */ starttime = time(NULL); for ( ; ; ) /* busy wait for 5 seconds */ if (time(NULL) > starttime + 5) break; pr_mask("finishing sig_usr1: "); canjump = 0; siglongjmp(jmpbuf, 1); /* jump back to main, don't return */}
- Now while
USR1is processing,SIG_ALARMcomes, it will call the handler
and thisstatic void sig_alrm(int signo) { pr_mask("in sig_alrm: "); }SIG_ALARMwill also get added to the proc mask. However, whensig_alarm()is done, thenSIG_ALARMis going to be removed from the proc mask. This is illustrated when the call topr_mask("finishing sig_usr1: ");is made insig_usr1().
How is SIG_ALARM not masked when pr_mask("finishing sig_usr1") is called? siglongjump() should only restore the old mask when siglongjump() is called, which happens after this call.