{"id":2103,"date":"2010-12-07T09:59:04","date_gmt":"2010-12-07T01:59:04","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=2103"},"modified":"2010-12-15T15:52:04","modified_gmt":"2010-12-15T07:52:04","slug":"fork%e7%b3%bb%e7%bb%9f%e8%b0%83%e7%94%a8%e5%88%86%e6%9e%901","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/2103","title":{"rendered":"fork\u7cfb\u7edf\u8c03\u7528\u5206\u6790(1)-\u51c6\u5907\u5de5\u4f5c"},"content":{"rendered":"<p><em><strong>\u63a5\u4e0b\u6765\u7684\u51e0\u7bc7\u6587\u7ae0\u5c06\u4f1a\u5206\u6790fork\u7cfb\u7edf\u8c03\u7528\u5728\u5185\u6838\u4e2d\u7684\u5b9e\u73b0\uff0c\u4ee5<\/strong><\/em><em><strong>\u4f5c\u4e3a\u672c\u5b66\u671flinux\u64cd\u4f5c\u7cfb\u7edf\u8bfe\u7a0b\u603b\u7684\u5206\u6790\u62a5\u544a\u3002\u672c\u6587\u6240\u91c7\u7528\u7684\u6e90\u7801\u5747\u4e3av2.6.11\u3002\u548c\u6700\u65b0\u5185\u6838\u76f8\u6bd4\uff0cdo_fork\u51fd\u6570\u7684\u53d8\u5316\u8f83\u5927\uff0c\u800c\u6838\u5fc3\u51fd\u6570copy_process\u5219\u51e0\u4e4e\u4fdd\u6301\u4e0d\u53d8\u3002<\/strong><\/em><\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u4e00\u79cd\u673a\u5236\u7528\u4e8e\u521b\u5efa\u65b0\u8fdb\u7a0b\uff0cfork()\u5c31\u662fLinux\u6216Unix\u63d0\u4f9b\u7ed9\u7a0b\u5e8f\u5458\u7528\u4e8e\u521b\u5efa\u8fdb\u7a0b\u7684\u65b9\u6cd5\u3002fork\u51fd\u6570\u7684\u76f8\u5173\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n<p><a rel=\"attachment wp-att-2184\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2103\/2-4\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2184\" title=\"2\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/21.jpg\" alt=\"\" width=\"550\" height=\"217\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/21.jpg 550w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/21-300x118.jpg 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p>\u901a\u5e38\uff0c\u6211\u4eec\u7a0b\u5e8f\u4e2d\u76f4\u63a5\u4f7f\u7528\u7684fork\u51fd\u6570\u662f\u5c06fork\u7cfb\u7edf\u8c03\u7528\u5c01\u88c5\u4e4b\u540e\u800c\u4ea7\u751f\u7684\u3002\u901a\u8fc7\u4e0a\u9762\u7684\u8868\u683c\uff0c\u6211\u4eec\u77e5\u9053fork\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u65b0\u7684\u8fdb\u7a0b\uff0c\u6240\u521b\u5efa\u7684\u8fdb\u7a0b\u79f0\u4e3a\u5f53\u524d\u8fdb\u7a0b\u7684\u5b50\u8fdb\u7a0b\uff0c\u800c\u5f53\u524d\u8fdb\u7a0b\u4e5f\u968f\u4e4b\u79f0\u4e3a\u5b50\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u3002\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u7236\u5b50\u8fdb\u7a0b\u4e0d\u540c\u7684\u8fd4\u56de\u503c\u6765\u533a\u5206\u7236\u5b50\u8fdb\u7a0b\uff0c\u5e76\u4f7f\u5176\u6267\u884c\u4e0d\u540c\u529f\u80fd\u7684\u4ee3\u7801\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u7528\u6237\u6001\u4e0b\u7684fork\u51fd\u6570\u662f\u5982\u4f55\u8c03\u7528fork\u7cfb\u7edf\u8c03\u7528\u7684\uff1f\u5185\u6838\u4e2d\u662f\u5982\u4f55\u521b\u5efa\u5b50\u8fdb\u7a0b\u7684\uff1f\u7236\u5b50\u8fdb\u7a0b\u7684\u8fd4\u56de\u503c\u662f\u5982\u4f55\u4ea7\u751f\u7684\uff1f\u8fd9\u662f\u672c\u6587\u6240\u91cd\u70b9\u8ba8\u8bba\u7684\u3002<\/p>\n<h4>\u8fdb\u7a0b\u63cf\u8ff0\u7b26<\/h4>\n<p>\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u4e00\u4e2a\u91cd\u8981\u7684\u57fa\u672c\u6982\u5ff5\u3002\u901a\u5e38\u6211\u4eec\u8ba4\u4e3a\u8fdb\u7a0b\u662f\u7a0b\u5e8f\u7684\u4e00\u6b21\u6267\u884c\u8fc7\u7a0b\u3002\u4e3a\u4e86\u63cf\u8ff0\u548c\u63a7\u5236\u8fdb\u7a0b\u7684\u8fd0\u884c\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4e3a\u6bcf\u4e2a\u8fdb\u7a0b\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u5373\u8fdb\u7a0b\u63a7\u5236\u5757\uff08Process Control Block\uff0cPCB\uff09\u3002\u6211\u4eec\u901a\u5e38\u6240\u8bf4\u7684\u8fdb\u7a0b\u5b9e\u4f53\u5305\u542b\u7a0b\u5e8f\u6bb5\uff0c\u6570\u636e\u6bb5\u548cPCB\u4e09\u90e8\u5206\uff0cPCB\u5728\u8fdb\u7a0b\u5b9e\u4f53\u4e2d\u5360\u636e\u91cd\u8981\u7684\u5730\u4f4d\u3002\u6240\u8c13\u7684\u521b\u5efa\u8fdb\u7a0b\uff0c\u5b9e\u8d28\u4e0a\u5c31\u662f\u521b\u5efaPCB\u7684\u8fc7\u7a0b\uff1b\u800c\u64a4\u9500\u8fdb\u7a0b\uff0c\u5b9e\u8d28\u4e0a\u4e5f\u5c31\u662f\u5bf9PCB\u7684\u64a4\u9500\u3002<\/p>\n<p>\u4e0a\u8ff0\u5185\u5bb9\u662f\u6211\u4eec\u5728\u64cd\u4f5c\u7cfb\u7edf\u539f\u7406\u8bfe\u4e0a\u6240\u5b66\u4e60\u5230\u7684\u3002\u5728Linux\u5185\u6838\u4e2d\uff0cPCB\u5bf9\u5e94\u7740\u4e00\u4e2a\u5177\u4f53\u7684\u7ed3\u6784\u4f53\u2014\u2014task_struct\uff0c\u4e5f\u5c31\u662f\u6240\u8c13\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff08process descriptor\uff09\u3002\u8be5\u6570\u636e\u7ed3\u6784\u4e2d\u5305\u542b\u4e86\u4e0e\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5173\u7684\u6240\u6709\u4fe1\u606f\uff0c\u6bd4\u5982\u5305\u542b\u4f17\u591a\u63cf\u8ff0\u8fdb\u7a0b\u5c5e\u6027\u7684\u5b57\u6bb5\uff0c\u4ee5\u53ca\u6307\u5411\u5176\u4ed6\u4e0e\u8fdb\u7a0b\u76f8\u5173\u7684\u7ed3\u6784\u4f53\u7684\u6307\u9488\u3002\u56e0\u6b64\uff0c\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u5185\u90e8\u662f\u6bd4\u8f83\u590d\u6742\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe1\u5927\u81f4\u7684\u4e86\u89e3\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u7ed3\u6784\u3002<\/p>\n<p><a rel=\"attachment wp-att-2176\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2103\/screenshot-1-3\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2176\" title=\"Screenshot-1\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot-11.png\" alt=\"\" width=\"559\" height=\"612\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot-11.png 559w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/Screenshot-11-274x300.png 274w\" sizes=\"auto, (max-width: 559px) 100vw, 559px\" \/><\/a><\/p>\n<p style=\"text-align: center;\"><strong>\u56fe1\uff08\u56fe\u7247\u6765\u81eaULK\uff09<\/strong><\/p>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u6709\u6307\u5411mm_struct\u7ed3\u6784\u4f53\u7684\u6307\u9488mm\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f\u5bf9\u8be5\u8fdb\u7a0b\u7528\u6237\u7a7a\u95f4\u7684\u63cf\u8ff0\uff1b\u4e5f\u6709\u6307\u5411fs_struct\u7ed3\u6784\u4f53\u7684\u6307\u9488fs\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f\u5bf9\u8fdb\u7a0b\u5f53\u524d\u6240\u5728\u76ee\u5f55\u7684\u63cf\u8ff0\uff1b\u4e5f\u6709\u6307\u5411files_struct\u7ed3\u6784\u4f53\u7684\u6307\u9488files\uff0c\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662f\u5bf9\u8be5\u8fdb\u7a0b\u5df2\u6253\u5f00\u7684\u6240\u6709\u6587\u4ef6\u8fdb\u884c\u63cf\u8ff0\uff1b\u53e6\u5916\u8fd8\u6709\u4e00\u4e2a\u5c0f\u578b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff08low-level information\uff09\u2014\u2014thread_info\u3002\u5728\u8fd9\u4e2a\u7ed3\u6784\u4f53\u4e2d\uff0c\u4e5f\u6709\u6307\u5411\u8be5\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u7684\u6307\u9488task\u3002\u56e0\u6b64\uff0c\u8fd9\u4e24\u4e2a\u7ed3\u6784\u4f53\u662f\u76f8\u4e92\u5173\u8054\u7684\u3002\u5173\u4e8ethread_info\u7ed3\u6784\u7684\u8be6\u7ec6\u8bf4\u660e\u53ef\u4ee5\u53c2\u8003\u672c\u7cfb\u5217\u6587\u7ae0\u7684\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p>\u4e0e\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\u7684\u7a0b\u5e8f\u76f8\u6bd4\uff0c\u6211\u4eec\u8ba4\u4e3a\u662f\u8fdb\u7a0b\u662f\u52a8\u6001\u7684\u3002\u8fd9\u662f\u56e0\u4e3a\u4e00\u4e2a\u8fdb\u7a0b\u5728\u5176\u201c\u4e00\u751f\u201d\u4e2d\u53ef\u80fd\u5904\u4e8e\u4e0d\u540c\u7684\u72b6\u6001\u3002\u56e0\u6b64\uff0c\u5728\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\uff0c\u4f7f\u7528state\u5b57\u6bb5\u5bf9\u8be5\u8fdb\u7a0b\u5f53\u524d\u6240\u5904\u7684\u72b6\u6001\u8fdb\u884c\u63cf\u8ff0\u3002\u5728\u5185\u6838\u4e2d\uff0c\u4f7f\u7528\u4e00\u7ec4\u5b8f\u6765\u63cf\u8ff0\u8fdb\u7a0b\u53ef\u80fd\u6240\u5904\u7684\u72b6\u6001\u3002\u8fd9\u4e9b\u5b8f\u4e4b\u95f4\u662f\u4e92\u65a5\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fdb\u7a0b\u4e00\u6b21\u6700\u591a\u53ea\u80fd\u4f7f\u7528\u4e00\u4e2a\u5b8f\u3002\u56e0\u4e3a\uff0c\u8fdb\u7a0b\u5728\u67d0\u4e00\u523b\u53ea\u53ef\u80fd\u5904\u4e8e\u4e00\u79cd\u72b6\u6001\u3002\u4e0b\u9762\u5bf9\u672c\u6587\u4e2d\u6240\u6d89\u53ca\u5230\u7684\u8fdb\u7a0b\u72b6\u6001\u8fdb\u884c\u7b80\u5355\u7684\u63cf\u8ff0\uff1a<\/p>\n<p><strong>\u53ef\u8fd0\u884c\u72b6\u6001\uff08TASK_RUNNING\uff09<\/strong><br \/>\n\u5982\u679c\u8fdb\u7a0b\u6b63\u5728CPU\u4e0a\u6267\u884c\u6216\u8005\u6b63\u5728\u7b49\u5f85\u88ab\u8c03\u5ea6\u7a0b\u5e8f\u6240\u8c03\u5ea6\uff0c\u90a3\u4e48\u5b83\u7684\u72b6\u6001\u5c31\u5904\u4e8e\u53ef\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p><strong>\u6682\u505c\u72b6\u6001\uff08TASK_STOPPED\uff09<\/strong><br \/>\n\u8fdb\u7a0b\u7684\u6267\u884c\u88ab\u6682\u5b9a\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5e38\u8bf4\u7684\u963b\u585e\u72b6\u6001\u548c\u7b49\u5f85\u72b6\u6001\u3002\u5f53\u8fdb\u7a0b\u63a5\u6536\u5230SIGSTOP\u3001SIGSTP\u7b49\u4fe1\u53f7\u65f6\uff0c\u5c31\u8fdb\u5165\u8be5\u72b6\u6001\u3002<\/p>\n<p><strong>\u8ddf\u8e2a\u72b6\u6001\uff08TASK_TRACED\uff09<\/strong><br \/>\n\u5f53\u4e00\u4e2a\u8fdb\u7a0b\u88ab\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u8ddf\u8e2a\u76d1\u63a7\u65f6\uff0c\u8fd9\u4e2a\u8fdb\u7a0b\u5c31\u5904\u4e8e\u8be5\u72b6\u6001\u3002\u6700\u5e38\u89c1\u7684\u573a\u666f\u5c31\u662f\u6211\u4eec\u8c03\u8bd5\u4e00\u4e2a\u7a0b\u5e8f\uff0c\u88ab\u8c03\u8bd5\u7684\u7a0b\u5e8f\u5c31\u5904\u4e8e\u6b64\u72b6\u6001\u3002<\/p>\n<h4>fork\u7cfb\u7edf\u8c03\u7528<\/h4>\n<p>\u5728\u7528\u6237\u6001\u4e0b\uff0c\u4f7f\u7528fork()\u521b\u5efa\u4e00\u4e2a\u8fdb\u7a0b\u5bf9\u6211\u4eec\u6765\u8bf4\u5df2\u7ecf\u4e0d\u518d\u964c\u751f\u3002\u9664\u4e86\u8fd9\u4e2a\u51fd\u6570\uff0c\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u7684\u8bde\u751f\u8fd8\u53ef\u4ee5\u5206\u522b\u901a\u8fc7vfork()\u548cclone()\u3002fork\u3001vfork\u548cclone\u4e09\u4e2aAPI\u51fd\u6570\u5747\u7531C\u5e93\u63d0\u4f9b\uff0c\u5b83\u4eec\u5206\u522b\u5728C\u5e93\u4e2d\u5c01\u88c5\u4e86\u4e0e\u5176\u540c\u540d\u7684\u7cfb\u7edf\u8c03\u7528fork()\uff0cvfork()\u548cclone()\u3002API\u6240\u5c01\u88c5\u7684\u7cfb\u7edf\u8c03\u7528\u5bf9\u7f16\u7a0b\u8005\u662f\u9690\u85cf\u7684\uff0c\u7f16\u7a0b\u8005\u53ea\u9700\u77e5\u9053\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9bAPI\u5373\u53ef\u3002<\/p>\n<p>\u4e0a\u8ff0\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\u6240\u5bf9\u5e94\u7684\u7cfb\u7edf\u8c03\u7528\u53f7\u5728linux\/include\/asm-i386\/unistd.h\u4e2d\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">   #define __NR_restart_syscall      0\r\n   #define __NR_exit                 1\r\n   #define __NR_fork                 2\r\n   \u2026\u2026 \u2026\u2026\r\n   #define __NR_clone              120\r\n   \u2026\u2026 \u2026\u2026\r\n   #define __NR_vfork              190\r\n   \u2026\u2026 \u2026\u2026<\/pre>\n<p>\u4f20\u7edf\u7684\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u7684\u65b9\u5f0f\u662f\u5b50\u8fdb\u7a0b\u62f7\u8d1d\u7236\u8fdb\u7a0b\u6240\u6709\u8d44\u6e90\uff0c\u8fd9\u65e0\u7591\u4f7f\u5f97\u8fdb\u7a0b\u7684\u521b\u5efa\u6548\u7387\u4f4e\uff0c\u56e0\u4e3a\u5b50\u8fdb\u7a0b\u9700\u8981\u62f7\u8d1d\u7236\u8fdb\u7a0b\u7684\u6574\u4e2a\u5730\u5740\u7a7a\u95f4\u3002\u66f4\u7cdf\u7cd5\u7684\u662f\uff0c\u5982\u679c\u5b50\u8fdb\u7a0b\u521b\u5efa\u540e\u53c8\u7acb\u9a6c\u53bb\u6267\u884cexec\u65cf\u51fd\u6570\uff0c\u90a3\u4e48\u521a\u521a\u624d\u4ece\u7236\u8fdb\u7a0b\u90a3\u91cc\u62f7\u8d1d\u7684\u5730\u5740\u7a7a\u95f4\u53c8\u8981\u88ab\u6e05\u9664\u4ee5\u4fbf\u88c5\u5165\u65b0\u7684\u8fdb\u7a0b\u6620\u50cf\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5185\u6838\u4e2d\u63d0\u4f9b\u4e86\u4e0a\u8ff0\u4e09\u79cd\u4e0d\u540c\u7684\u7cfb\u7edf\u8c03\u7528\u3002<\/p>\n<p>1\uff0e \u5185\u6838\u91c7\u7528\u5199\u65f6\u590d\u5236\u6280\u672f\u5bf9\u4f20\u7edf\u7684fork\u51fd\u6570\u8fdb\u884c\u4e86\u4e0b\u9762\u7684\u4f18\u5316\u3002\u5373\u5b50\u8fdb\u7a0b\u521b\u5efa\u540e\uff0c\u7236\u5b50\u4ee5\u53ea\u8bfb\u7684\u65b9\u5f0f\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u8d44\u6e90\uff08\u5e76\u4e0d\u5305\u62ec\u7236\u8fdb\u7a0b\u7684\u9875\u8868\u9879\uff09\u3002\u5f53\u5b50\u8fdb\u7a0b\u9700\u8981\u4fee\u6539\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u7684\u67d0\u4e00\u9875\u65f6\uff0c\u624d\u4e3a\u5b50\u8fdb\u7a0b\u590d\u5236\u8be5\u9875\u3002\u91c7\u7528\u8fd9\u6837\u7684\u6280\u672f\u53ef\u4ee5\u907f\u514d\u5bf9\u7236\u8fdb\u7a0b\u4e2d\u67d0\u4e9b\u6570\u636e\u4e0d\u5fc5\u8981\u7684\u590d\u5236\u3002<\/p>\n<p>2\uff0e \u4f7f\u7528vfork\u51fd\u6570\u521b\u5efa\u7684\u5b50\u8fdb\u7a0b\u4f1a\u5b8c\u5168\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4\uff0c\u751a\u81f3\u662f\u7236\u8fdb\u7a0b\u7684\u9875\u8868\u9879\u3002\u7236\u5b50\u8fdb\u7a0b\u4efb\u610f\u4e00\u65b9\u5bf9\u4efb\u4f55\u6570\u636e\u7684\u4fee\u6539\u4f7f\u5f97\u53e6\u4e00\u65b9\u90fd\u53ef\u4ee5\u611f\u77e5\u5230\u3002\u4e3a\u4e86\u4f7f\u5f97\u53cc\u65b9\u4e0d\u53d7\u8fd9\u79cd\u5f71\u54cd\uff0cvfork\u51fd\u6570\u521b\u5efa\u4e86\u5b50\u8fdb\u7a0b\u540e\uff0c\u7236\u8fdb\u7a0b\u4fbf\u88ab\u963b\u585e\u76f4\u81f3\u5b50\u8fdb\u7a0b\u8c03\u7528\u4e86exec()\u6216exit()\u3002\u7531\u4e8e\u73b0\u5728fork\u51fd\u6570\u5f15\u5165\u4e86\u5199\u65f6\u590d\u5236\u6280\u672f\uff0c\u5728\u4e0d\u8003\u8651\u590d\u5236\u7236\u8fdb\u7a0b\u9875\u8868\u9879\u7684\u60c5\u51b5\u4e0b\uff0cvfork\u51fd\u6570\u51e0\u4e4e\u4e0d\u4f1a\u88ab\u4f7f\u7528\u3002<\/p>\n<p>3\uff0e clone\u51fd\u6570\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6\u7075\u6d3b\u5ea6\u6bd4\u8f83\u5927\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u53c2\u6570\u6765\u9009\u62e9\u6027\u7684\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u8d44\u6e90\u3002\u5177\u4f53\u53c2\u6570\u53ef\u53c2\u89c1\u88681<\/p>\n<p>\u5c31\u50cf\u4e00\u5f00\u59cb\u6240\u5206\u6790\u7684\u90a3\u6837\uff0c\u7528\u6237\u7a0b\u5e8f\u5e76\u4e0d\u76f4\u63a5\u4f7f\u7528\u7cfb\u7edf\u8c03\u7528\uff0c\u800c\u662f\u901a\u8fc7C\u5e93\u4e2d\u7684API\u3002\u800c\u7cfb\u7edf\u8c03\u7528\u5728\u5185\u6838\u4e2d\u4e5f\u5e76\u4e0d\u662f\u76f4\u63a5\u5b9e\u73b0\u7684\uff0c\u800c\u662f\u901a\u8fc7\u8c03\u7528\u5404\u81ea\u5bf9\u5e94\u7684\u670d\u52a1\u4f8b\u7a0b\u3002\u7cfb\u7edf\u8c03\u7528fork\u3001vfork\u548cclone\u5728\u5185\u6838\u4e2d\u5bf9\u5e94\u7684\u670d\u52a1\u4f8b\u7a0b\u5206\u522b\u4e3asys_fork()\uff0csys_vfork()\u548csys_clone()\u3002\u56e0\u6b64\uff0c\u60f3\u8981\u4e86\u89e3fork\u7b49\u7cfb\u7edf\u8c03\u7528\u7684\u8be6\u7ec6\u6267\u884c\u8fc7\u7a0b\uff0c\u5c31\u5fc5\u987b\u67e5\u770b\u5b83\u4eec\u6240\u5bf9\u5e94\u7684\u5185\u6838\u51fd\u6570\uff08\u4e5f\u5c31\u662f\u670d\u52a1\u4f8b\u7a0b\uff09\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002\u4e0a\u8ff0\u4e09\u4e2a\u7cfb\u7edf\u8c03\u7528\u5bf9\u5e94\u7684\u670d\u52a1\u4f8b\u7a0b\u5206\u522b\u5b9a\u4e49\u5728linux\/arch\/i386\/kernel\/process.c \u4e2d\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">asmlinkage int sys_fork(struct pt_regs regs)\r\n{\r\n\treturn do_fork(SIGCHLD, regs.esp, \u00aes, 0, NULL, NULL);\r\n}\r\n\r\nasmlinkage int sys_clone(struct pt_regs regs)\r\n{\r\n\tunsigned long clone_flags;\r\n\tunsigned long newsp;\r\n\tint __user *parent_tidptr, *child_tidptr;\r\n\r\n\tclone_flags = regs.ebx;\r\n\tnewsp = regs.ecx;\r\n\tparent_tidptr = (int __user *)regs.edx;\r\n\tchild_tidptr = (int __user *)regs.edi;\r\n\tif (!newsp)\r\n\t\tnewsp = regs.esp;\r\n\treturn do_fork(clone_flags, newsp, \u00aes, 0, parent_tidptr, child_tidptr);\r\n}\r\n\r\nasmlinkage int sys_vfork(struct pt_regs regs)\r\n{\r\n\treturn do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs.esp, \u00aes, 0, NULL, NULL);\r\n}<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230do_fork()\u5747\u88ab\u4e0a\u8ff0\u4e09\u4e2a\u670d\u52a1\u4f8b\u7a0b\u8c03\u7528\u3002\u800c\u5728do_fork()\u5185\u90e8\u53c8\u8c03\u7528\u4e86copy_process()\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u56fe2\u6765\u7406\u89e3\u4e0a\u8ff0\u7684\u8c03\u7528\u5173\u7cfb\u3002<\/p>\n<p><a rel=\"attachment wp-att-2185\" href=\"http:\/\/edsionte.com\/techblog\/archives\/2103\/1-3\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2185\" title=\"1\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/1.jpg\" alt=\"\" width=\"553\" height=\"569\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/1.jpg 553w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/12\/1-291x300.jpg 291w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/a><strong>\u00a0<\/strong><\/p>\n<p><strong>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u56fe2<\/strong><br \/>\n\u4ece\u4e0a\u9762\u7684\u5206\u6790\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u660e\u786e\u4e86do_fork()\u548ccopy_process()\u662f\u672c\u6587\u7684\u4e3b\u8981\u5206\u6790\u5bf9\u8c61\u3002\u4f46\u662f\u5728\u8fd9\u4e4b\u524d\uff0c\u6211\u4eec\u6709\u5fc5\u8981\u5148\u5206\u6790\u4e00\u4e0b\u7528\u6237\u8fdb\u7a0b\u8fdb\u5165\u7cfb\u7edf\u8c03\u7528\u7684\u8fc7\u7a0b\u3002\u8be6\u89c1\u4e0b\u6587\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a5\u4e0b\u6765\u7684\u51e0\u7bc7\u6587\u7ae0\u5c06\u4f1a\u5206\u6790fork\u7cfb\u7edf\u8c03\u7528\u5728\u5185\u6838\u4e2d\u7684\u5b9e\u73b0\uff0c\u4ee5\u4f5c\u4e3a\u672c\u5b66\u671flinux\u64cd\u4f5c\u7cfb\u7edf\u8bfe\u7a0b\u603b\u7684\u5206\u6790\u62a5\u544a\u3002\u672c\u6587\u6240 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[180,161,179],"class_list":["post-2103","post","type-post","status-publish","format-standard","hentry","category-linux","tag-fork","tag-161","tag-179"],"views":10297,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2103","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=2103"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/2103\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=2103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=2103"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=2103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}