How to use sigsuspend

前端 未结 2 1137
后悔当初
后悔当初 2021-02-04 16:47

I have this code, and i have to make the program block repeatedly waiting for a signal. My teacher wants us to use the sigsuspend and masks instead of pause or sleep. I\'m not f

相关标签:
2条回答
  • 2021-02-04 16:56

    The simple way to get started is to just call sigsuspend with an empty mask (i.e., any signal can wake up the program):

    sigset_t myset;
    (void) sigemptyset(&myset);
    while (1) {
        (void) printf("I'm running, waiting for a signal...\n");
        (void) sigsuspend(&myset);
    }
    

    The next step would be to use sigprocmask to disable your two handled signals (SIGINT and SIGQUIT) from occurring other than when you're waiting from them in sigsuspend. Then you would use the “old mask” obtained from sigprocmask instead of the empty mask when suspending.

    0 讨论(0)
  • 2021-02-04 17:06

    The GNU libc manual description seems clear and thorough:

    Function: int sigsuspend (const sigset_t *set)

    This function replaces the process's signal mask with set and then suspends the process until a signal is delivered whose action is either to terminate the process or invoke a signal handling function. In other words, the program is effectively suspended until one of the signals that is not a member of set arrives.

    If the process is woken up by delivery of a signal that invokes a handler function, and the handler function returns, then sigsuspend also returns.

    The mask remains set only as long as sigsuspend is waiting. The function sigsuspend always restores the previous signal mask when it returns.

    The return value and error conditions are the same as for pause.

    With sigsuspend, you can replace the pause or sleep loop in the previous section with something completely reliable:

     sigset_t mask, oldmask;
    
     ...
    
     /* Set up the mask of signals to temporarily block. */
     sigemptyset (&mask);
     sigaddset (&mask, SIGUSR1);
    
     ...
    
     /* Wait for a signal to arrive. */
     sigprocmask (SIG_BLOCK, &mask, &oldmask);
     while (!usr_interrupt)
       sigsuspend (&oldmask);
     sigprocmask (SIG_UNBLOCK, &mask, NULL);
    

    This last piece of code is a little tricky. The key point to remember here is that when sigsuspend returns, it resets the process's signal mask to the original value, the value from before the call to sigsuspend—in this case, the SIGUSR1 signal is once again blocked. The second call to sigprocmask is necessary to explicitly unblock this signal.

    One other point: you may be wondering why the while loop is necessary at all, since the program is apparently only waiting for one SIGUSR1 signal. The answer is that the mask passed to sigsuspend permits the process to be woken up by the delivery of other kinds of signals, as well—for example, job control signals. If the process is woken up by a signal that doesn't set usr_interrupt, it just suspends itself again until the “right” kind of signal eventually arrives.

    This technique takes a few more lines of preparation, but that is needed just once for each kind of wait criterion you want to use. The code that actually waits is just four lines.

    0 讨论(0)
提交回复
热议问题