{"id":579,"date":"2016-12-07T15:07:30","date_gmt":"2016-12-07T07:07:30","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=579"},"modified":"2016-12-07T15:08:13","modified_gmt":"2016-12-07T07:08:13","slug":"signal%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=579","title":{"rendered":"signal\u51fd\u6570"},"content":{"rendered":"<p>\u8981\u4f7f\u7528UNIX\u7cfb\u7edf\u4e2d\u4fe1\u53f7\u8fd9\u4e00\u529f\u80fd\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<span style=\"color: #993300;\">signal<\/span>\u51fd\u6570\uff1a<\/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: 209px;\" border=\"1\" width=\"1220\" 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\nvoid (*signal(int <span class=\"docEmphItalicAlt\">signo<\/span>, void (*<span class=\"docEmphItalicAlt\">func<\/span>)(int)))(int);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td class=\"docTableCell\" align=\"right\" valign=\"top\">\n<p class=\"docText\">Returns: previous disposition of signal (see following) if OK, <tt>SIG_ERR<\/tt> on error<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u4e2a\u5b9a\u4e49\u770b\u4e0a\u53bb\u6709\u70b9\u590d\u6742\uff0c\u53ef\u4ee5\u5c06\u5176\u7b80\u5316\u4e3a\uff1a<\/p>\n<p>typedef void (*HANDER) (int);<\/p>\n<p><span style=\"text-decoration: underline;\"><span style=\"color: #993300; text-decoration: underline;\">HANDER signal(int signo, HANDER func);<\/span><\/span><\/p>\n<p>\u539f\u578b\u7684\u5206\u6790\uff1a<\/p>\n<p>\u53ef\u4ee5\u628a\u539f\u578b\u770b\u505a\uff1avoid (<span style=\"color: #008000;\">*<\/span><span style=\"text-decoration: underline; color: #800000;\">signal(&#8230;)<\/span>)(int);<\/p>\n<p>signal(&#8230;)\u662f\u4e00\u4e2a<span style=\"color: #800000;\">\u51fd\u6570<\/span>\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2a<span style=\"color: #008000;\">\u6307\u9488<span style=\"color: #000000;\">\uff0c\u8be5<span style=\"color: #008000;\">\u6307\u9488<\/span>\u7684\u7c7b\u578b\u53c8\u662f\u4ec0\u4e48\u5462\uff1f<\/span><\/span><\/p>\n<p>\u8fd9\u65f6\u518d\u628a\u539f\u578b\u770b\u505a<span style=\"color: #0000ff;\">void (<\/span><span style=\"color: #008000;\">*<\/span><span style=\"color: #0000ff;\">)(int)<\/span>;\u8be5\u6307\u9488\u6307\u5411\u4e00\u4e2a\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u7684\u53c2\u6570\u662f\u4e00\u4e2aint\u578b\u53d8\u91cf\uff0c\u5e76\u8fd4\u56devoid\u3002\u2014\u2014\u8fd9\u5c31\u662f<span style=\"color: #008000;\">\u6307\u9488<\/span>\u7684\u7c7b\u578b\u3002<\/p>\n<p><em>signo<\/em>\u53c2\u6570\u662f\u8981\u6355\u83b7\u7684\u4fe1\u53f7\u540d\u5b57\u3002<\/p>\n<p><em>func<\/em>\u662f\uff1a<\/p>\n<ol>\n<li>\u5e38\u91cf<span style=\"color: #993300;\">SIG_IGN<\/span>\uff0c\u544a\u8bc9\u7cfb\u7edf\u5ffd\u7565\u4fe1\u53f7\u3002\uff08<span style=\"color: #993300;\">SIGKILL<\/span>\u548c<span style=\"color: #993300;\">SIGSTOP<\/span>\u4e0d\u80fd\u5ffd\u7565\uff09<\/li>\n<li>\u5e38\u91cf<span style=\"color: #993300;\">SIG_DFL<\/span>\uff0c\u544a\u8bc9\u7cfb\u7edf\u4f7f\u7528\u9ed8\u8ba4\u65b9\u6cd5\u5904\u7406\u3002<\/li>\n<li>\u5f53\u4ea7\u751f\u4fe1\u53f7\u65f6\u5904\u7406\u4fe1\u53f7\u7684\u51fd\u6570\u7684\u5730\u5740\u3002<\/li>\n<\/ol>\n<p>\u5982\u540c\u4e0a\u9762\u539f\u578b\u5206\u6790\u6240\u8bf4\uff0c<span style=\"color: #993300;\">signal<\/span>\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u6307\u9488\uff08\u8be5\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u662f<span style=\"color: #993300;\">void<\/span>\uff09\u3002signal\u51fd\u6570\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570signo\u662f\u6574\u578b\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5e26\u4e00\u4e2a\u6574\u6570\u53c2\u6570\u7684\u51fd\u6570\u6307\u9488\uff08\u8be5\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u662f<span style=\"color: #993300;\">void<\/span>\uff09\u3002<\/p>\n<h1>\u4f8b\u5b50<\/h1>\n<p>\u6211\u4eec\u5728\u540e\u53f0\u8fd0\u884c\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u7136\u540e\u4f7f\u7528<span style=\"color: #993300;\">kill<\/span>\u547d\u4ee4\u5411\u5b83\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u3002<span style=\"color: #993300;\">kill<\/span>\u547d\u4ee4\u548c<span style=\"color: #993300;\">kill<\/span>\u51fd\u6570\u53ea\u662f\u53d1\u9001\u4fe1\u53f7\u5230\u8fdb\u7a0b\u6216\u8fdb\u7a0b\u7ec4\u3002\u4fe1\u53f7\u662f\u5426\u80fd\u7ed3\u675f\u8fdb\u7a0b\u4f9d\u8d56\u4e8e\u6536\u5230\u4fe1\u53f7\u7684\u8fdb\u7a0b\u662f\u5426\u5b89\u6392\u4e86\u5904\u7406\u4fe1\u53f7\u7684\u51fd\u6570\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<pre>#include &lt;stdio.h&gt;\r\n#include &lt;signal.h&gt;\r\n\r\nstatic void sig_usr(int);   \/* one handler for both signals *\/\r\n\r\nint\r\nmain(void)\r\n{\r\n    if (signal(SIGUSR1, sig_usr) == SIG_ERR)\r\n        err_sys(\"can't catch SIGUSR1\");\r\n    if (signal(SIGUSR2, sig_usr) == SIG_ERR)\r\n        err_sys(\"can't catch SIGUSR2\");\r\n    for ( ; ; )\r\n        pause();\r\n}\r\n\r\nstatic void\r\nsig_usr(int signo)      \/* argument is signal number *\/\r\n{\r\n    if (signo == SIGUSR1)\r\n        printf(\"received SIGUSR1\\n\");\r\n    else if (signo == SIGUSR2)\r\n        printf(\"received SIGUSR2\\n\");\r\n    else\r\n        err_dump(\"received signal %d\\n\", signo);\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6267\u884c\u65b9\u6cd5\uff1a<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<pre>   $ <span class=\"docEmphStrong\">.\/a.out &amp;<\/span>                   <span class=\"docEmphItalicAlt\">start process in background<\/span>\r\n   [1]      7216                 <span class=\"docEmphasis\">job-control shell prints job number and process ID<\/span>\r\n   $ <span class=\"docEmphStrong\">kill -USR1 7216<\/span>             <span class=\"docEmphItalicAlt\">send it<\/span> SIGUSR1\r\n   received SIGUSR1\r\n   $ <span class=\"docEmphStrong\">kill -USR2 7216<\/span>             <span class=\"docEmphItalicAlt\">send it<\/span> SIGUSR2\r\n   received SIGUSR2\r\n   $ <span class=\"docEmphStrong\">kill 7216<\/span>                   <span class=\"docEmphItalicAlt\">now send it<\/span> SIGTERM\r\n   [1]+  Terminated    .\/a.out\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u7a0b\u5e8f\u7684\u542f\u52a8\uff1a<\/h2>\n<p>\u5f53\u7a0b\u5e8f\u88ab\u6267\u884c\u65f6\uff0c\u6240\u6709\u7684\u4fe1\u53f7\u72b6\u6001\u8981\u4e48\u662f\u9ed8\u8ba4\uff0c\u8981\u4e48\u662f\u5ffd\u7565\u3002\u4e00\u822c\uff0c\u6240\u6709\u4fe1\u53f7\u90fd\u88ab\u8bbe\u7f6e\u6210\u5b83\u4eec\u7684\u9ed8\u8ba4\u52a8\u4f5c\uff0c\u9664\u975e\u8fdb\u7a0b\u8c03\u7528<span style=\"color: #993300;\">exec<\/span>\u51fd\u6570\u5ffd\u7565\u4fe1\u53f7\u3002\u786e\u5207\u5730\u8bb2\uff0c<span style=\"color: #993300;\">exec<\/span>\u51fd\u6570\u5c06\u6355\u6349\u5230\u7684\u4fe1\u53f7\u90fd\u66f4\u6539\u4e3a\u5b83\u4eec\u7684\u9ed8\u8ba4\u52a8\u4f5c\uff0c\u5e76\u4e14\u4e0d\u6539\u53d8\u5176\u5b83\u5269\u4e0b\u4fe1\u53f7\u7684\u72b6\u6001\u3002\uff08\u901a\u5e38\uff0c\u4e00\u4e2a\u4fe1\u53f7\u88ab\u8fdb\u7a0b\u6355\u83b7\u540e\uff0c\u8be5\u8fdb\u7a0b\u8c03\u7528<span style=\"color: #993300;\">exec<\/span>\u542f\u52a8\u7684\u65b0\u7a0b\u5e8f\u4e0d\u80fd\u6355\u83b7\u8fd9\u4e2a\u4fe1\u53f7\uff0c\u56e0\u4e3a\u4fe1\u53f7\u6355\u6349\u7a0b\u5e8f\u7684\u5730\u5740\u5728\u65b0\u8c03\u7528\u7684\u7a0b\u5e8f\u4e2d\u6ca1\u6709\u610f\u4e49\u3002\uff09<\/p>\n<p>\u672c\u4f8b\u6f14\u793a\u4e86shell\u5982\u4f55\u5bf9\u5f85\u540e\u53f0\u8fdb\u7a0b\u7684\u4e2d\u65ad\u548c\u9000\u51fa\u4fe1\u53f7\u3002shell\u672c\u8eab\u5e76\u4e0d\u652f\u6301\u4efb\u52a1\u63a7\u5236\uff0c\u5f53\u6211\u4eec\u6267\u884c\uff1a<\/p>\n<p><span style=\"color: #993300;\">cc main.c &amp;<\/span><\/p>\n<p>\u65f6\uff0cshell\u81ea\u52a8\u8bbe\u7f6e\u540e\u53f0\u8fdb\u7a0b\u7684\u4e2d\u65ad\u548c\u9000\u51fa\u4fe1\u53f7\u4e3a\u5ffd\u7565\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u6211\u4eec\u952e\u76d8\u8f93\u5165\u4e2d\u65ad\u5b57\u7b26\u4e0d\u4f1a\u4e2d\u65ad\u540e\u53f0\u8fdb\u7a0b\u3002\u5982\u679cshell\u4e0d\u8fd9\u4e48\u505a\u7684\u8bdd\uff0c\u6211\u4eec\u8f93\u5165\u4e2d\u65ad\u5b57\u7b26\u540e\u4e0d\u4ec5\u524d\u53f0\u8fdb\u7a0b\u4f1a\u7ec8\u6b62\uff0c\u540e\u53f0\u8fdb\u7a0b\u4e5f\u4f1a\u7ec8\u6b62\u3002<\/p>\n<p>\u5f88\u591a\u4ea4\u4e92\u7a0b\u5e8f\u90fd\u6355\u83b7\u4e24\u79cd\u4fe1\u53f7\uff0c\u5b83\u4eec\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/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;\">         void sig_int(int), sig_quit(int);\r\n\r\n         if (signal(SIGINT, SIG_IGN) != SIG_IGN)\r\n             signal(SIGINT, sig_int);\r\n         if (signal(SIGQUIT, SIG_IGN) != SIG_IGN)\r\n             signal(SIGQUIT, sig_quit);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5982\u679c\u4fe1\u53f7\u6ca1\u6709\u88ab\u8fdb\u7a0b\u5ffd\u7565\uff0c\u90a3\u4e48\u624d\u4f1a\u6267\u884c\u8fd9\u4e9b\u4ee3\u7801\u3002<\/p>\n<p>\u8fd9\u4e24\u4e2a\u8c03\u7528signal\u51fd\u6570\u7684\u65b9\u6cd5\u90fd\u6709\u9650\u5236\uff1a\u4e0d\u80fd\u5224\u65ad\u5f53\u524d\u4fe1\u53f7\u7684\u5e03\u5c40\uff08disposition\uff09\u662f\u5426\u5df2\u7ecf\u6539\u53d8\u3002<\/p>\n<h2>\u8fdb\u7a0b\u521b\u8fdb\uff1a<\/h2>\n<p>\u5f53\u8fdb\u7a0b\u8c03\u7528<span style=\"color: #993300;\">fork<\/span>\u65f6\uff0c\u5b50\u8fdb\u7a0b\u7ee7\u627f\u4e86\u7236\u8fdb\u7a0b\u7684\u4fe1\u53f7\u5e03\u5c40\uff08disposition\uff09\u3002\u56e0\u4e3a\u5b50\u8fdb\u7a0b\u4ece\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u955c\u50cf\u5f00\u59cb\uff0c\u6240\u4ee5\u4fe1\u53f7\u6355\u6349\u7a0b\u5e8f\u7684\u5730\u5740\u5bf9\u5b50\u8fdb\u7a0b\u662f\u6709\u610f\u4e49\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4f7f\u7528UNIX\u7cfb\u7edf\u4e2d\u4fe1\u53f7\u8fd9\u4e00\u529f\u80fd\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528signal\u51fd\u6570\uff1a #include &lt;signal.h&gt; void (*signal(int s&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=579\"> 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-579","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/579","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=579"}],"version-history":[{"count":8,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":587,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions\/587"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}