How to use sigsuspend

前端 未结 2 1138
后悔当初
后悔当初 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.

提交回复
热议问题