sigprocmask函數使用精要

#include "apue.h"測試

static void     sig_quit(int);ui

int
main(void)
{
        sigset_t        newmask, oldmask, pendmask;進程

        if (signal(SIGQUIT, sig_quit) == SIG_ERR)
                err_sys("can't catch SIGQUIT");
        sigemptyset(&newmask);
        sigaddset(&newmask, SIGQUIT);
        sigaddset(&newmask, SIGINT);
        if (sigprocmask(SIG_BLOCK, &newmask, &oldmask) < 0)
                err_sys("SIG_BLOCK error");rem

        sleep(5);       /* SIGQUIT here will remain pending */it

        if (sigpending(&pendmask) < 0)
                err_sys("sigpending error");
        if (sigismember(&pendmask, SIGQUIT))file

             printf("\nSIGQUIT pending\n");
             if (sigismember(&pendmask, SIGINT))
                printf("\nSIGINT pending\n");    程序

        /*
         * Reset signal mask which unblocks SIGQUIT.
         */
        if (sigprocmask(SIG_SETMASK, &oldmask, NULL) < 0)
                err_sys("SIG_SETMASK error");
        printf("SIGQUIT unblocked\n");error

        sleep(5);       /* SIGQUIT here will terminate with core file */
        exit(0);
}ember

static void
sig_quit(int signo)
{
        printf("caught SIGQUIT\n");
        if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)
                err_sys("can't reset SIGQUIT");
}static

以上程序出自apue,實在是爲了測試一下sigprocmask的做用,由運行的結果能夠看出,sigprocmask只是將信號阻塞,並未消除,也就是說,信號集一旦恢復以後,該處理的進程仍是要處理的,在第一個sleep以前按下中斷鍵,進程不會處理,可是一旦恢復以後,進程就退出了。

相關文章
相關標籤/搜索