1

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 USR1 signal 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 USR1 is processing, SIG_ALARM comes, it will call the handler
    static void
    sig_alrm(int signo)
    {
        pr_mask("in sig_alrm: ");
    }
    
    and this SIG_ALARM will also get added to the proc mask. However, when sig_alarm() is done, then SIG_ALARM is going to be removed from the proc mask. This is illustrated when the call to pr_mask("finishing sig_usr1: "); is made in sig_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.

AdminBee
  • 22,803

0 Answers0