{"id":716,"date":"2017-03-06T11:41:49","date_gmt":"2017-03-06T03:41:49","guid":{"rendered":"http:\/\/www.5x44.cn\/?p=716"},"modified":"2017-03-07T16:39:36","modified_gmt":"2017-03-07T08:39:36","slug":"system%e5%87%bd%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/www.5x44.cn\/?p=716","title":{"rendered":"system\u51fd\u6570"},"content":{"rendered":"<p>\u5728APUE8.13\u8282\uff0c\u6211\u4eec\u663e\u793a\u4e86\u4e00\u4e2a<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u7684\u5b9e\u73b0\u3002\u90a3\u4e2a\u7248\u672c\u4e2d\u5e76\u4e0d\u5904\u7406\u4efb\u4f55\u4fe1\u53f7\u3002POSIX.1\u8981\u6c42<span style=\"color: #993300;\">system<\/span>\u5ffd\u7565<span style=\"color: #993300;\">SIGINT<\/span>\u548c<span style=\"color: #993300;\">SIGQUIT<\/span>\u5e76\u4e14\u963b\u585e<span style=\"color: #993300;\">SIGCHLD<\/span>\u3002\u4e4b\u524d\u663e\u793a\u4e86\u4e00\u4e2a\u6b63\u786e\u5904\u7406\u8fd9\u4e9b\u4fe1\u53f7\u7684\u7248\u672c\uff0c\u73b0\u5728\u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u4e3a\u4ec0\u4e48\u8981\u62c5\u5fc3\u8fd9\u4e9b\u4fe1\u53f7\u7684\u5904\u7406\u3002<\/p>\n<h1>\u4f8b\u5b50<\/h1>\n<p>Figure 10.26\u7684\u7a0b\u5e8f\u663e\u793a\u4e86\u4e00\u4e2a\u4f7f\u7528APUE8.13\u8282\u4e2d\u7684<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u8c03\u7528<span style=\"color: #993300;\">ed<\/span>\u7f16\u8f91\u5668\u3002\u8be5\u7a0b\u5e8f\u6355\u83b7\u4e86<span style=\"color: #993300;\">SIGINT<\/span>\u548c<span style=\"color: #993300;\">SIGCHLD<\/span>\u4fe1\u53f7\u3002\u5982\u679c\u6211\u4eec\u8c03\u7528\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\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;\">    $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n<span class=\"docEmphStrong\">\r\n    a<\/span>                         <span class=\"docEmphItalicAlt\">append text to the editor's buffer<\/span>\r\n<span class=\"docEmphStrong\">\r\n    Here is one line of text<\/span>\r\n<span class=\"docEmphStrong\">\r\n    .<\/span>                         <span class=\"docEmphItalicAlt\">period on a line by itself stops append mode<\/span>\r\n<span class=\"docEmphStrong\">\r\n    1,$p<\/span>                      <span class=\"docEmphItalicAlt\">print first through last lines of buffer to see what's there<\/span>\r\n    Here is one line of text\r\n    <span class=\"docEmphStrong\">w temp.foo<\/span>                <span class=\"docEmphItalicAlt\">write the buffer to a file<\/span>\r\n    25                        <span class=\"docEmphItalicAlt\">editor says it wrote 25 bytes<\/span>\r\n    \r\n    <span class=\"docEmphStrong\">q<\/span>                         <span class=\"docEmphItalicAlt\">and leave the editor<\/span>\r\n    caught SIGCHLD\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f53\u7f16\u8f91\u5668\u7ec8\u6b62\u65f6\uff0c\u7cfb\u7edf\u53d1\u9001<span style=\"color: #993300;\">SIGCHLD<\/span>\u4fe1\u53f7\u7ed9\u5b83\u7684\u7236\u8fdb\u7a0b\uff08\u4e5f\u5c31\u662f<span style=\"color: #993300;\">a.out<\/span>\u8fdb\u7a0b\uff09\u3002\u6211\u4eec\u6355\u83b7\u5230\u5b83\u5e76\u4e14\u4ece\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4e2d\u8fd4\u56de\u3002But if it is catching the <span style=\"color: #993300;\">SIGCHLD<\/span> signal, the parent should be doing so because it has created its own children, so that it knows when its children have terminated. \u5728<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u8fd0\u884c\u65f6\u4fe1\u53f7\u4f20\u9001\u5230\u7236\u8fdb\u7a0b\u91cc\uff0c\u8be5\u4fe1\u53f7\u5e94\u8be5\u662f\u88ab\u963b\u585e\u7684\u3002\u800c\u4e14\u8fd9\u4e5f\u662fPOSIX.1\u6307\u5b9a\u7684\u3002\u53e6\u5916\uff0c\u5f53\u7531<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u7a7f\u4ef6\u7684\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u65f6\uff0c\u5b83\u5e94\u8be5\u6b3a\u9a97<span style=\"color: #993300;\">system<\/span>\u7684\u8c03\u7528\u8005\u53bb\u8ba4\u4e3a\u81ea\u5df1\u7684\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u7ec8\u6b62\u4e86\u3002\u90a3\u4e48\u8c03\u7528\u8005\u4f1a\u4f7f\u7528<span style=\"color: #993300;\">wait<\/span>\u51fd\u6570\u53bb\u83b7\u5f97\u5b50\u8fdb\u7a0b\u7684\u7ed3\u675f\u72b6\u6001\uff0c\u4ece\u800c\u9632\u6b62<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u83b7\u5f97\u5b50\u8fdb\u7a0b\u7684\u7ed3\u675f\u72b6\u6001\u548c\u8fd4\u56de\u503c\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u518d\u6b21\u8fd0\u884c\u8fd9\u4e2a\u7a0b\u5e8f\uff0c\u8fd9\u6b21\u7ed9\u7f16\u8f91\u5668\u53d1\u9001\u4e00\u4e2a\u4e2d\u65ad\u4fe1\u53f7\uff0c\u6211\u4eec\u4f1a\u5f97\u5230\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;\"> $ <span class=\"docEmphStrong\">.\/a.out<\/span>\r\n<span class=\"docEmphStrong\">\r\n    a<\/span>              <span class=\"docEmphItalicAlt\">append text to the editor's buffer<\/span>\r\n<span class=\"docEmphStrong\">\r\n    hello, world<\/span>\r\n<span class=\"docEmphStrong\">\r\n    .<\/span>              <span class=\"docEmphItalicAlt\">period on a line by itself stops append mode<\/span>\r\n<span class=\"docEmphStrong\">\r\n    1,$p<\/span>           <span class=\"docEmphItalicAlt\">print first through last lines to see what's there<\/span>\r\n    hello, world\r\n    <span class=\"docEmphStrong\">w temp.foo<\/span>     <span class=\"docEmphItalicAlt\">write the buffer to a file<\/span>\r\n    13             <span class=\"docEmphItalicAlt\">editor says it wrote 13 bytes<\/span>\r\n<span class=\"docEmphStrong\">\r\n    ^?<\/span>             <span class=\"docEmphItalicAlt\">type the interrupt character<\/span>\r\n    ?              <span class=\"docEmphItalicAlt\">editor catches signal, prints question mark<\/span>\r\n    caught SIGINT  <span class=\"docEmphItalicAlt\">and so does the parent process<\/span>\r\n<span class=\"docEmphStrong\">\r\n    q<\/span>              <span class=\"docEmphItalicAlt\">leave editor<\/span>\r\n    caught SIGCHLD\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u56de\u5fc6APUE9.6\u8282\uff0c\u8f93\u5165\u4e2d\u65ad\u5b57\u7b26\u5f15\u53d1\u7684\u4e2d\u65ad\u4fe1\u53f7\u4f1a\u53d1\u9001\u5230\u6240\u6709\u524d\u53f0\u8fdb\u7a0b\u7ec4\u7684\u8fdb\u7a0b\u4e2d\u3002Figure10.27\u663e\u793a\u4e86\u5f53\u7f16\u8f91\u5668\u8fd0\u884c\u65f6\uff0c\u8fdb\u7a0b\u7684\u5b89\u6392\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<span style=\"color: #993300;\">SIGINT<\/span>\u88ab\u53d1\u9001\u5230\u4e09\u4e2a\u524d\u53f0\u8fdb\u7a0b\u4e2d\u3002\uff08shell\u5ffd\u7565\u5b83\uff09\u5c31\u50cf\u6211\u4eec\u4ece\u8f93\u51fa\u770b\u5230\u7684\u90a3\u6837\uff0c<span style=\"color: #993300;\">a.out<\/span>\u8fdb\u7a0b\u548c\u7f16\u8f91\u5668\u90fd\u6355\u83b7\u5230\u4e86\u8fd9\u4e2a\u4fe1\u53f7\u3002\u4f46\u662f\u5f53\u6211\u4eec\u4f7f\u7528<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u8fd0\u884c\u53e6\u4e00\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u8fd9\u4e24\u4e2a\u8fdb\u7a0b\u4e0d\u4f1a\u90fd\u6210\u4e3a\u7236\u8fdb\u7a0b\u5e76\u4e14\u5b50\u8fdb\u7a0b\u6355\u83b7\u4e24\u4e2a\u7ec8\u7aef\u4ea7\u751f\u7684\u4fe1\u53f7\uff1a\u4e2d\u65ad\u548c\u9000\u51fa\u3002\u8fd9\u4e24\u4e2a\u4fe1\u53f7\u5e94\u8be5\u5df2\u7ecf\u88ab\u53d1\u9001\u5230\u8be5\u8fd0\u884c\u7684\u5b50\u8fdb\u7a0b\u3002\u56e0\u4e3a\u547d\u4ee4\u662f\u88ab<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u6267\u884c\u7684\uff0c\u8be5\u547d\u4ee4\u53ef\u4ee5\u662f\u4e00\u4e2a\u4ea4\u4e92\u547d\u4ee4\uff08\u5c31\u50cf<span style=\"color: #993300;\">ed<\/span>\u7a0b\u5e8f\u4e00\u6837\uff09\uff0c\u5e76\u4e14\u56e0\u4e3a<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u7684\u8c03\u7528\u8005\u5df2\u7ecf\u653e\u5f03\u4e86\u5bf9\u8be5\u6267\u884c\u547d\u4ee4\u7684\u63a7\u5236\u6743\uff0c\u7b49\u5f85\u5b83\u7ed3\u675f\uff0csystem\u51fd\u6570\u7684\u8c03\u7528\u8005\u5e94\u8be5\u4e0d\u4f1a\u6536\u5230\u7ec8\u7aef\u4ea7\u751f\u7684\u8fd9\u4e24\u79cd\u4fe1\u53f7\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48POSIX.1\u6307\u5b9asystem\u51fd\u6570\u5e94\u8be5\u5728\u547d\u4ee4\u6267\u884c\u671f\u95f4\u5ffd\u7565\u8fd9\u4e24\u79cd\u4fe1\u53f7\uff08\u4e2d\u65ad\u548c\u9000\u51fa\uff09\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h3 class=\"docExampleTitle\">Figure 10.26. Using <tt>system<\/tt> to invoke the <tt>ed<\/tt> editor<\/h3>\n<pre>#include \"apue.h\"\r\n\r\nstatic void\r\nsig_int(int signo)\r\n{\r\n    printf(\"caught SIGINT\\n\");\r\n}\r\n\r\nstatic void\r\nsig_chld(int signo)\r\n{\r\n    printf(\"caught SIGCHLD\\n\");\r\n}\r\n\r\nint\r\nmain(void)\r\n{\r\n     if (signal(SIGINT, sig_int) == SIG_ERR)\r\n         err_sys(\"signal(SIGINT) error\");\r\n     if (signal(SIGCHLD, sig_chld) == SIG_ERR)\r\n         err_sys(\"signal(SIGCHLD) error\");\r\n     if (system(\"\/bin\/ed\") &lt; 0)\r\n         err_sys(\"system() error\");\r\n     exit(0);\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-717\" src=\"http:\/\/blog.5x44.cn\/wp-content\/uploads\/2017\/03\/Figure10.27.jpg\" alt=\"figure10-27\" width=\"706\" height=\"167\" srcset=\"https:\/\/www.5x44.cn\/wp-content\/uploads\/2017\/03\/Figure10.27.jpg 706w, https:\/\/www.5x44.cn\/wp-content\/uploads\/2017\/03\/Figure10.27-300x71.jpg 300w, https:\/\/www.5x44.cn\/wp-content\/uploads\/2017\/03\/Figure10.27-604x143.jpg 604w\" sizes=\"auto, (max-width: 706px) 100vw, 706px\" \/><\/p>\n<h2>\u4f8b\u5b50<\/h2>\n<p>Figure10.28\u663e\u793a\u4e86<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u5904\u7406\u4fe1\u53f7\u7684\u5b9e\u73b0\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u6211\u4eec\u5728Figure10.26\u4e2d\u4f7f\u7528\u6211\u4eec\u8fd9\u4e2a\u7248\u672c\u7684system\u51fd\u6570\uff0c\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u4ee3\u7801\u81f3\u5c11\u6709\u4ee5\u4e0b\u4e00\u79cd\u7f3a\u9677\u3002<\/p>\n<ol>\n<li>\u5f53\u6211\u4eec\u8f93\u5165\u4e2d\u65ad\u6216\u9000\u51fa\u5b57\u7b26\u65f6\uff0c\u6ca1\u6709\u4fe1\u53f7\u53d1\u9001\u5230\u8c03\u7528\u8fdb\u7a0b\u3002<\/li>\n<li>\u5f53ed\u547d\u4ee4\u9000\u51fa\u65f6\uff0c<span style=\"color: #993300;\">SIGCHLD<\/span>\u4fe1\u53f7\u6ca1\u6709\u88ab\u53d1\u9001\u5230\u8c03\u7528\u8fdb\u7a0b\u3002\u53d6\u800c\u4ee3\u4e4b\u7684\u662f\uff0c\u5b83\u88ab\u963b\u585e\u4e86\u76f4\u5230\u6211\u4eec\u5728\u6700\u8fd1\u4e00\u6b21\u8c03\u7528<span style=\"color: #993300;\">sigprocmask<\/span>\u5bf9\u5176\u8fdb\u884c\u89e3\u9501\uff0c\u4e4b\u540e<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u901a\u8fc7\u8c03\u7528<span style=\"color: #993300;\">waitpid<\/span>\u201c\u627e\u56de\u201d\u5b50\u8fdb\u7a0b\u7684\u7ec8\u6b62\u72b6\u6001\u3002<\/li>\n<\/ol>\n<p>\uff08POSIX.1\u58f0\u660e\u5f53<span style=\"color: #993300;\">SIGCHLD<\/span>\u662fpending\u72b6\u6001\u65f6\uff0c\u5982\u679c<span style=\"color: #993300;\">wait<\/span>\u6216<span style=\"color: #993300;\">waitpid<\/span>\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684\u72b6\u6001\uff0c\u90a3\u4e48<span style=\"color: #993300;\">SIGCHLD<\/span>\u5e94\u8be5\u4e0d\u80fd\u88ab\u4f20\u9001\u5230\u8fdb\u7a0b\uff0c\u9664\u975e\u5176\u5b83\u5b50\u8fdb\u7a0b\u7684\u72b6\u6001\u4e5f\u662f\u53ef\u7528\u7684\u3002\u672c\u4e66\u4e2d\u7684\u56db\u79cd\u5b9e\u73b0\u5e76\u6ca1\u6709\u8ba8\u8bba\u5b83\u7684\u8bed\u6cd5\u3002\u800c\u662f\u4f7f\u7528<span style=\"color: #993300;\">SIGCHLD<\/span>\u5728<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u8c03\u7528<span style=\"color: #993300;\">waitpid<\/span>\u540e\u4fdd\u7559pending\u72b6\u6001\uff1b\u5f53\u4fe1\u53f7\u88ab\u89e3\u9501\u540e\uff0c\u5b83\u4f1a\u88ab\u4f20\u9001\u5230\u8c03\u7528\u8005\u8fdb\u7a0b\u90a3\u91cc\u3002\u5982\u679c\u6211\u4eec\u5728Figure10.26\u7684<span style=\"color: #993300;\">sig_chld<\/span>\u7a0b\u5e8f\u4e2d\u8c03\u7528\u4e86<span style=\"color: #993300;\">wait<\/span>\uff0c\u5b83\u53ef\u80fd\u4f1a\u8fd4\u56de1\u5e76\u8bbe\u7f6e<span style=\"color: #993300;\">errno<\/span>\u4e3a<span style=\"color: #993300;\">ECHILD<\/span>\uff0c\u56e0\u4e3a<span style=\"color: #993300;\">system<\/span>\u51fd\u6570\u5df2\u7ecf\u6536\u5230\u5b50\u8fdb\u7a0b\u7684\u7ec8\u6b62\u72b6\u6001\u3002\uff09<\/p>\n<p>\u5f88\u591a\u8001\u4ee3\u7801\u663e\u793a\u4e86\u4e2d\u65ad\u548c\u9000\u51fa\u4fe1\u53f7\u7684\u5ffd\u7565\uff0c\u5c31\u50cf\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;\">  if ((pid = fork()) &lt; 0) {\r\n        err_sys(\"fork error\");\r\n    } else if (pid == 0) {\r\n        \/* child *\/\r\n        execl(...);\r\n        _exit(127);\r\n    }\r\n\r\n    \/* parent *\/\r\n    old_intr = signal(SIGINT, SIG_IGN);\r\n    old_quit = signal(SIGQUIT, SIG_IGN);\r\n    waitpid(pid, &amp;status, 0)\r\n    signal(SIGINT, old_intr);\r\n    signal(SIGQUIT, old_quit);\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u95ee\u9898\u662f\u6211\u4eec\u4e0d\u80fd\u4fdd\u8bc1\u5728<span style=\"color: #993300;\">fork<\/span>\u540e\u662f\u7236\u8fdb\u7a0b\u5148\u8fd0\u884c\u8fd8\u662f\u5b50\u8fdb\u7a0b\u5148\u8fd0\u884c\u3002\u5982\u679c\u5b50\u8fdb\u7a0b\u5148\u8fd0\u884c\u5e76\u4e14\u7236\u8fdb\u7a0b\u5728\u4e4b\u540e\u7684\u4e00\u6bb5\u65f6\u95f4\u6ca1\u6709\u8fd0\u884c\uff0c\u4e2d\u65ad\u4fe1\u53f7\u6709\u53ef\u80fd\u5728\u7236\u8fdb\u7a0b\u4e4b\u524d\u4ea7\u751f\uff0c\u4ece\u800c\u6539\u53d8\u5b83\u7684\u9884\u7f6e\u884c\u4e3a\uff08disposition\uff09\u88ab\u5ffd\u7565\u3002\u5bf9\u4e8e\u8fd9\u4e2a\u7406\u7531\uff0c\u5728Figure10.28\u6211\u4eec\u5728fork\u4e4b\u524d\u6539\u53d8\u4fe1\u53f7\u7684\u9884\u7f6e\u884c\u4e3a\uff08disposition\uff09\u3002<\/p>\n<p>\u6ce8\u610f\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u5b50\u8fdb\u7a0b\u8c03\u7528<span style=\"color: #993300;\">execl<\/span>\u4e4b\u524d\u91cd\u7f6e\u8fd9\u4e24\u4e2a\u4fe1\u53f7\uff08\u4e2d\u65ad\u548c\u9000\u51fa\uff09\u7684\u9884\u7f6e\u884c\u4e3a\uff08disposition\uff09\u3002\u57fa\u4e8e\u8c03\u7528\u8005\u7684\u9884\u7f6e\u884c\u4e3a\uff08dispositions\uff09\uff0c\u5141\u8bb8<span style=\"color: #993300;\">execl<\/span>\u6539\u53d8\u5b83\u4eec\u7684\u90e8\u7f72\u4e3a\u9ed8\u8ba4\uff0c\u5c31\u50cf\u6211\u4eec\u5728APUE8.10\u8282\u63cf\u8ff0\u7684\u90a3\u6837\u3002<\/p>\n<table border=\"0\" width=\"100%\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\">\n<h3 class=\"docExampleTitle\">Figure 10.28. Correct POSIX.1 implementation of <tt>system<\/tt> function<\/h3>\n<pre><span style=\"color: #993300;\">#include      &lt;sys\/wait.h&gt;\r\n#include      &lt;errno.h&gt;\r\n#include      &lt;signal.h&gt;\r\n#include      &lt;unistd.h&gt;\r\n\r\nint\r\nsystem(const char *cmdstring)   \/* with appropriate signal handling *\/\r\n{\r\n    pid_t               pid;\r\n    int                 status;\r\n    struct sigaction    ignore, saveintr, savequit;\r\n    sigset_t            chldmask, savemask;\r\n\r\n    if (cmdstring == NULL)\r\n        return(1);      \/* always a command processor with UNIX *\/\r\n\r\n    ignore.sa_handler = SIG_IGN;    \/* ignore SIGINT and SIGQUIT *\/\r\n    sigemptyset(&amp;ignore.sa_mask);\r\n    ignore.sa_flags = 0;\r\n    if (sigaction(SIGINT, &amp;ignore, &amp;saveintr) &lt; 0)\r\n        return(-1);\r\n    if (sigaction(SIGQUIT, &amp;ignore, &amp;savequit) &lt; 0)\r\n        return(-1);\r\n    sigemptyset(&amp;chldmask);         \/* now block SIGCHLD *\/\r\n    sigaddset(&amp;chldmask, SIGCHLD);\r\n    if (sigprocmask(SIG_BLOCK, &amp;chldmask, &amp;savemask) &lt; 0)\r\n        return(-1);\r\n\r\n    if ((pid = fork()) &lt; 0) {\r\n        status = -1;    \/* probably out of processes *\/\r\n    } else if (pid == 0) {          \/* child *\/\r\n        \/* restore previous signal actions &amp; reset signal mask *\/\r\n        sigaction(SIGINT, &amp;saveintr, NULL);\r\n        sigaction(SIGQUIT, &amp;savequit, NULL);\r\n        sigprocmask(SIG_SETMASK, &amp;savemask, NULL);\r\n\r\n        execl(\"\/bin\/sh\", \"sh\", \"-c\", cmdstring, (char *)0);\r\n        _exit(127);     \/* exec error *\/\r\n    } else {                        \/* parent *\/\r\n       while (waitpid(pid, &amp;status, 0) &lt; 0)\r\n           if (errno != EINTR) {\r\n               status = -1; \/* error other than EINTR from waitpid() *\/\r\n               break;\r\n           }\r\n    }\r\n\r\n    \/* restore previous signal actions &amp; reset signal mask *\/\r\n    if (sigaction(SIGINT, &amp;saveintr, NULL) &lt; 0)\r\n        return(-1);\r\n    if (sigaction(SIGQUIT, &amp;savequit, NULL) &lt; 0)\r\n        return(-1);\r\n    if (sigprocmask(SIG_SETMASK, &amp;savemask, NULL) &lt; 0)\r\n        return(-1);\r\n\r\n    return(status);\r\n}\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2><span style=\"color: #993300;\">system<\/span>\u7684\u8fd4\u56de\u503c<\/h2>\n<p>\u8981\u77e5\u9053system\u7684\u8fd4\u56de\u503c\uff0c\u5b83\u662fshell\u7684\u7ec8\u6b62\u72b6\u6001\uff0c\u5e76\u4e0d\u4e00\u76f4\u662f\u547d\u4ee4\u7684\u7ec8\u6b62\u72b6\u6001\u3002\u6211\u4eec\u5df2\u7ecf\u5728Figure8.23\u770b\u4e86\u4e00\u4e9b\u4f8b\u5b50\uff0c\u7ed3\u679c\u662f\u6211\u4eec\u6240\u671f\u671b\u7684\u90a3\u6837\uff1a\u5982\u679c\u6211\u4eec\u6267\u884c\u4e00\u4e2a\u7b80\u5355\u547d\u4ee4\uff0c\u7c7b\u4f3c<span style=\"color: #993300;\">date<\/span>\uff0c\u7ec8\u6b62\u72b6\u6001\u662f0\u3002\u6267\u884cshell\u547d\u4ee4<span style=\"color: #993300;\">exit 44<\/span>\u7ed9\u6211\u4eec\u4e00\u4e2a\u7ec8\u6b62\u72b6\u600144\u3002\u4f7f\u7528\u4e86\u4fe1\u53f7\u4f1a\u53d1\u751f\u4ec0\u4e48\u5462\uff1f<\/p>\n<p>\u8ba9\u6211\u4eec\u8fd0\u884cFigure8.24\u4e2d\u7684\u7a0b\u5e8f\uff0c\u5e76\u53d1\u9001\u4e00\u4e9b\u4fe1\u53f7\u7ed9\u6b63\u5728\u6267\u884c\u7684\u547d\u4ee4\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;\">   $ <span class=\"docEmphStrong\">tsys \"sleep 30\"<\/span>\r\n<span class=\"docEmphStrong\">\r\n   ^?<\/span>normal termination, exit status = 130    <span class=\"docEmphItalicAlt\">we type the interrupt key<\/span>\r\n   $ <span class=\"docEmphStrong\">tsys \"sleep 30\"<\/span>\r\n<span class=\"docEmphStrong\">\r\n   ^\\<\/span>sh: 946 Quit                             <span class=\"docEmphItalicAlt\">we type the quit key<\/span>\r\n   normal termination, exit status = 131\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u4e2d\u65ad\u4fe1\u53f7\u7ec8\u6b62<span style=\"color: #993300;\">sleep<\/span>\u51fd\u6570\uff0c<span style=\"color: #993300;\">pr_exit<\/span>\u51fd\u6570\uff08FIGURE8.5\uff09\u4f1a\u8ba4\u4e3a\u5b83\u662f\u88ab\u6b63\u5e38\u7ec8\u6b62\u3002\u540c\u6837\u7684\u4e8b\u60c5\u4f1a\u53d1\u751f\u5728\u5f53\u6211\u4eec\u4f7f\u7528\u9000\u51fa\u952e\u5173\u95ed<span style=\"color: #993300;\">sleep<\/span>\u65f6\u3002\u5f53\u547d\u4ee4\u88ab\u4fe1\u53f7\u7ec8\u6b62\u5b83\u7684\u7ed3\u675f\u72b6\u6001\u662f128\u52a0\u4e0a\u4fe1\u53f7\u53f7\u65f6\uff0c\u5728\u6587\u6863\u8d2b\u7620\u7684Bourne Shell\u4e0a\u4f1a\u53d1\u751f\u4ec0\u4e48\uff1f<\/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\">sh<\/span>                             <span class=\"docEmphItalicAlt\">make sure we're running the Bourne shell<\/span>\r\n   $ <span class=\"docEmphStrong\">sh -c \"sleep 30\"<\/span>\r\n<span class=\"docEmphStrong\">\r\n   ^?<\/span>                               <span class=\"docEmphItalicAlt\">type the interrupt key<\/span>\r\n   $ <span class=\"docEmphStrong\">echo $?<\/span>                        <span class=\"docEmphItalicAlt\">print termination status of last command<\/span>\r\n   130\r\n   $ <span class=\"docEmphStrong\">sh -c \"sleep 30\"<\/span>\r\n<span class=\"docEmphStrong\">\r\n   ^\\<\/span>sh: 962 Quit - core dumped     <span class=\"docEmphItalicAlt\">type the quit key<\/span>\r\n   $ <span class=\"docEmphStrong\">echo $?<\/span>                        <span class=\"docEmphItalicAlt\">print termination status of last command<\/span>\r\n   131\r\n   $ <span class=\"docEmphStrong\">exit<\/span>                           <span class=\"docEmphItalicAlt\">leave Bourne shell<\/span>\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5728\u8be5\u7cfb\u7edf\u4e0a\u4f7f\u7528<span style=\"color: #993300;\">SIGINT<\/span>\u7684\u503c\u662f2\uff0c<span style=\"color: #993300;\">SIGQUIT<\/span>\u7684\u503c\u662f3\uff0c\u6240\u4ee5\u7ed9\u6211\u4eecshell\u7684\u7ec8\u6b62\u72b6\u6001\u662f130\u548c131\u3002<\/p>\n<p>\u8ba9\u6211\u4eec\u8bd5\u4e00\u4e0b\u7c7b\u4f3c\u7684\u4f8b\u5b50\uff0c\u4f46\u662f\u8fd9\u6b21\u6211\u4eec\u5c06\u76f4\u63a5\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7\u5230shell\u5e76\u4e14\u770b<span style=\"color: #993300;\">system<\/span>\u8fd4\u56de\u4e86\u4ec0\u4e48\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;\">$ <span class=\"docEmphStrong\">tsys \"sleep 30\" &amp;<\/span>                 <span class=\"docEmphItalicAlt\">start it in background this time<\/span>\r\n    9257\r\n    $ <span class=\"docEmphStrong\">ps -f<\/span>                             <span class=\"docEmphItalicAlt\">look at the process IDs<\/span>\r\n         UID   PID   PPID   TTY    TIME CMD\r\n         sar  9260    949   pts\/5  0:00 ps -f\r\n         sar  9258   9257   pts\/5  0:00 sh -c sleep 60\r\n         sar   949    947   pts\/5  0:01 \/bin\/sh\r\n         sar  9257    949   pts\/5  0:00 tsys sleep 60\r\n         sar  9259   9258   pts\/5  0:00 sleep 60\r\n    $ <span class=\"docEmphStrong\">kill -KILL 9258<\/span>                   <span class=\"docEmphItalicAlt\">kill the shell itself<\/span>\r\n    abnormal termination, signal number = 9\r\n<\/span><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u91cc\u6211\u4eec\u80fd\u770b\u5230<span style=\"color: #993300;\">system<\/span>\u7684\u8fd4\u56de\u503c\uff0c\u4ec5\u5f53shell\u5b83\u81ea\u5df1\u4e0d\u6b63\u5e38\u7ec8\u6b62\u65f6\u624d\u4f1a\u62a5\u544a\u4e86\u4e0d\u6b63\u5e38\u7ec8\u6b62\u7684\u63d0\u793a\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528system\u51fd\u6570\u5199\u4e00\u4e2a\u7a0b\u5e8f\u65f6\uff0c\u5148\u786e\u5b9a\u6b63\u786e\u7684\u8fd4\u56de\u503c\u3002\u5982\u679c\u4f60\u8c03\u7528\u4e86<span style=\"color: #993300;\">fork<\/span>\uff0c<span style=\"color: #993300;\">exec<\/span>\u6216<span style=\"color: #993300;\">wait<\/span>\uff0c\u7ec8\u6b62\u72b6\u6001\u4e0d\u540c\u4e8e\u4f60\u8c03\u7528\u7684<span style=\"color: #993300;\">system<\/span>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728APUE8.13\u8282\uff0c\u6211\u4eec\u663e\u793a\u4e86\u4e00\u4e2asystem\u51fd\u6570\u7684\u5b9e\u73b0\u3002\u90a3\u4e2a\u7248\u672c\u4e2d\u5e76\u4e0d\u5904\u7406\u4efb\u4f55\u4fe1\u53f7\u3002POSIX.1\u8981\u6c42system\u5ffd\u7565SIGINT\u548cSIGQUIT\u5e76\u4e14\u963b\u585eS&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.5x44.cn\/?p=716\"> 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-716","post","type-post","status-publish","format-standard","hentry","category-apue"],"_links":{"self":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/716","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=716"}],"version-history":[{"count":8,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":736,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/736"}],"wp:attachment":[{"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.5x44.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}