{"id":76,"date":"2015-10-26T19:35:29","date_gmt":"2015-10-26T11:35:29","guid":{"rendered":"http:\/\/dishudishu.com\/?p=76"},"modified":"2015-10-26T19:46:17","modified_gmt":"2015-10-26T11:46:17","slug":"linux%e7%b3%bb%e7%bb%9f%e4%b8%8b%e7%9a%84fork%e5%87%bd%e6%95%b0%ef%bc%8cwait%e5%87%bd%e6%95%b0%e5%92%8cexit%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/dishudishu.com\/?p=76","title":{"rendered":"linux\u7cfb\u7edf\u4e0b\u7684fork\u51fd\u6570\uff0cwait\u51fd\u6570\u548cexit\u51fd\u6570"},"content":{"rendered":"<p><strong>fork()\u51fd\u6570<\/strong>\u901a\u8fc7<span style=\"text-decoration: underline;\">\u7cfb\u7edf\u8c03\u7528<\/span>\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\uff08\u5b50\u8fdb\u7a0b\uff09\uff0c\u7cfb\u7edf\u4e3a\u5b83\u5206\u914d\u8d44\u6e90\uff0c\u5e76\u628a\u5176\u7236\u8fdb\u7a0b\u7684<span style=\"text-decoration: underline;\">\u5f53\u524d\u7684\u60c5\u51b5<\/span>\u590d\u5236\u5230\u5b50\u8fdb\u7a0b\u4e2d\uff0c\u4ece\u6574\u4f53\u4e0a\u770b\u76f8\u5f53\u4e8e\u6709\u4e24\u4efd\u6570\u636e\uff08\u6240\u4ee5\u7b80\u5355\u6539\u52a8\u4e00\u4e2a\u8fdb\u7a0b\u5185\u7684\u53d8\u91cf\u7684\u6570\u636e\u5b8c\u5168\u4e0d\u4f1a\u5f71\u54cd\u5230\u53e6\u4e00\u4e2a\u8fdb\u7a0b\uff09\u3002<\/p>\n<p>fork()\u51fd\u6570\u8c03\u7528\u4e00\u6b21\uff0c\u8fd4\u56de\u4e24\u6b21\u3002\u770b\u4e0b\u9762\u4e00\u6bb5\u4ee3\u7801<\/p>\n<pre class=\"lang:c decode:true \">pid_t = fpid;\r\nfpid = fork();<\/pre>\n<p>\u8fd9\u91ccfork()\u51fd\u6570\u53ea\u51fa\u73b0\u4e86\u4e00\u6b21\uff0c\u5373\u53ea\u88ab\u8c03\u7528\u4e86\u4e00\u6b21\uff0c\u4f46\u6b64\u65f6\u5b83\u5df2\u7ecf\u521b\u5efa\u4e86\u53e6\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u6240\u4ee5\u5171\u4e24\u4e2a\u8fdb\u7a0b\u3002\u53ef\u4ee5\u7406\u89e3\u4e3a\uff0c\u7236\u8fdb\u7a0b\u8c03\u7528\u4e86fork()\u51fd\u6570\uff0c\u521b\u5efa\u5b50\u8fdb\u7a0b\u4ee5\u540e\uff0cfork()\u8fd4\u56de\u4e86\u4e00\u4e2a\u503c\u7ed9\u7236\u8fdb\u7a0b\uff0c\u540c\u65f6\u8fd8\u8fd4\u56de\u4e86\u4e00\u4e2a\u503c\u7ed9\u65b0\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u3002<\/p>\n<p>fork()\u51fd\u6570\u6709\u4e09\u79cd\u8fd4\u56de\u503c\uff1a<\/p>\n<p>1\uff09\u5728\u7236\u8fdb\u7a0b\u4e2d\uff0cfork()\u8fd4\u56de\u65b0\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\u8fdb\u7a0bID\uff1b<\/p>\n<p>2\uff09\u5728\u5b50\u8fdb\u7a0b\u4e2d\uff0cfork\u8fd4\u56de0\uff1b<\/p>\n<p>3\uff09\u5982\u679c\u51fa\u73b0\u9519\u8bef\uff0cfork\u8fd4\u56de\u4e00\u4e2a\u8d1f\u503c\uff1b<\/p>\n<p>\u4e0a\u9762\u63d0\u5230fork()\u51fd\u6570\u53ea\u590d\u5236\u6240\u8c13\u201c\u5f53\u524d\u7684\u60c5\u51b5\u201d\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u201c\u6b64\u65f6\u5df2\u7ecf\u6267\u884c\u8fc7\u7684\u8bed\u53e5\u6240\u5448\u73b0\u7684\u60c5\u51b5\u201d\uff0c\u6240\u4ee5\u65f6\u95f4\u4e0a\u5728fork()\u4ee5\u540e\u624d\u6267\u884c\u7684\u8bed\u53e5\u4f1a\u5728\u4e24\u4e2a\u8fdb\u7a0b\u4e2d\u6267\u884c\u3002\u82e5\u6709\u8fd9\u6837\u7684\u8bed\u53e5\uff1a<\/p>\n<pre class=\"lang:c decode:true \">fpid1 = fork();\r\nfpid2 = fork();<\/pre>\n<p>\u5219\u6267\u884c\u5b8c\u540e\u7cfb\u7edf\u5c06\u4f1a\u67094\u4e2a\u8fdb\u7a0b\u3002\u5177\u4f53\u7684\uff0c\u4e00\u5f00\u59cb\u53ea\u6709\u8fdb\u7a0b1\uff0c\u6267\u884c\u5b8c\u7b2c\u4e00\u4e2afork()\u4ee5\u540e\u8fdb\u7a0b1\u521b\u5efa\u4e86\u8fdb\u7a0b2\uff0cfork()\u5206\u522b\u7ed9\u4ed6\u4eec\u8fd4\u56de\u4e86\u4e00\u4e2a\u503c\u3002\u4e4b\u540e\uff0c\u5bf9\u4e8e\u8fdb\u7a0b1\u548c\u8fdb\u7a0b2\uff0c\u90fd\u662f\u4ece\u5f97\u5230\u4e00\u4e2a\u8fd4\u56de\u503c\u4ee5\u540e\u7ee7\u7eed\u8fd0\u884c\u7684\uff0c\u4e5f\u5c31\u662f\u5206\u522b\u8c03\u7528\u4e86\u4e00\u6b21fork()\u3002\u8fd9\u6837\u8fdb\u7a0b1\u5c31\u521b\u5efa\u4e86\u8fdb\u7a0b3\uff0c\u8fdb\u7a0b2\u521b\u5efa\u4e86\u8fdb\u7a0b4\uff08\u6b64\u5904\u6570\u5b57\u4e0d\u8868\u793a\u5148\u540e\u521b\u5efa\u987a\u5e8f\uff0c\u89c1\u4e0b\u4e00\u6bb5\uff09\u3002\u9762\u8bd5\u9898\u4e2d\u53ef\u80fd\u4f1a\u6709\u8fd9\u6837\u7684\u8003\u70b9\uff0c\u7ed9\u51fa\u7684\u4f8b\u5b50\u662f\u6700\u7b80\u5355\u6700\u57fa\u672c\u7684\u60c5\u5f62\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u521b\u5efa\u65b0\u8fdb\u7a0b\u6210\u529f\u540e\uff0c\u7cfb\u7edf\u4e2d\u51fa\u73b0\u4e24\u4e2a\u57fa\u672c\u5b8c\u5168\u76f8\u540c\u7684\u8fdb\u7a0b\uff0c\u8fd9\u4e24\u4e2a\u8fdb\u7a0b\u6267\u884c\u6ca1\u6709\u56fa\u5b9a\u7684\u5148\u540e\u987a\u5e8f\uff0c\u54ea\u4e2a\u8fdb\u7a0b\u5148\u6267\u884c\u8981\u770b\u7cfb\u7edf\u7684\u8fdb\u7a0b\u8c03\u5ea6\u7b56\u7565\u3002<\/p>\n<p>\u5982\u679c\u8981\u63a7\u5236fork()\u4ee5\u540e\uff0c\u7b49\u5b50\u8fdb\u7a0b\u6267\u884c\u5b8c\u5e76\u8fd4\u56de\u4e86\uff0c\u518d\u6267\u884c\u7236\u8fdb\u7a0b\uff0c\u53ef\u4ee5\u8c03\u7528wait()\u51fd\u6570\u3002\u8bb2wait()\u51fd\u6570\u4e4b\u524d\uff0c\u5148\u7b80\u5355\u8bb2\u4e00\u4e2a\u6982\u5ff5\uff1a<strong>\u50f5\u5c38\u8fdb\u7a0b<\/strong>\u3002<\/p>\n<p>\u5728UNIX\u7cfb\u7edf\u4e0b\uff0c\u82e5\u4e00\u4e2a\u5b50\u8fdb\u7a0b\u7ed3\u675f\u4e86\uff0c\u4f46\u5b83\u7684\u7236\u8fdb\u7a0b\u6ca1\u6709\u7b49\u5f85\uff08\u8c03\u7528wait() \/waitpid()\uff09\u5b83\uff0c\u90a3\u5b83\u5c06\u4f1a\u53d8\u6210\u4e00\u4e2a\u50f5\u5c38\u8fdb\u7a0b\u3002\u50f5\u5c38\u8fdb\u7a0b\u7684\u5371\u5bb3\u5728\u4e8e\uff1a\u82e5\u4e0d\u53ca\u65f6\u91ca\u653e\uff0c\u5b83\u4f1a\u4e00\u76f4\u5360\u7528\u7740\u5b83\u7684\u8fdb\u7a0b\u53f7\uff0c\u800c\u7cfb\u7edf\u6240\u80fd\u4f7f\u7528\u7684\u8fdb\u7a0b\u53f7\u662f\u6709\u9650\u7684\uff0c\u6240\u4ee5\u5927\u91cf\u7684\u50f5\u5c38\u8fdb\u7a0b\u53ef\u80fd\u5bfc\u81f4\u7cfb\u7edf\u65e0\u6cd5\u521b\u5efa\u65b0\u8fdb\u7a0b\u3002<\/p>\n<p>\u8bb2\u56de<strong>wait()\u51fd\u6570<\/strong>\u3002\u8fdb\u7a0b\u4e00\u65e6\u8c03\u7528\u4e86wait()\uff0c\u5c31\u7acb\u5373\u963b\u585e\u81ea\u5df1\uff0c\u5e76\u7531wait\u81ea\u52a8\u5206\u6790\u662f\u5426\u6709\u67d0\u4e2a\u5f53\u524d\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\u5df2\u7ecf\u9000\u51fa\uff0c\u5982\u679c\u8ba9\u5b83\u627e\u5230\u4e86\u8fd9\u6837\u4e00\u4e2a\u5df2\u7ecf\u53d8\u6210\u50f5\u5c38\u7684\u5b50\u8fdb\u7a0b\uff0cwait\u5c31\u4f1a\u6536\u96c6\u8fd9\u4e2a\u5b50\u8fdb\u7a0b\u7684\u4fe1\u606f\uff0c\u5e76\u628a\u5b83\u5f7b\u5e95\u9500\u6bc1\u540e\u8fd4\u56de\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\u8fd9\u6837\u4e00\u4e2a\u5b50\u8fdb\u7a0b\uff0cwait\u5c31\u4f1a\u4e00\u76f4\u963b\u585e\u5728\u8fd9\u91cc\uff0c\u76f4\u5230\u6709\u4e00\u4e2a\u51fa\u73b0\u4e3a\u6b62\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>exit()\u51fd\u6570<\/strong>\u53ef\u7528\u4e8e\u7ec8\u7ed3\u4e00\u4e2a\u8fdb\u7a0b\u3002exit\u7684\u8fd4\u56de\u503c\u662f\u8fd4\u56de\u7ed9\u64cd\u4f5c\u7cfb\u7edf\u7684\uff0c\u4f46\u5982\u679c\u662f\u591a\u8fdb\u7a0b\uff0c\u5219\u662f\u8fd4\u56de\u7ed9\u7236\u8fdb\u7a0b\u7684\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u5199\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u7a0b\u5e8f\uff1a\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8ba1\u7b97\u6590\u6ce2\u90a3\u5951\u6570\u5217\u5e76\u628a\u7236\u8fdb\u7a0b\u6302\u8d77\u3002<\/p>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/wait.h&gt;\r\n\r\nint main()\r\n{\r\n    pid_t pid;          \/\/process id\r\n    int i, a, b, fib;\r\n\r\n    int n;\r\n    scanf(\"%d\", &amp;n);\r\n\r\n    pid = fork();\r\n\r\n    if(pid &lt; 0)         \/\/\u8bf4\u660e\u5f53\u524d\u8fdb\u7a0b\u662f\u7236\u8fdb\u7a0b\u4e14\u521b\u5efa\u5b50\u8fdb\u7a0b\u5931\u8d25\r\n    {\r\n        fprintf(stderr, \"Fork Failed\\n\");\r\n        exit(-1);\r\n    }\r\n    else if(pid == 0)   \/\/\u8bf4\u660e\u5f53\u524d\u8fdb\u7a0b\u662f\u5b50\u8fdb\u7a0b\r\n    {\r\n        if(n == 1)\r\n            printf(\"0\\n\");\r\n        else if(n == 2)\r\n            printf(\"0 1\\n\");\r\n        else if(n &gt; 2)\r\n        {\r\n            a = 0;\r\n            b = 1;\r\n            printf(\"0 1 \");\r\n\r\n            for(i = 3; i &lt;= n; i++)\r\n            {\r\n                fib = a + b;\r\n                printf(\"%d%c\", fib, \" \\n\"[i == n]);\r\n                a = b;\r\n                b = fib;\r\n            }\r\n        }\r\n    }\r\n    else                \/\/\u8bf4\u660e\u5f53\u524d\u8fdb\u7a0b\u662f\u7236\u8fdb\u7a0b\r\n    {\r\n        wait(NULL);\r\n\r\n        exit(0);\r\n    }\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6587\u4e2d\u63d0\u5230\u7684\u76f8\u5173\u6982\u5ff5\u53ea\u505a\u4e86\u5f88\u7c97\u6d45\u7684\u89e3\u91ca\uff0c\u6df1\u5165\u5b66\u4e60\u7684\u8bdd\u8bf7\u79fb\u6b65\u4e0b\u9762\u94fe\u63a5\u3002<\/p>\n<p>reference\uff1a<\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/jason314\/article\/details\/5640969\">fork()\u51fd\u6570<\/a><\/p>\n<p><a href=\"http:\/\/wenku.baidu.com\/link?url=-jPByzWLbet-2WIrPsI9XAYnY6aKK7sGGUN9ElL87g_D-yQ4i99iHa6XyUh0WYKb_XJb1IwdLA5gpzDEz1eAXZUCVANfg0vIUIHrO8QT-ti\">\u50f5\u5c38\u8fdb\u7a0b<\/a><\/p>\n<p><a href=\"http:\/\/blog.163.com\/lqy_super\/blog\/static\/1997510212012112953858902\/\">wait()\u51fd\u6570<\/a><\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/firefly_2002\/article\/details\/7960595\">exit\u548creturn\u7684\u533a\u522b<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>fork()\u51fd\u6570\u901a\u8fc7\u7cfb\u7edf\u8c03\u7528\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\uff08\u5b50\u8fdb\u7a0b\uff09\uff0c\u7cfb\u7edf\u4e3a\u5b83\u5206\u914d\u8d44\u6e90\uff0c\u5e76\u628a\u5176\u7236\u8fdb\u7a0b\u7684\u5f53\u524d\u7684\u60c5\u51b5\u590d\u5236\u5230\u5b50\u8fdb\u7a0b\u4e2d &hellip; <a href=\"https:\/\/dishudishu.com\/?p=76\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">linux\u7cfb\u7edf\u4e0b\u7684fork\u51fd\u6570\uff0cwait\u51fd\u6570\u548cexit\u51fd\u6570<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":[],"categories":[1,4],"tags":[],"views":673,"_links":{"self":[{"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dishudishu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=76"}],"version-history":[{"count":3,"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/dishudishu.com\/index.php?rest_route=\/wp\/v2\/posts\/76\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/dishudishu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dishudishu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dishudishu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}