{"id":669,"date":"2017-02-06T16:08:21","date_gmt":"2017-02-06T08:08:21","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=669"},"modified":"2017-02-09T15:31:41","modified_gmt":"2017-02-09T07:31:41","slug":"sigpending%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=669","title":{"rendered":"sigpending\u51fd\u6570"},"content":{"rendered":"<p><span style=\"color: #993300;\">sigpending<\/span>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u88ab\u5f53\u524d\u8fdb\u7a0b\u963b\u585e\u7684\u4fe1\u53f7\u96c6\uff0c\u8fd9\u4e2a\u4fe1\u53f7\u96c6\u901a\u8fc7<em>set<\/em>\u53c2\u6570\u8fd4\u56de\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<table class=\"allBorders\" style=\"height: 205px;\" border=\"1\" width=\"498\" rules=\"none\" cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td class=\"docTableCell\" align=\"left\" valign=\"top\">\n<pre><span style=\"color: #993300;\">#include &lt;signal.h&gt;\r\n\r\nint sigpending(sigset_t *<span class=\"docEmphItalicAlt\">set<\/span>);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">Returns: 0 if OK, 1 on error<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u4f8b\u5b50\uff1a<\/h3>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h5 class=\"docExampleTitle\">Figure 10.15. Example of signal sets and <span style=\"color: #993300;\"><tt>sigprocmask<\/tt><\/span><\/h5>\n<pre><span style=\"color: #993300;\">#include \"apue.h\"\r\n\r\nstatic void sig_quit(int);\r\n\r\nint\r\nmain(void)\r\n{\r\n    sigset_t    newmask, oldmask, pendmask;\r\n\r\n    if (signal(SIGQUIT, sig_quit) == SIG_ERR)\r\n        err_sys(\"can't catch SIGQUIT\");\r\n\r\n    \/*\r\n     * Block SIGQUIT and save current signal mask.\r\n     *\/\r\n    sigemptyset(&amp;newmask);\r\n    sigaddset(&amp;newmask, SIGQUIT);\r\n    if (sigprocmask(SIG_BLOCK, &amp;newmask, &amp;oldmask) &lt; 0)\r\n        err_sys(\"SIG_BLOCK error\");\r\n\r\n    sleep(5);   \/* SIGQUIT here will remain pending *\/\r\n    if (sigpending(&amp;pendmask) &lt; 0)\r\n        err_sys(\"sigpending error\");\r\n    if (sigismember(&amp;pendmask, SIGQUIT))\r\n        printf(\"\\nSIGQUIT pending\\n\");\r\n\r\n    \/*\r\n     * Reset signal mask which unblocks SIGQUIT.\r\n     *\/\r\n    if (sigprocmask(SIG_SETMASK, &amp;oldmask, NULL) &lt; 0)\r\n        err_sys(\"SIG_SETMASK error\");\r\n    printf(\"SIGQUIT unblocked\\n\");\r\n\r\n    sleep(5);   \/* SIGQUIT here will terminate with core file *\/\r\n    exit(0);\r\n}\r\n\r\nstatic void\r\nsig_quit(int signo)\r\n{\r\n    printf(\"caught SIGQUIT\\n\");\r\n    if (signal(SIGQUIT, SIG_DFL) == SIG_ERR)\r\n        err_sys(\"can't reset SIGQUIT\");\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Figure 10.15\u663e\u793a\u4e86\u5f88\u591a\u6211\u4eec\u63cf\u8ff0\u8fc7\u7684\u4fe1\u53f7\u529f\u80fd\u3002<\/p>\n<p>\u8fdb\u7a0b\u963b\u585e\u4e86<span style=\"color: #993300;\">SIGQUIT<\/span>\u4fe1\u53f7\uff0c\u4fdd\u5b58\u4e86\u5f53\u524d\u7684\u4fe1\u53f7\u63a9\u7801\uff08\u7528\u4e8e\u4e4b\u540e\u91cd\u7f6e\uff09\uff0c\u7136\u540e\u4f11\u77205\u79d2\u949f\u3002\u5728\u8fdb\u7a0b\u8fd0\u884c\u671f\u95f4\u53d1\u751f\u7684\u4efb\u4f55\u9000\u51fa\u4fe1\u53f7\u90fd\u4f1a\u88ab\u963b\u585e\uff0c\u76f4\u5230\u89e3\u9664\u963b\u585e\u624d\u4f1a\u88ab\u4f20\u9001\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u5f53\u6211\u4eec\u963b\u585e\u4e86\u4fe1\u53f7\u65f6\u4fdd\u5b58\u4e86\u65e7\u7684\u4fe1\u53f7\u63a9\u7801\u3002\u89e3\u9664\u963b\u585e\u65f6\u6211\u4eec\u8981\u5bf9\u65e7\u7684\u63a9\u4f5c<span style=\"color: #993300;\">SIG_SETMASK<\/span>\u64cd\u4f5c\u3002\u6216\u8005\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u53ea\u5bf9\u963b\u585e\u7684\u4fe1\u53f7\u505a<span style=\"color: #993300;\">SIG_UNBLOCK<\/span>\u64cd\u4f5c\u3002\u8981\u77e5\u9053\u5982\u679c\u6211\u4eec\u5199\u4e86\u4e00\u4e2a\u51fd\u6570\uff0c\u5b83\u80fd\u88ab\u5176\u5b83\u51fd\u6570\u8c03\u7528\uff0c\u5e76\u4e14\u6211\u4eec\u9700\u8981\u5728\u6211\u4eec\u7684\u51fd\u6570\u4e2d\u963b\u585e\u4e00\u4e2a\u4fe1\u53f7\uff0c\u65e0\u8bba\u5982\u4f55\u6211\u4eec\u90fd\u4e0d\u80fd\u901a\u8fc7<span style=\"color: #993300;\">SIG_UNBLOCK<\/span>\u53bb\u89e3\u9501\u8be5\u4fe1\u53f7\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5fc5\u987b\u4f7f\u7528<span style=\"color: #993300;\">SIG_SETMASK<\/span>\u5e76\u4e14\u91cd\u7f6e\u4fe1\u53f7\u63a9\u7801\u5230\u5b83\u4e4b\u524d\u7684\u503c\uff0c\u56e0\u4e3a\u8c03\u7528\u8005\u53ef\u80fd\u5728\u6211\u4eec\u8c03\u7528\u51fd\u6570\u4e4b\u524d\u5c31\u5df2\u7ecf\u963b\u585e\u4e86\u8fd9\u4e2a\u4fe1\u53f7\u3002\u7a0d\u540e10.18\u8282\u4ecb\u7ecd<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u65f6\u4f1a\u770b\u5230\u76f8\u5173\u7684\u4f8b\u5b50\u3002<\/p>\n<p>\u5982\u679c\u5728\u51fd\u6570\u4f11\u7720\u9636\u6bb5\u4ea7\u751f\u4e86\u9000\u51fa\u4fe1\u53f7\uff0c\u90a3\u4e48\u8fd9\u4e2a\u9000\u51fa\u4fe1\u53f7\u5e76\u4e0d\u88ab\u963b\u585e\uff0c\u6240\u4ee5\u5b83\u4f1a\u5728<span style=\"color: #993300;\">sigprocmask<\/span>\u8fd4\u56de\u4e4b\u524d\u88ab\u4f20\u9012\u5230\u8fdb\u7a0b\u3002\u6211\u4eec\u5c06\u4f1a\u770b\u5230\u5b83\u53d1\u751f\uff0c\u56e0\u4e3a\u5728\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u7684<span style=\"color: #993300;\">printf<\/span>\u4f1a\u5728<span style=\"color: #993300;\">sigprocmask<\/span>\u540e\u9762\u7684<span style=\"color: #993300;\">printf<\/span>\u4e4b\u524d\u8f93\u51fa\u3002<\/p>\n<p>\u8fdb\u7a0b\u4f11\u77205\u79d2\u949f\uff0c\u7528\u4e8e\u5e72\u5176\u5b83\u7684\u4e8b\u60c5\u3002\u5982\u679c\u6211\u4eec\u5728\u4f11\u7720\u5176\u95f4\u4ea7\u751f\u4e86\u9000\u51fa\u4fe1\u53f7\uff0c\u8fd9\u4e2a\u4fe1\u53f7\u4ea4\u4f1a\u7ec8\u6b62\u8fdb\u7a0b\uff0c\u56e0\u4e3a\u5f53\u6355\u83b7\u8be5\u4fe1\u53f7\u65f6\uff0c\u6211\u4eec\u91cd\u7f6e\u4fe1\u53f7\u7684\u52a8\u4f5c\u4e3a\u9ed8\u8ba4\u3002\u770b\u4e0b\u9762\u7684\u8f93\u51fa\uff0c\u7ec8\u7aef\u6253\u5370<span style=\"color: #993300;\">^\\<\/span>\u4ee3\u8868\u7ec8\u7aef\u9000\u51fa\u5b57\u7b26:<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<pre><span style=\"color: #993300;\">    $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n    <span class=\"docEmphStrong\">\r\n    ^\\<\/span>                       <span class=\"docEmphItalicAlt\">generate signal once (before 5 seconds are up)<\/span>\r\n    SIGQUIT pending          <span class=\"docEmphItalicAlt\">after return from<\/span> sleep\r\n    caught SIGQUIT           <span class=\"docEmphItalicAlt\">in signal handler<\/span>\r\n    SIGQUIT unblocked        <span class=\"docEmphItalicAlt\">after return from<\/span> sigprocmask\r\n    <span class=\"docEmphStrong\">^\\<\/span>Quit(coredump)         <span class=\"docEmphItalicAlt\">generate signal again<\/span>\r\n    $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n    <span class=\"docEmphStrong\">\r\n    ^\\^\\^\\^\\^\\^\\^\\^\\^\\^\\<\/span>     <span class=\"docEmphItalicAlt\">generate signal 10 times (before 5 seconds are up)<\/span>\r\n    SIGQUIT pending\r\n    caught SIGQUIT           <span class=\"docEmphItalicAlt\">signal is generated only once<\/span>\r\n    SIGQUIT unblocked\r\n    <span class=\"docEmphStrong\">^\\<\/span>Quit(coredump)         <span class=\"docEmphItalicAlt\">generate signal again<\/span>\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>sigpending\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u88ab\u5f53\u524d\u8fdb\u7a0b\u963b\u585e\u7684\u4fe1\u53f7\u96c6\uff0c\u8fd9\u4e2a\u4fe1\u53f7\u96c6\u901a\u8fc7set\u53c2\u6570\u8fd4\u56de\u3002 #include &lt;signal.h&gt; int sigpen&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=669\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-669","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=669"}],"version-history":[{"count":2,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/669\/revisions"}],"predecessor-version":[{"id":678,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/669\/revisions\/678"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}