{"id":362,"date":"2021-11-23T21:41:23","date_gmt":"2021-11-23T13:41:23","guid":{"rendered":"https:\/\/pixelhn.cn\/?p=362"},"modified":"2021-11-24T16:29:45","modified_gmt":"2021-11-24T08:29:45","slug":"linux-%e8%bf%9b%e7%a8%8b%ef%bc%881%ef%bc%89","status":"publish","type":"post","link":"https:\/\/pixelhn.cn\/?p=362","title":{"rendered":"Linux\u2014\u2014\u8fdb\u7a0b\uff081\uff09"},"content":{"rendered":"<h2>Linux\u2014\u2014\u8fdb\u7a0b\uff081\uff09<\/h2>\n<p>\u5728Linux\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u7684\u547d\u4ee4\u662f\u4f7f\u7528<code>fork()<\/code>\u7cfb\u7edf\u8c03\u7528\uff1a<\/p>\n<pre><code class=\"language-c\">int fork();<\/code><\/pre>\n<hr \/>\n<p>\u6211\u4eec\u628a\u8c03\u7528<code>fork()<\/code>\u7684\u8fdb\u7a0b\u79f0\u4e3a<strong>\u7236\u8fdb\u7a0b<\/strong>\uff0c\u628a\u521b\u5efa\u7684\u65b0\u8fdb\u7a0b\u79f0\u4e3a<strong>\u5b50\u8fdb\u7a0b<\/strong>\u3002\u5b50\u8fdb\u7a0b\u5c06\u62e5\u6709\u7236\u8fdb\u7a0b\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u5730\u5740\u7a7a\u95f4\u3001\u6570\u636e\u548c\u5806\u6808\u6bb5\u7684\u4e00\u4efd\u62f7\u8d1d\uff0c\u4ee5\u53ca\u5bf9\u6240\u6709\u6253\u5f00\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff08\u5728\u5185\u6838\u4e2d\uff09\u7684\u8bbf\u95ee\u6743\u3002\u5b83\u4eec\u6709\u5404\u81ea\u7684\u865a\u62df\u673a\uff0c\u5728\u5404\u81ea\u72ec\u7acb\u7684\u5730\u5740\u7a7a\u95f4\u4e2d<strong>\u5e76\u53d1<\/strong>\u4ece<code>fork()<\/code>\u4e4b\u540e\u7684\u540c\u4e00\u6267\u884c\u70b9\u5f00\u59cb\u6267\u884c\u2014\u2014\u5728\u6b64\u523b\u7236\u8fdb\u7a0b\u4e0e\u5b50\u8fdb\u7a0b\u4eff\u4f5b\u4e24\u4e2a\u5b8c\u5168\u76f8\u540c\u7684\u5e73\u884c\u5b87\u5b99\uff1b<\/p>\n<p>\u4e0d\u540c\u5728\u4e8e\uff1a\u7236\u8fdb\u7a0b\u4e2d\u7684<code>fork()<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u5b50\u8fdb\u7a0b\u7684PID\uff1b\u5b50\u8fdb\u7a0b\u4e2d\u5219\u8fd4\u56de<code>0<\/code>\u503c\uff1b\u57fa\u4e8e\u6b64\uff0c\u4e00\u822c\u5730\uff0c\u53ef\u4ee5\u4f7f\u7528\u6761\u4ef6\u5224\u65ad\uff0c\u8ba9\u7236\u8fdb\u7a0b\u4e0e\u5b50\u8fdb\u7a0b\u4ea7\u751f\u4e0d\u540c\u884c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-c\">childpid = fork();\nif( childpid == 0 ){\n    \/\/\u5b50\u8fdb\u7a0b\u5c06\u4f1a\u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c\n    ...\n    exit(0);\n}\n\/\/\u7236\u8fdb\u7a0b\u5c06\u4f1a\u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c\n...<\/code><\/pre>\n<p>\u8fd9\u4eff\u4f5b\u662f\u4e24\u4e2a\u5e73\u884c\u5b87\u5b99\u540c\u65f6\u5bf9\u859b\u5b9a\u8c14\u7684\u732b\u8fdb\u884c\u89c2\u5bdf\u2014\u2014\u5176\u4e2d\u4e00\u4e2a\u5b87\u5b99\u7684\u732b\u662f\u6b7b\u6389\u7684\u800c\u53e6\u5916\u4e00\u4e2a\u5b87\u5b99\u7684\u732b\u662f\u6d3b\u7740\uff0c\u4ece\u6b64\u4e24\u4e2a\u5e73\u884c\u5b87\u5b99\u53ef\u4ee5\u5f00\u59cb\u6709\u5b8c\u5168\u4e0d\u540c\u7684\u884c\u4e3a<\/p>\n<blockquote>\n<p>\u300a\u859b\u5b9a\u8c14\u7684\u732b\u300b\u7684\u4e00\u79cd\u89e3\u91ca\u4e3a\u5728\u89c2\u5bdf\u7684\u77ac\u95f4\uff0c\u5b87\u5b99\u624d\u53d1\u751f\u5206\u88c2\uff0c\u5f62\u6210\u4e24\u4e2a\u5e73\u884c\u5b87\u5b99\u2014\u2014\u5176\u5404\u81ea\u89c2\u5bdf\u7ed3\u679c\u4e3a\u5176\u4e2d\u4e00\u4e2a\u732b\u8fd8\u6d3b\u7740\uff0c\u53e6\u4e00\u4e2a\u732b\u5df2\u7ecf\u6b7b\u6389\u3002<\/p>\n<\/blockquote>\n<p>\u53e6\u5916\u5b50\u8fdb\u7a0b\u548c\u7236\u8fdb\u7a0b\u4e0d\u80fd\u901a\u8fc7\u5f15\u7528\u5b58\u50a8\u5728\u76f8\u540c\u5730\u5740\u4e0a\u7684\u53d8\u91cf\u8fdb\u884c\u901a\u4fe1\uff0c\u552f\u4e00\u53ef\u4ee5\u5171\u4eab\u5f15\u7528\u7684\u662f\u6253\u5f00\u7684\u6587\u4ef6\u2014\u2014Linux\u5229\u7528\u8fd9\u79cd\u65b9\u5f0f\u6765\u4f5c\u4e3a\u8fdb\u7a0b\u95f4\u63a5\u901a\u4fe1\u673a\u5236\u3002<\/p>\n<hr \/>\n<p>\u6d4b\u8bd5\u4ee3\u7801<\/p>\n<pre><code class=\"language-c\">\/\/fork.c\n#include&lt;stdio.h&gt;\n#include&lt;stdlib.h&gt;\n#include &lt;unistd.h&gt;\n\nvoid main(){\n        int childpid, parentpid;\n        parentpid = getpid();\n        childpid = fork();\n        if( childpid == 0 ){\n                childpid = getpid();\n                printf(&quot;I&#039;m child my pid:%d,my parent pid:%d\\n&quot;,childpid,parentpid);\n                exit(0);\n        }\n        printf(&quot;I&#039;m parent my pid:%d,my child pid:%d\\n&quot;,parentpid,childpid);\n        exit(0);\n}<\/code><\/pre>\n<p>\u8fd0\u884c<\/p>\n<pre><code class=\"language-shell\">$ cc fork.c \n$ .\/a.out \nI&#039;m parent my pid:45173,my child pid:45174\nI&#039;m child my pid:45174,my parent pid 45173<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u2014\u2014\u8fdb\u7a0b\uff081\uff09 \u5728Linux\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u7684\u547d\u4ee4\u662f\u4f7f\u7528fork()\u7cfb\u7edf\u8c03\u7528\uff1a int fork(&hellip; <a class=\"more-link\" href=\"https:\/\/pixelhn.cn\/?p=362\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">Linux\u2014\u2014\u8fdb\u7a0b\uff081\uff09<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-362","post","type-post","status-publish","format-standard","hentry","category-linux","category-all","entry"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/posts\/362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=362"}],"version-history":[{"count":9,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":378,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=\/wp\/v2\/posts\/362\/revisions\/378"}],"wp:attachment":[{"href":"https:\/\/pixelhn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelhn.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}