{"id":1635,"date":"2015-11-12T12:27:02","date_gmt":"2015-11-12T04:27:02","guid":{"rendered":"http:\/\/www.51cos.com\/?p=1635"},"modified":"2015-11-12T12:27:02","modified_gmt":"2015-11-12T04:27:02","slug":"linux%e4%b8%8b%e4%bd%bf%e7%94%a8system%e5%87%bd%e6%95%b0%e4%b8%80%e5%ae%9a%e8%a6%81%e8%b0%a8%e6%85%8elinux%e4%b8%8bsystem%e5%87%bd%e6%95%b0%e5%bc%95%e5%8f%91%e7%9a%84%e9%94%99%e8%af%af","status":"publish","type":"post","link":"http:\/\/www.51cos.com\/?p=1635","title":{"rendered":"Linux\u4e0b\u4f7f\u7528system()\u51fd\u6570\u4e00\u5b9a\u8981\u8c28\u614e,Linux\u4e0bsystem()\u51fd\u6570\u5f15\u53d1\u7684\u9519\u8bef"},"content":{"rendered":"<p> \u66fe\u7ecf\u7684\u66fe\u7ecf\uff0c\u88absystem()\u51fd\u6570\u6298\u78e8\u8fc7\uff0c\u4e4b\u6240\u4ee5\u8fd9\u6837\uff0c\u662f\u56e0\u4e3a\u5bf9system()\u51fd\u6570\u4e86\u89e3\u4e0d\u591f\u6df1\u5165\u3002\u53ea\u662f\u7b80\u5355\u7684\u77e5\u9053\u7528\u8fd9\u4e2a\u51fd\u6570\u6267\u884c\u4e00\u4e2a\u7cfb\u7edf\u547d\u4ee4\uff0c\u8fd9\u8fdc\u8fdc\u4e0d\u591f\uff0c\u5b83\u7684\u8fd4\u56de\u503c\u3001\u5b83\u6240\u6267\u884c\u547d\u4ee4\u7684\u8fd4\u56de\u503c\u4ee5\u53ca\u547d\u4ee4\u6267\u884c\u5931\u8d25\u539f\u56e0\u5982\u4f55\u5b9a\u4f4d\uff0c\u8fd9\u624d\u662f\u91cd\u70b9\u3002\u5f53\u521d\u56e0\u4e3a\u8fd9\u4e2a\u51fd\u6570\u98ce\u9669\u8f83\u591a\uff0c\u6545\u629b\u5f03\u4e0d\u7528\uff0c\u6539\u7528\u5176\u4ed6\u7684\u65b9\u6cd5\u3002\u8fd9\u91cc\u5148\u4e0d\u8bf4\u6211\u7528\u4e86\u4ec0\u4e48\u65b9\u6cd5\uff0c\u8fd9\u91cc\u5fc5\u987b\u8981\u641e\u61c2system()\u51fd\u6570\uff0c\u56e0\u4e3a\u8fd8\u662f\u6709\u5f88\u591a\u4eba\u7528\u4e86system()\u51fd\u6570\uff0c\u6709\u65f6\u4f60\u4e0d\u5f97\u4e0d\u9762\u5bf9\u5b83\u3002<\/p>\n<p>\u5148\u6765\u770b\u4e00\u4e0bsystem()\u51fd\u6570\u7684\u7b80\u5355\u4ecb\u7ecd\uff1a<\/p>\n<p>#include <stdlib.h><br \/>\nint system(const char *command);<\/p>\n<p>system() executes a command specified in command by calling \/bin\/sh -c command, and returns after the command has been completed. During execution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT will be ignored.<br \/>\nsystem()\u51fd\u6570\u8c03\u7528\/bin\/sh\u6765\u6267\u884c\u53c2\u6570\u6307\u5b9a\u7684\u547d\u4ee4\uff0c\/bin\/sh \u4e00\u822c\u662f\u4e00\u4e2a\u8f6f\u8fde\u63a5\uff0c\u6307\u5411\u67d0\u4e2a\u5177\u4f53\u7684shell\uff0c\u6bd4\u5982bash\uff0c-c\u9009\u9879\u662f\u544a\u8bc9shell\u4ece\u5b57\u7b26\u4e32command\u4e2d\u8bfb\u53d6\u547d\u4ee4\uff1b<br \/>\n\u5728\u8be5command\u6267\u884c\u671f\u95f4\uff0cSIGCHLD\u662f\u88ab\u963b\u585e\u7684\uff0c\u597d\u6bd4\u5728\u8bf4\uff1ahi\uff0c\u5185\u6838\uff0c\u8fd9\u4f1a\u4e0d\u8981\u7ed9\u6211\u9001SIGCHLD\u4fe1\u53f7\uff0c\u7b49\u6211\u5fd9\u5b8c\u518d\u8bf4\uff1b<br \/>\n\u5728\u8be5command\u6267\u884c\u671f\u95f4\uff0cSIGINT\u548cSIGQUIT\u662f\u88ab\u5ffd\u7565\u7684\uff0c\u610f\u601d\u662f\u8fdb\u7a0b\u6536\u5230\u8fd9\u4e24\u4e2a\u4fe1\u53f7\u540e\u6ca1\u6709\u4efb\u4f55\u52a8\u4f5c\u3002 <\/p>\n<p> \u518d\u6765\u770b\u4e00\u4e0bsystem()\u51fd\u6570\u8fd4\u56de\u503c\uff1a<br \/>\nThe value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise. This latter return status is in the format specified in wait(2). Thus, the exit code of the command will be WEXITSTATUS(status). In case \/bin\/sh could not be executed, the exit status will be that of a command that does exit(127).<br \/>\nIf the value of command is NULL, system() returns nonzero if the shell is available, and zero if not.<br \/>\n\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3system()\u51fd\u6570\u8fd4\u56de\u503c\uff0c\u9700\u8981\u4e86\u89e3\u5176\u6267\u884c\u8fc7\u7a0b\uff0c\u5b9e\u9645\u4e0asystem()\u51fd\u6570\u6267\u884c\u4e86\u4e09\u6b65\u64cd\u4f5c\uff1a<br \/>\n1.fork\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff1b<br \/>\n2.\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528exec\u51fd\u6570\u53bb\u6267\u884ccommand\uff1b<br \/>\n3.\u5728\u7236\u8fdb\u7a0b\u4e2d\u8c03\u7528wait\u53bb\u7b49\u5f85\u5b50\u8fdb\u7a0b\u7ed3\u675f\u3002<br \/>\n\u5bf9\u4e8efork\u5931\u8d25\uff0csystem()\u51fd\u6570\u8fd4\u56de-1\u3002<br \/>\n\u5982\u679cexec\u6267\u884c\u6210\u529f\uff0c\u4e5f\u5373command\u987a\u5229\u6267\u884c\u5b8c\u6bd5\uff0c\u5219\u8fd4\u56decommand\u901a\u8fc7exit\u6216return\u8fd4\u56de\u7684\u503c\u3002<br \/>\n\uff08\u6ce8\u610f\uff0ccommand\u987a\u5229\u6267\u884c\u4e0d\u4ee3\u8868\u6267\u884c\u6210\u529f\uff0c\u6bd4\u5982command\uff1a&#8221;rm debuglog.txt&#8221;\uff0c\u4e0d\u7ba1\u6587\u4ef6\u5b58\u4e0d\u5b58\u5728\u8be5command\u90fd\u987a\u5229\u6267\u884c\u4e86\uff09<br \/>\n\u5982\u679cexec\u6267\u884c\u5931\u8d25\uff0c\u4e5f\u5373command\u6ca1\u6709\u987a\u5229\u6267\u884c\uff0c\u6bd4\u5982\u88ab\u4fe1\u53f7\u4e2d\u65ad\uff0c\u6216\u8005command\u547d\u4ee4\u6839\u672c\u4e0d\u5b58\u5728\uff0csystem()\u51fd\u6570\u8fd4\u56de127.<br \/>\n\u5982\u679ccommand\u4e3aNULL\uff0c\u5219system()\u51fd\u6570\u8fd4\u56de\u975e0\u503c\uff0c\u4e00\u822c\u4e3a1.<\/p>\n<p>\u770b\u4e00\u4e0bsystem()\u51fd\u6570\u7684\u6e90\u7801<br \/>\n\u770b\u5b8c\u8fd9\u4e9b\uff0c\u6211\u60f3\u80af\u5b9a\u6709\u4eba\u5bf9system()\u51fd\u6570\u8fd4\u56de\u503c\u8fd8\u662f\u4e0d\u6e05\u695a\uff0c\u770b\u6e90\u7801\u6700\u6e05\u695a\uff0c\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2asystem()\u51fd\u6570\u7684\u5b9e\u73b0\uff1a <\/p>\n<pre class=\"prettyprint linenums\" >\nint system(const char * cmdstring)<br \/>\n{<br \/>\n    pid_t pid;<br \/>\n    int status;<\/p>\n<p>if(cmdstring == NULL)<br \/>\n{<br \/>\n    return (1); \/\/\u5982\u679ccmdstring\u4e3a\u7a7a\uff0c\u8fd4\u56de\u975e\u96f6\u503c\uff0c\u4e00\u822c\u4e3a1<br \/>\n}<\/p>\n<p>if((pid = fork())<0)\n{\n    status = -1; \/\/fork\u5931\u8d25\uff0c\u8fd4\u56de-1\n}\nelse if(pid == 0)\n{\n    execl(\"\/bin\/sh\", \"sh\", \"-c\", cmdstring, (char *)0);\n    _exit(127); \/\/ exec\u6267\u884c\u5931\u8d25\u8fd4\u56de127\uff0c\u6ce8\u610fexec\u53ea\u5728\u5931\u8d25\u65f6\u624d\u8fd4\u56de\u73b0\u5728\u7684\u8fdb\u7a0b\uff0c\u6210\u529f\u7684\u8bdd\u73b0\u5728\u7684\u8fdb\u7a0b\u5c31\u4e0d\u5b58\u5728\u5566~~\n}\nelse \/\/\u7236\u8fdb\u7a0b\n{\n    while(waitpid(pid, &#038;status, 0) < 0)\n    {\n        if(errno != EINTR)\n        {\n            status = -1; \/\/\u5982\u679cwaitpid\u88ab\u4fe1\u53f7\u4e2d\u65ad\uff0c\u5219\u8fd4\u56de-1\n            break;\n        }\n    }\n}\n \n    return status; \/\/\u5982\u679cwaitpid\u6210\u529f\uff0c\u5219\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684\u8fd4\u56de\u72b6\u6001\n}\n \n\n\u4ed4\u7ec6\u770b\u5b8c\u8fd9\u4e2asystem()\u51fd\u6570\u7684\u7b80\u5355\u5b9e\u73b0\uff0c\u90a3\u4e48\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u5c31\u6e05\u6670\u4e86\u5427\uff0c\u90a3\u4e48\u4ec0\u4e48\u65f6\u5019system()\u51fd\u6570\u8fd4\u56de0\u5462\uff1f\u53ea\u5728command\u547d\u4ee4\u8fd4\u56de0\u65f6\u3002\n\n\u770b\u4e00\u4e0b\u8be5\u600e\u4e48\u76d1\u63a7system()\u51fd\u6570\u6267\u884c\u72b6\u6001\n\u8fd9\u91cc\u7ed9\u6211\u51fa\u7684\u505a\u6cd5\uff1a \n\n\n\n\n<pre class=\"prettyprint linenums\" >\nint status;<br \/>\nif(NULL == cmdstring) \/\/\u5982\u679ccmdstring\u4e3a\u7a7a\u8d81\u65e9\u95ea\u9000\u5427\uff0c\u5c3d\u7ba1system()\u51fd\u6570\u4e5f\u80fd\u5904\u7406\u7a7a\u6307\u9488<br \/>\n{<br \/>\n    return XXX;<br \/>\n}<br \/>\nstatus = system(cmdstring);<br \/>\nif(status < 0)\n{\n    printf(\"cmd: %s\\t error: %s\", cmdstring, strerror(errno)); \/\/ \u8fd9\u91cc\u52a1\u5fc5\u8981\u628aerrno\u4fe1\u606f\u8f93\u51fa\u6216\u8bb0\u5165Log\n    return XXX;\n}\n \nif(WIFEXITED(status))\n{\n    printf(\"normal termination, exit status = %d\\n\", WEXITSTATUS(status)); \/\/\u53d6\u5f97cmdstring\u6267\u884c\u7ed3\u679c\n}\nelse if(WIFSIGNALED(status))\n{\n    printf(\"abnormal termination,signal number =%d\\n\", WTERMSIG(status)); \/\/\u5982\u679ccmdstring\u88ab\u4fe1\u53f7\u4e2d\u65ad\uff0c\u53d6\u5f97\u4fe1\u53f7\u503c\n}\nelse if(WIFSTOPPED(status))\n{\n    printf(\"process stopped, signal number =%d\\n\", WSTOPSIG(status)); \/\/\u5982\u679ccmdstring\u88ab\u4fe1\u53f7\u6682\u505c\u6267\u884c\uff0c\u53d6\u5f97\u4fe1\u53f7\u503c\n}\n \n\nsystem()\u51fd\u6570\u7528\u8d77\u6765\u5f88\u5bb9\u6613\u51fa\u9519\uff0c\u8fd4\u56de\u503c\u592a\u591a\uff0c\u800c\u4e14\u8fd4\u56de\u503c\u5f88\u5bb9\u6613\u8ddfcommand\u7684\u8fd4\u56de\u503c\u6df7\u6dc6\u3002\u8fd9\u91cc\u63a8\u8350\u4f7f\u7528popen()\u51fd\u6570\u66ff\u4ee3\uff0c\u5173\u4e8epopen()\u51fd\u6570\u7684\u7b80\u5355\u4f7f\u7528\u4e5f\u53ef\u4ee5\u901a\u8fc7\u4e0a\u9762\u7684\u94fe\u63a5\u67e5\u770b\u3002\n\npopen()\u51fd\u6570\u8f83\u4e8esystem()\u51fd\u6570\u7684\u4f18\u52bf\u5728\u4e8e\u4f7f\u7528\u7b80\u5355\uff0cpopen()\u51fd\u6570\u53ea\u8fd4\u56de\u4e24\u4e2a\u503c\uff1a\n\u6210\u529f\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684status\uff0c\u4f7f\u7528WIFEXITED\u76f8\u5173\u5b8f\u5c31\u53ef\u4ee5\u53d6\u5f97command\u7684\u8fd4\u56de\u7ed3\u679c\uff1b\n\u5931\u8d25\u8fd4\u56de-1\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528perro()\u51fd\u6570\u6216strerror()\u51fd\u6570\u5f97\u5230\u6709\u7528\u7684\u9519\u8bef\u4fe1\u606f\u3002\n\n\u8fd9\u7bc7\u6587\u7ae0\u53ea\u6d89\u53ca\u4e86system()\u51fd\u6570\u7684\u7b80\u5355\u4f7f\u7528\uff0c\u8fd8\u6ca1\u6709\u8c08\u53caSIGCHLD\u3001SIGINT\u548cSIGQUIT\u5bf9system()\u51fd\u6570\u7684\u5f71\u54cd\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u4e4b\u6240\u4ee5\u4eca\u5929\u5199\u8fd9\u7bc7\u6587\u7ae0\uff0c\u662f\u56e0\u4e3a\u9879\u76ee\u4e2d\u56e0\u6709\u4eba\u4f7f\u7528\u4e86system()\u51fd\u6570\u800c\u9020\u6210\u4e86\u5f88\u4e25\u91cd\u7684\u4e8b\u6545\u3002\u73b0\u50cf\u662fsystem()\u51fd\u6570\u6267\u884c\u65f6\u4f1a\u4ea7\u751f\u4e00\u4e2a\u9519\u8bef\uff1a\u201cNo child processes\u201d\u3002\n\n\n\n\n\u4eca\u5929\uff0c\u4e00\u4e2a\u8fd0\u884c\u4e86\u8fd1\u4e00\u5e74\u7684\u7a0b\u5e8f\u7a81\u7136\u6302\u6389\u4e86\uff0c\u95ee\u9898\u5b9a\u4f4d\u5230\u662fsystem()\u51fd\u6570\u51fa\u7684\u95ee\u9898\uff0c\u5148\u770b\u4e00\u4e0b\u95ee\u9898\n\n\u7b80\u5355\u5c01\u88c5\u4e86\u4e00\u4e0bsystem()\u51fd\u6570\uff1a \n\n\n\n\n<pre class=\"prettyprint linenums\" >\nint pox_system(const char *cmd_line)<br \/>\n{<br \/>\n    return system(cmd_line);<br \/>\n}<\/p>\n<p>int ret = 0;<br \/>\nret = pox_system(\"gzip -c \/var\/opt\/I00005.xml > \/var\/opt\/I00005.z\");<br \/>\nif(0 != ret)<br \/>\n{<br \/>\n    Log(\"zip file failed\\n\");<br \/>\n}<\/p>\n<p>\u95ee\u9898\u73b0\u8c61\uff1a\u6bcf\u6b21\u6267\u884c\u5230\u6b64\u5904\uff0c\u90fd\u4f1azip failed\u3002\u800c\u5355\u72ec\u628a\u8be5\u547d\u4ee4\u62ff\u51fa\u6765\u5728shell\u91cc\u6267\u884c\u5374\u603b\u662f\u5bf9\u7684\uff0c\u4e8b\u5b9e\u4e0a\u8be5\u6bb5\u4ee3\u7801\u5df2\u8fd0\u884c\u4e86\u5f88\u957f\u65f6\u95f4\uff0c\u4ece\u6ca1\u51fa\u8fc7\u95ee\u9898\u3002<\/p>\n<p>\u7cdf\u7cd5\u7684\u65e5\u5fd7<\/p>\n<p>\u5206\u6790log\u65f6\uff0c\u6211\u4eec\u53ea\u80fd\u770b\u5230\u201czip file failed\u201d\u8fd9\u4e2a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u4fe1\u606f\uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48fail\uff0c\u6beb\u65e0\u7ebf\u7d22\u3002<br \/>\n\u90a3\u597d\uff0c\u6211\u4eec\u5148\u8bd5\u7740\u627e\u51fa\u66f4\u591a\u7684\u7ebf\u7d22\uff1a <\/p>\n<pre class=\"prettyprint linenums\" >\nint ret = 0;<br \/>\nret = pox_system(\"gzip -c \/var\/opt\/I00005.xml > \/var\/opt\/I00005.z\");<br \/>\nif(0 != ret)<br \/>\n{<br \/>\n    Log(\"zip file failed: %s\\n\", strerror(errno)); \/\/\u5c1d\u8bd5\u6253\u5370\u51fa\u7cfb\u7edf\u9519\u8bef\u4fe1\u606f<br \/>\n}<\/p>\n<p>\u6211\u4eec\u589e\u52a0\u4e86log\uff0c\u901a\u8fc7system()\u51fd\u6570\u8bbe\u7f6e\u7684errno\uff0c\u6211\u4eec\u5f97\u5230\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u7ebf\u7d22\uff1asystem()\u51fd\u6570\u5931\u8d25\u662f\u7531\u4e8e\u201c No child processes\u201d\u3002\u7ee7\u7eed\u627eRoot Cause\u3002<\/p>\n<p>\u8c01\u52a8\u4e86errno<\/p>\n<p>\u6211\u4eec\u901a\u8fc7\u4e0a\u9762\u7684\u7ebf\u7d22\uff0c\u77e5\u9053system()\u51fd\u6570\u8bbe\u7f6e\u4e86errno\u4e3aECHILD\uff0c\u7136\u800c\u4ecesystem()\u51fd\u6570\u7684man\u624b\u518c\u91cc\u6211\u4eec\u627e\u4e0d\u5230\u4efb\u4f55\u6709\u5173EHILD\u7684\u4fe1\u606f\u3002\u6211\u4eec\u77e5\u9053system()\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u4e3a\uff1afork()->exec()->waitpid()\u3002\u5f88\u663e\u7136waitpid()\u6709\u91cd\u5927\u5acc\u7591\uff0c\u6211\u4eec\u53bb\u67e5\u4e00\u4e0bman\u624b\u518c\uff0c\u770b\u8be5\u51fd\u6570\u6709\u6ca1\u6709\u53ef\u80fd\u8bbe\u7f6eECHILD\uff1a<br \/>\nECHILD<br \/>\n(for waitpid() or waitid()) The process specified by pid (waitpid()) or idtype and id (waitid()) does not exist or is not a child of the calling process. (This can happen for one's own child if the action for SIGCHLD is set to SIG_IGN. See also the Linux Notes section about threads.)<br \/>\n\u679c\u7136\u6709\u6599\uff0c\u5982\u679cSIGCHLD\u4fe1\u53f7\u884c\u4e3a\u88ab\u8bbe\u7f6e\u4e3aSIG_IGN\u65f6\uff0cwaitpid()\u51fd\u6570\u6709\u53ef\u80fd\u56e0\u4e3a\u627e\u4e0d\u5230\u5b50\u8fdb\u7a0b\u800c\u62a5ECHILD\u9519\u8bef\u3002\u4f3c\u4e4e\u6211\u4eec\u627e\u5230\u4e86\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\uff1a\u5728\u8c03\u7528system()\u51fd\u6570\u524d\u91cd\u65b0\u8bbe\u7f6eSIGCHLD\u4fe1\u53f7\u4e3a\u7f3a\u7701\u503c\uff0c\u5373signal(SIGCHLD, SIG_DFL)\u3002\u6211\u4eec\u5f88\u5174\u594b\uff0c\u6682\u65f6\u987e\u4e0d\u4e0a\u770bLinux Notes\u90e8\u5206\uff0c\u76f4\u63a5\u52a0\u4e0a\u4ee3\u7801\u6d4b\u8bd5\uff01\u4e56\u4e56\uff0c\u95ee\u9898\u89e3\u51b3\u4e86\uff01<\/p>\n<p>\u5982\u6b64\u5904\u7406\u95ee\u9898\u662f\u4f60\u7684\u98ce\u683c\u5417<\/p>\n<p>\u6b63\u5f53\u6211\u4eec\u6025\u4e8echeck in \u4ee3\u7801\u65f6\uff0c\u4e00\u4e2a\u7591\u95ee\u51fa\u73b0\u4e86\uff1a\u201c\u8fd9\u4e2a\u9519\u8bef\u4e3a\u4ec0\u4e48\u4ee5\u524d\u6ca1\u53d1\u751f\u201d\uff1f\u662f\u554a\uff0c\u8fd0\u884c\u826f\u597d\u7684\u7a0b\u5e8f\u600e\u4e48\u7a81\u7136\u5c31\u6302\u4e86\u5462\uff1f\u9996\u5148\u6211\u4eec\u4ee3\u7801\u6ca1\u6709\u6539\u52a8\uff0c\u90a3\u4e48\u80af\u5b9a\u662f\u5916\u90e8\u56e0\u7d20\u4e86\u3002\u4e00\u60f3\u5230\u5916\u90e8\u56e0\u7d20\uff0c\u6211\u4eec\u5f00\u59cb\u62b1\u6028\uff1a\u201c\u80af\u5b9a\u662f\u5176\u4ed6\u7ec4\u7684\u7a0b\u5e8f\u5f71\u54cd\u6211\u4eec\u4e86\uff01\u201d\u4f46\u62b1\u6028\u8fd9\u662f\u6ca1\u7528\u7684\uff0c\u5982\u679c\u4f60\u8fd9\u4e48\u8ba4\u4e3a\uff0c\u90a3\u4e48\u8bf7\u62ff\u51fa\u8bc1\u636e\uff01\u4f46\u9759\u4e0b\u6765\u5206\u6790\u4e00\u4e0b\u4e0d\u96be\u53d1\u73b0\uff0c\u8fd9\u4e0d\u53ef\u80fd\u662f\u5176\u4ed6\u7a0b\u5e8f\u7684\u5f71\u54cd\uff0c\u5176\u4ed6\u8fdb\u7a0b\u4e0d\u53ef\u80fd\u5f71\u54cd\u6211\u4eec\u8fdb\u7a0b\u5bf9\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<p>system()\u51fd\u6570\u4e4b\u524d\u6ca1\u51fa\u9519\uff0c\u662f\u56e0\u4e3asysteme()\u51fd\u6570\u4f9d\u8d56\u4e86\u7cfb\u7edf\u7684\u4e00\u4e2a\u7279\u6027\uff0c\u90a3\u5c31\u662f\u5185\u6838\u521d\u59cb\u5316\u8fdb\u7a0b\u65f6\u5bf9SIGCHLD\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u4e3aSIG_DFL\uff0c\u8fd9\u662f\u4ec0\u4e48\u4ec0\u4e48\u610f\u601d\u5462\uff1f\u5373\u5185\u6838\u53d1\u73b0\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\u7ec8\u6b62\u540e\u7ed9\u8fdb\u7a0b\u53d1\u9001\u4e00\u4e2aSIGCHLD\u4fe1\u53f7\uff0c\u8fdb\u7a0b\u6536\u5230\u8be5\u4fe1\u53f7\u540e\u91c7\u7528SIG_DFL\u65b9\u5f0f\u5904\u7406\uff0c\u90a3\u4e48SIG_DFL\u53c8\u662f\u4ec0\u4e48\u65b9\u5f0f\u5462\uff1fSIG_DFL\u662f\u4e00\u4e2a\u5b8f\uff0c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u6307\u9488\uff0c\u4e8b\u5b9e\u4e0a\u8be5\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u4ec0\u4e48\u4e5f\u6ca1\u505a\u3002\u8fd9\u4e2a\u7279\u6027\u6b63\u662fsystem()\u51fd\u6570\u9700\u8981\u7684\uff0csystem()\u51fd\u6570\u9996\u5148fork()\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u6267\u884ccommand\u547d\u4ee4\uff0c\u6267\u884c\u5b8c\u540esystem()\u51fd\u6570\u4f1a\u4f7f\u7528waitpid()\u51fd\u6570\u5bf9\u5b50\u8fdb\u7a0b\u8fdb\u884c\u6536\u5c38\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u6e05\u9192\u7684\u5f97\u77e5\uff0csystem()\u6267\u884c\u524d\uff0cSIGCHLD\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f\u80af\u5b9a\u53d8\u4e86\uff0c\u4e0d\u518d\u662fSIG_DFL\u4e86\uff0c\u81f3\u4e8e\u53d8\u6210\u4ec0\u4e48\u6682\u65f6\u4e0d\u77e5\u9053\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u6211\u4eec\u4e5f\u4e0d\u9700\u8981\u77e5\u9053\uff0c\u6211\u4eec\u53ea\u9700\u8981\u8bb0\u5f97\u4f7f\u7528system()\u51fd\u6570\u524d\u628aSIGCHLD\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u663e\u5f0f\u4fee\u6539\u4e3aSIG_DFL\u65b9\u5f0f\uff0c\u540c\u65f6\u8bb0\u5f55\u539f\u6765\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u4f7f\u7528\u5b8csystem()\u540e\u518d\u8bbe\u4e3a\u539f\u6765\u7684\u5904\u7406\u65b9\u5f0f\u3002\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u5c4f\u853d\u56e0\u7cfb\u7edf\u5347\u7ea7\u6216\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u6539\u53d8\u5e26\u6765\u7684\u5f71\u54cd\u3002<\/p>\n<p>\u9a8c\u8bc1\u731c\u60f3 <\/p>\n<p>\u6211\u4eec\u516c\u53f8\u91c7\u7528\u7684\u662f\u6301\u7eed\u96c6\u6210+\u654f\u6377\u5f00\u53d1\u6a21\u5f0f\uff0c\u6bcf\u5929\u90fd\u4f1a\u7531\u4e13\u95e8\u7684team\u8d1f\u8d23\u81ea\u52a8\u5316case\u7684\u6d4b\u8bd5\uff0c\u6bcf\u6b21\u79f0\u4e3a\u4e00\u4e2abuild\uff0c\u6211\u4eec\u5206\u6790\u4e86\u672c\u6b21build\u4e0e\u4e0a\u6b21build\u4f7f\u7528\u7684\u7cfb\u7edf\u7248\u672c\uff0c\u53d1\u73b0\u7248\u672c\u786e\u5b9e\u5347\u7ea7\u4e86\u3002\u4e8e\u662f\u6211\u4eec\u627e\u5230\u4e86\u76f8\u5173team\u8fdb\u884c\u9a8c\u8bc1\uff0c\u6211\u4eec\u628a\u95ee\u9898\u8be6\u7ec6\u7684\u63cf\u8ff0\u4e86\u4e00\u4e0b\uff0c\u5f88\u5feb\u5bf9\u65b9\u7ed9\u4e86\u53cd\u9988\uff0c\u4e0b\u9762\u662f\u90ae\u4ef6\u56de\u590d\u539f\u6587\uff1a<\/p>\n<p>    LIBGEN \u91cc\u65b0\u589e\u52a0\u4e86SIGCHLD\u7684\u5904\u7406\u3002\u5c06\u5176ignore\u3002\u4e3a\u4e86\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u7684\u4ea7\u751f\u3002 <\/p>\n<p>\u770b\u6765\u6211\u4eec\u7684\u731c\u60f3\u6ca1\u9519\uff01\u95ee\u9898\u5206\u6790\u5230\u8fd9\u91cc\uff0c\u89e3\u51b3\u65b9\u6cd5\u4e5f\u6e05\u6670\u4e86\uff0c\u4e8e\u662f\u6211\u4eec\u4fee\u6539\u4e86\u6211\u4eec\u7684pox_system()\u51fd\u6570\uff1a <\/p>\n<pre class=\"prettyprint linenums\" >\ntypedef void (*sighandler_t)(int);<br \/>\nint pox_system(const char *cmd_line)<br \/>\n{<br \/>\n   int ret = 0;<br \/>\n   sighandler_t old_handler;<\/p>\n<p>   old_handler = signal(SIGCHLD, SIG_DFL);<br \/>\n   ret = system(cmd_line);<br \/>\n   signal(SIGCHLD, old_handler);<\/p>\n<p>   return ret;<br \/>\n}<\/p>\n<p> \u6211\u60f3\u8fd9\u662f\u8c03\u7528system()\u6bd4\u8f83\u5b8c\u7f8e\u7684\u89e3\u51b3\u65b9\u6848\u4e86\uff0c\u540c\u65f6\u4f7f\u7528pox_system()\u51fd\u6570\u5c01\u88c5\u5e26\u6765\u4e86\u975e\u5e38\u68d2\u7684\u6613\u7ef4\u62a4\u6027\uff0c\u6211\u4eec\u53ea\u9700\u8981\u4fee\u6539\u6b64\u5904\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u4ed6\u8c03\u7528\u5904\u90fd\u4e0d\u9700\u8981\u6539\u3002<\/p>\n<p>\u540e\u6765\uff0c\u67e5\u770b\u4e86\u5bf9\u65b9\u4fee\u6539\u7684\u4ee3\u7801\uff0c\u679c\u7136\u4ece\u4ee3\u7801\u4e0a\u627e\u5230\u4e86\u7b54\u6848\uff1a <\/p>\n<pre class=\"prettyprint linenums\" >\n\/* Ignore SIGCHLD to avoid zombie process *\/<br \/>\nif (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {<br \/>\n    return -1;<br \/>\n} else {<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5176\u4ed6\u601d\u8003<\/p>\n<p>\u6211\u4eec\u516c\u53f8\u7684\u4ee3\u7801\u4f7f\u7528SVN\u8fdb\u7a0b\u7ba1\u7406\u7684\uff0c\u5230\u76ee\u524d\u4e3a\u6b62\u6709\u5f88\u591abranch\uff0c\u9010\u6e10\u7684\uff0c\u51e0\u4e4e\u6bcf\u4e2abranch\u90fd\u51fa\u73b0\u4e86\u4e0a\u9762\u7684\u95ee\u9898\uff0c\u4e8e\u662f\u6211\u9010\u4e2a\u5728\u5404\u4e2abranchc\u4e0afix\u8fd9\u4e2a\u95ee\u9898\uff0c\u51e0\u4e4e\u5fd9\u4e86\u4e00\u5929\uff0c\u56e0\u4e3a\u6709\u7684branch\u5df2\u88ab\u9501\u5b9a\uff0c\u518d\u60f3merge\u4ee3\u7801\u5fc5\u987b\u627e\u76f8\u5173\u8d1f\u8d23\u4eba\u8bf4\u660e\u95ee\u9898\u7684\u4e25\u91cd\u6027\uff0c\u8fd8\u8981\u5728\u4e0d\u540c\u7684\u73af\u5883\u4e0a\u6d4b\u8bd5\uff0c\u6211\u8fb9\u505a\u8fd9\u4e9b\u8fb9\u60f3\uff0c\u7cfb\u7edf\u8fd9\u6837\u5347\u7ea7\u5408\u9002\u5417\uff1f<\/p>\n<p>\u9996\u5148\uff0c\u7531\u4e8e\u7cfb\u7edf\u7684\u5347\u7ea7\u5bfc\u81f4\u6211\u4eec\u7684\u4ee3\u7801\u5728\u6d4b\u8bd5\u65f6\u53d1\u73b0\u95ee\u9898\uff0c\u8fd9\u65f6\u518d\u6025\u5fd9\u53bbfix\uff0c\u9020\u6210\u4e86\u6211\u4eec\u7684\u88ab\u52a8\uff0c\u6211\u60f3\u8fd9\u662f\u4ed6\u4eec\u7684\u4e00\u4e2a\u5931\u8bef\u3002\u4f60\u505a\u7684\u5347\u7ea7\u5fc5\u987b\u8981\u8003\u8651\u5230\u5bf9\u5176\u4ed6team\u7684\u5f71\u54cd\u5427\uff1f\u4f55\u51b5\u4f60\u505a\u7684\u662f\u7cfb\u7edf\u5347\u7ea7\u3002\u5347\u7ea7\u524d\u9700\u8981\u505a\u4e2a\u98ce\u9669\u8bc4\u4f30\uff0c\u5bf9\u53ef\u80fd\u9020\u6210\u7684\u5f71\u54cd\u901a\u77e5\u5927\u5bb6\uff0c\u8fd9\u6837\u624d\u804c\u4e1a\u561b\u3002<\/p>\n<p>\u518d\u8005\uff0c\u636e\u4ed6\u4eec\u7684\u8bf4\u6cd5\uff0c\u4fee\u6539\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u662f\u4e3a\u4e86\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\uff0c\u5f53\u7136\u521d\u8877\u662f\u597d\u7684\uff0c\u4f46\u8fd9\u6837\u7684\u5347\u7ea7\u5f71\u54cd\u4e86\u4e00\u4e9b\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u5f0f\uff0c\u6bd4\u5982system()\u51fd\u6570\u3001wait()\u51fd\u6570\u3001waipid()\u3001fork()\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u90fd\u4e0e\u5b50\u8fdb\u7a0b\u6709\u5173\uff0c\u5982\u679c\u4f60\u5e0c\u671b\u4f7f\u7528wait()\u6216waitpid()\u5bf9\u5b50\u8fdb\u7a0b\u6536\u5c38\uff0c\u90a3\u4e48\u4f60\u5fc5\u987b\u4f7f\u7528\u4e0a\u9762\u4ecb\u7ecd\u7684\u65b9\u5f0f\uff1a\u5728\u8c03\u7528\u524d\uff08\u4e8b\u5b9e\u4e0a\u662ffork()\u524d\uff09\u5c06SIGCHLD\u4fe1\u53f7\u7f6e\u4e3aSIG_DFL\u5904\u7406\u65b9\u5f0f\uff0c\u8c03\u7528\u540e\uff08\u4e8b\u5b9e\u4e0await()\/waitpid()\u540e\uff09\u518d\u5c06\u4fe1\u53f7\u5904\u7406\u65b9\u5f0f\u8bbe\u7f6e\u4e3a\u4ece\u524d\u7684\u503c\u3002\u4f60\u7684\u7cfb\u7edf\u5347\u7ea7\uff0c\u5f3a\u5236\u5927\u5bb6\u5b8c\u5584\u4ee3\u7801\uff0c\u786e\u5b9e\u63d0\u9ad8\u4e86\u4ee3\u7801\u8d28\u91cf\uff0c\u4f46\u662f\u5bf9\u4e8e\u8fd9\u79cd\u5347\u7ea7\u6211\u4e0d\u662f\u5f88\u8ba4\u540c\uff0c\u8bd5\u60f3\u4e00\u4e0b\uff0c\u4f60\u89c1\u8fc7\u591a\u5c11fork()->waitpid()\u524d\u540e\u90fd\u8bbe\u7f6eSIGCHLD\u4fe1\u53f7\u7684\u4ee3\u7801\uff1f<\/p>\n<p>\u4f7f\u7528system()\u51fd\u6570\u7684\u5efa\u8bae<\/p>\n<p>\u4e0a\u5728\u7ed9\u51fa\u4e86\u8c03\u7528system()\u51fd\u6570\u7684\u6bd4\u8f83\u5b89\u5168\u7684\u7528\u6cd5\uff0c\u4f46\u4f7f\u7528system()\u51fd\u6570\u8fd8\u662f\u5bb9\u6613\u51fa\u9519\uff0c\u9519\u5728\u54ea\uff1f\u90a3\u5c31\u662fsystem()\u51fd\u6570\u7684\u8fd4\u56de\u503c\uff0c\u5173\u4e8e\u5176\u8fd4\u56de\u503c\u7684\u4ecb\u7ecd\u8bf7\u89c1\u4e0a\u7bc7\u6587\u7ae0\u3002system()\u51fd\u6570\u6709\u65f6\u5f88\u65b9\u4fbf\uff0c\u4f46\u4e0d\u53ef\u6ee5\u7528\uff01<\/p>\n<p>1\u3001\u5efa\u8baesystem()\u51fd\u6570\u53ea\u7528\u6765\u6267\u884cshell\u547d\u4ee4\uff0c\u56e0\u4e3a\u4e00\u822c\u6765\u8bb2\uff0csystem()\u8fd4\u56de\u503c\u4e0d\u662f0\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\uff1b<\/p>\n<p>2\u3001\u5efa\u8bae\u76d1\u63a7\u4e00\u4e0bsystem()\u51fd\u6570\u7684\u6267\u884c\u5b8c\u6bd5\u540e\u7684errno\u503c\uff0c\u4e89\u53d6\u51fa\u9519\u65f6\u7ed9\u51fa\u66f4\u591a\u6709\u7528\u4fe1\u606f\uff1b<\/p>\n<p>3\u3001\u5efa\u8bae\u8003\u8651\u4e00\u4e0bsystem()\u51fd\u6570\u7684\u66ff\u4ee3\u51fd\u6570popen()\uff1b\u5176\u7528\u6cd5\u5728\u6211\u7684\u53e6\u4e00\u7bc7\u6587\u7ae0\u6709\u4ecb\u7ecd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u66fe\u7ecf\u7684\u66fe\u7ecf\uff0c\u88absystem()\u51fd\u6570\u6298\u78e8\u8fc7\uff0c\u4e4b\u6240\u4ee5\u8fd9\u6837\uff0c\u662f\u56e0\u4e3a\u5bf9system()\u51fd\u6570\u4e86\u89e3\u4e0d\u591f\u6df1\u5165\u3002\u53ea\u662f\u7b80\u5355\u7684\u77e5\u9053 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1635","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1635","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1635"}],"version-history":[{"count":2,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1637,"href":"http:\/\/www.51cos.com\/index.php?rest_route=\/wp\/v2\/posts\/1635\/revisions\/1637"}],"wp:attachment":[{"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.51cos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}