{"id":3254,"date":"2011-09-02T18:49:30","date_gmt":"2011-09-02T10:49:30","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3254"},"modified":"2012-04-04T20:27:51","modified_gmt":"2012-04-04T12:27:51","slug":"%e8%bf%9b%e7%a8%8b%e5%9c%a8%e5%86%85%e6%a0%b8%e4%b8%ad%e7%9a%84%e8%a7%92%e8%89%b2%e6%89%ae%e6%bc%94","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3254","title":{"rendered":"\u8fdb\u7a0b\u5728Linux\u5185\u6838\u4e2d\u7684\u89d2\u8272\u626e\u6f14"},"content":{"rendered":"<p>\u5728Linux\u5185\u6838\u4e2d\uff0c\u5185\u6838\u5c06\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u4e00\u89c6\u540c\u4ec1\uff0c\u5373\u5185\u6838\u4f7f\u7528\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784task_struct\u6765\u5206\u522b\u8868\u793a\u4ed6\u4eec\uff1b\u5185\u6838\u4f7f\u7528\u76f8\u540c\u7684\u8c03\u5ea6\u7b97\u6cd5\u5bf9\u8fd9\u4e09\u8005\u8fdb\u884c\u8c03\u5ea6\uff1b\u5e76\u4e14\u5185\u6838\u4e5f\u4f7f\u7528\u540c\u4e00\u4e2a\u51fd\u6570do_fork()\u6765\u5206\u522b\u521b\u5efa\u8fd9\u4e09\u79cd\u6267\u884c\u7ebf\u7a0b\uff08thread of execution\uff09\u3002\u6267\u884c\u7ebf\u7a0b\u901a\u5e38\u662f\u6307\u4efb\u4f55\u6b63\u5728\u6267\u884c\u7684\u4ee3\u7801\u5b9e\u4f8b\uff0c\u6bd4\u5982\u4e00\u4e2a\u5185\u6838\u7ebf\u7a0b\uff0c\u4e00\u4e2a\u4e2d\u65ad\u5904\u7406\u7a0b\u5e8f\u6216\u4e00\u4e2a\u8fdb\u5165\u5185\u6838\u7684\u8fdb\u7a0b\u3002<\/p>\n<p>\u8fd9\u6837\u5904\u7406\u65e0\u7591\u662f\u7b80\u6d01\u65b9\u4fbf\u7684\uff0c\u5e76\u4e14\u5185\u6838\u5728\u7edf\u4e00\u5904\u7406\u8fd9\u4e09\u8005\u4e4b\u4f59\u5e76\u6ca1\u6709\u5931\u53bb\u4ed6\u4eec\u672c\u8eab\u6240\u5177\u6709\u7684\u7279\u6027\u3002\u672c\u6587\u5c06\u7ed3\u5408\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u7684\u7279\u6027\u6d45\u8c08\u8fdb\u7a0b\u5728\u5185\u6838\u4e2d\u7684\u89d2\u8272\u626e\u6f14\u95ee\u9898\u3002<\/p>\n<h3>1.\u8fdb\u7a0b\u63cf\u8ff0\u7b26task_struct\u7684\u591a\u89d2\u8272\u626e\u6f14<\/h3>\n<p>\u4e0a\u8ff0\u4e09\u79cd\u6267\u884c\u7ebf\u7a0b\u5728\u5185\u6838\u4e2d\u90fd\u4f7f\u7528\u7edf\u4e00\u7684\u6570\u636e\u7ed3\u6784task_struct\u6765\u8868\u793a\u3002task_struct\u7ed3\u6784\u5373\u6240\u8c13\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff0c\u5b83\u5305\u542b\u4e86\u4e0e\u4e00\u4e2a\u8fdb\u7a0b\u76f8\u5173\u7684\u6240\u6709\u4fe1\u606f\u3002\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u4e0d\u4ec5\u5305\u542b\u4e86\u8bb8\u591a\u63cf\u8ff0\u8fdb\u7a0b\u5c5e\u6027\u7684\u5b57\u6bb5\uff0c\u800c\u4e14\u8fd8\u6709\u4e00\u7cfb\u5217\u6307\u5411\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u7684\u6307\u9488\u3002\u4e0b\u9762\u5c06\u7b80\u5355\u4ecb\u7ecd\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u51e0\u4e2a\u6bd4\u8f83\u7279\u6b8a\u7684\u5b57\u6bb5\uff0c\u5b83\u4eec\u5206\u522b\u6307\u5411\u4ee3\u8868\u8fdb\u7a0b\u6240\u62e5\u6709\u7684\u8d44\u6e90\u7684\u6570\u636e\u7ed3\u6784\u3002<\/p>\n<p><strong>mm\u5b57\u6bb5<\/strong>\uff1a\u6307\u5411mm_struct\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7c7b\u578b\u7528\u6765\u63cf\u8ff0\u8fdb\u7a0b\u6574\u4e2a\u7684\u865a\u62df\u5730\u5740\u7a7a\u95f4\u3002<\/p>\n<p><strong>fs\u5b57\u6bb5<\/strong>\uff1a\u6307\u5411fs_struct\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7c7b\u578b\u7528\u6765\u63cf\u8ff0\u8fdb\u7a0b\u6240\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u548c\u5f53\u524d\u8fdb\u7a0b\u6240\u5728\u7684\u76ee\u5f55\u4fe1\u606f\u3002<\/p>\n<p><strong>files\u5b57\u6bb5<\/strong>\uff1a\u6307\u5411files_struct\u7ed3\u6784\u7684\u6307\u9488\uff0c\u8be5\u7c7b\u578b\u7528\u6765\u63cf\u8ff0\u5f53\u524d\u8fdb\u7a0b\u6240\u6253\u5f00\u6587\u4ef6\u7684\u4fe1\u606f\u3002<\/p>\n<p><strong>signal\u5b57\u6bb5<\/strong>\uff1a\u6307\u5411signal_struct\u7ed3\u6784\uff08\u4fe1\u53f7\u63cf\u8ff0\u7b26\uff09\u7684\u6307\u9488\uff0c\u8be5\u7c7b\u578b\u7528\u6765\u63cf\u8ff0\u8fdb\u7a0b\u6240\u80fd\u5904\u7406\u7684\u4fe1\u53f7\u3002<\/p>\n<p>\u5bf9\u4e8e\u666e\u901a\u8fdb\u7a0b\u6765\u8bf4\uff0c\u4e0a\u8ff0\u5b57\u6bb5\u5206\u522b\u6307\u5411\u5177\u4f53\u7684\u6570\u636e\u7ed3\u6784\u4ee5\u8868\u793a\u8be5\u8fdb\u7a0b\u6240\u62e5\u6709\u7684\u8d44\u6e90\u3002<\/p>\n<p>\u5bf9\u5e94\u6bcf\u4e2a\u7ebf\u7a0b\u800c\u8a00\uff0c\u5185\u6838\u901a\u8fc7\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u4e0e\u5176\u8fdb\u884c\u5173\u8054\u3002\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u4e4b\u6240\u8f7b\u91cf\uff0c\u662f\u56e0\u4e3a\u5b83\u4e0e\u5176\u4ed6\u8fdb\u7a0b\u5171\u4eab\u4e0a\u8ff0\u6240\u63d0\u53ca\u7684\u8fdb\u7a0b\u8d44\u6e90\u3002\u6bd4\u5982\u8fdb\u7a0bA\u521b\u5efa\u4e86\u7ebf\u7a0bB\uff0c\u5219B\u7ebf\u7a0b\u4f1a\u5728\u5185\u6838\u4e2d\u5bf9\u5e94\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u3002\u8fd9\u4e2a\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u5f88\u81ea\u7136\u7684\u5bf9\u5e94\u4e00\u4e2a\u8fdb\u7a0b\u63cf\u8ff0\u7b26\uff0c\u53ea\u4e0d\u8fc7B\u7ebf\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u7684\u67d0\u4e9b\u4ee3\u8868\u8d44\u6e90\u6307\u9488\u4f1a\u548cA\u8fdb\u7a0b\u4e2d\u5bf9\u5e94\u7684\u5b57\u6bb5\u6307\u5411\u540c\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u6837\u5c31\u5b9e\u73b0\u4e86\u591a\u7ebf\u7a0b\u4e4b\u95f4\u7684\u8d44\u6e90\u5171\u4eab\u3002<\/p>\n<p>\u7531\u4e8e\u5185\u6838\u7ebf\u7a0b\u53ea\u8fd0\u884c\u5728\u5185\u6838\u6001\uff0c\u5e76\u4e14\u53ea\u80fd\u7531\u5176\u4ed6\u5185\u6838\u7ebf\u7a0b\u521b\u5efa\uff0c\u6240\u4ee5\u5185\u6838\u7ebf\u7a0b\u5e76\u4e0d\u9700\u8981\u548c\u666e\u901a\u8fdb\u7a0b\u90a3\u6837\u7684\u72ec\u7acb\u5730\u5740\u7a7a\u95f4\u3002\u56e0\u6b64\u5185\u6838\u7ebf\u7a0b\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u7684mm\u6307\u9488\u5373\u4e3aNULL\u3002\u5185\u6838\u7ebf\u7a0b\u662f\u5426\u5171\u4eab\u7236\u5185\u6838\u7ebf\u7a0b\u7684\u67d0\u4e9b\u8d44\u6e90\uff0c\u5219\u901a\u8fc7\u5411\u5185\u6838\u7ebf\u7a0b\u521b\u5efa\u51fd\u6570kernel_thread()\u4f20\u9012\u53c2\u6570\u6765\u51b3\u5b9a\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u9762\u7684\u5206\u6790\u53ef\u4ee5\u53d1\u73b0\uff0c\u5185\u6838\u4e2d\u4f7f\u7528\u7edf\u4e00\u7684\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u6765\u8868\u793a\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\uff0c\u6839\u636e\u4ed6\u4eec\u4e0d\u540c\u7684\u7279\u6027\uff0c\u5176\u8fdb\u7a0b\u63cf\u8ff0\u7b26\u4e2d\u67d0\u4e9b\u4ee3\u8868\u8d44\u6e90\u7684\u5b57\u6bb5\u7684\u6307\u5411\u4f1a\u6709\u6240\u4e0d\u540c\uff0c\u4ee5\u5b9e\u73b0\u626e\u6f14\u4e0d\u540c\u89d2\u8272\u3002<\/p>\n<h3>2. do_fork()\u7684\u591a\u89d2\u8272\u626e\u6f14<\/h3>\n<p>\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u4ee5\u53ca\u5185\u6838\u7ebf\u7a0b\u90fd\u6709\u5bf9\u5e94\u7684\u521b\u5efa\u51fd\u6570\uff0c\u4e0d\u8fc7\u8fd9\u4e09\u8005\u6240\u5bf9\u5e94\u7684\u521b\u5efa\u51fd\u6570\u6700\u7ec8\u5728\u5185\u6838\u90fd\u662f\u7531do_fork()\u8fdb\u884c\u521b\u5efa\u7684\uff0c\u5177\u4f53\u7684\u8c03\u7528\u5173\u7cfb\u56fe\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/edsionte.com\/techblog\/archives\/3254\/proc_thread_colar\" rel=\"attachment wp-att-3903\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-3903\" title=\"proc_thread_colar\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/09\/proc_thread_colar.jpeg\" alt=\"\" width=\"400\" height=\"437\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/09\/proc_thread_colar.jpeg 666w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/09\/proc_thread_colar-274x300.jpg 274w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>\u4ece\u56fe\u4e2d\u53ef\u4ee5\u770b\u51fa\uff0c\u5185\u6838\u4e2d\u521b\u5efa\u8fdb\u7a0b\u7684\u6838\u5fc3\u51fd\u6570\u5373\u4e3a\u770bdo_fork()\uff0c\u8be5\u51fd\u6570\u7684\u539f\u578b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">long do_fork(unsigned long clone_flags,\r\n               unsigned long stack_start,\r\n               struct pt_regs *regs,\r\n               unsigned long stack_size,\r\n               int __user *parent_tidptr,\r\n               int __user *child_tidptr)<\/pre>\n<p>\u8be5\u51fd\u6570\u7684\u53c2\u6570\u4e2a\u6570\u662f\u56fa\u5b9a\u7684\uff0c\u6bcf\u4e2a\u53c2\u6570\u7684\u529f\u80fd\u5982\u4e0b\uff1a<\/p>\n<p><strong>clone_flags<\/strong>\uff1a\u4ee3\u8868\u8fdb\u7a0b\u5404\u79cd\u7279\u6027\u7684\u6807\u5fd7\u3002\u4f4e\u5b57\u8282\u6307\u5b9a\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u53d1\u9001\u7ed9\u7236\u8fdb\u7a0b\u7684\u4fe1\u53f7\u4ee3\u7801\uff0c\u4e00\u822c\u4e3aSIGCHLD\u4fe1\u53f7\uff0c\u5269\u4f59\u4e09\u4e2a\u5b57\u8282\u662f\u82e5\u5e72\u4e2a\u6807\u5fd7\u6216\u8fd0\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n<p><strong>stack_start<\/strong>\uff1a\u5b50\u8fdb\u7a0b\u7528\u6237\u6001\u5806\u6808\u7684\u6307\u9488\uff0c\u8be5\u53c2\u6570\u4f1a\u88ab\u8d4b\u503c\u7ed9\u5b50\u8fdb\u7a0b\u7684esp\u5bc4\u5b58\u5668\u3002<\/p>\n<p><strong>regs<\/strong>\uff1a\u6307\u5411\u901a\u7528\u5bc4\u5b58\u5668\u503c\u7684\u6307\u9488\uff0c\u5f53\u8fdb\u7a0b\u4ece\u7528\u6237\u6001\u5207\u6362\u5230\u5185\u6838\u6001\u65f6\u901a\u7528\u5bc4\u5b58\u5668\u4e2d\u7684\u503c\u4f1a\u88ab\u4fdd\u5b58\u5230\u5185\u6838\u6001\u5806\u6808\u4e2d\u3002<\/p>\n<p><strong>stack_size<\/strong>\uff1a\u672a\u88ab\u4f7f\u7528\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u3002<\/p>\n<p><strong>parent_tidptr<\/strong>\uff1a\u8be5\u5b50\u8fdb\u7a0b\u7684\u7236\u8fdb\u7a0b\u7528\u6237\u6001\u53d8\u91cf\u7684\u5730\u5740\uff0c\u4ec5\u5f53CLONE_PARENT_SETTID\u88ab\u8bbe\u7f6e\u65f6\u6709\u6548\u3002<\/p>\n<p><strong>child_tidptr<\/strong>\uff1a\u8be5\u5b50\u8fdb\u7a0b\u7528\u6237\u6001\u53d8\u91cf\u7684\u5730\u5740\uff0c\u4ec5\u5f53CLONE_CHILD_SETTID\u88ab\u8bbe\u7f6e\u65f6\u6709\u6548\u3002<\/p>\n<p>\u65e2\u7136\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u5728\u5185\u6838\u4e2d\u90fd\u662f\u901a\u8fc7do_fork()\u5b8c\u6210\u521b\u5efa\u7684\uff0c\u90a3\u4e48do_fork()\u662f\u5982\u4f55\u4f53\u73b0\u5176\u529f\u80fd\u7684\u591a\u6837\u6027\uff1f\u5176\u5b9e\uff0cclone_flags\u53c2\u6570\u5728\u8fd9\u91cc\u8d77\u5230\u4e86\u5173\u952e\u4f5c\u7528\uff0c\u901a\u8fc7\u9009\u53d6\u4e0d\u540c\u7684\u6807\u5fd7\uff0c\u4ece\u800c\u4fdd\u8bc1\u4e86do_fork()\u51fd\u6570\u5b9e\u73b0\u591a\u89d2\u8272\u2014\u2014\u521b\u5efa\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u2014\u2014\u529f\u80fd\u7684\u5b9e\u73b0\u3002clone_flags\u53c2\u6570\u53ef\u53d6\u7684\u6807\u5fd7\u5f88\u591a\uff0c\u4e0b\u9762\u53ea\u4ecb\u7ecd\u51e0\u4e2a\u4e0e\u672c\u6587\u76f8\u5173\u7684\u6807\u5fd7\u3002<\/p>\n<p><strong>CLONE_VIM<\/strong>\uff1a\u5b50\u8fdb\u7a0b\u5171\u4eab\u7236\u8fdb\u7a0b\u5185\u5b58\u63cf\u8ff0\u7b26\u548c\u6240\u6709\u7684\u9875\u8868\u3002<\/p>\n<p><strong>CLONE_FS<\/strong>\uff1a\u5b50\u8fdb\u7a0b\u5171\u4eab\u7236\u8fdb\u7a0b\u6240\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u548c\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u3002<\/p>\n<p><strong>CLONE_FILES<\/strong>\uff1a\u5b50\u8fdb\u7a0b\u5171\u4eab\u7236\u8fdb\u7a0b\u6253\u5f00\u7684\u6587\u4ef6\u3002<\/p>\n<p><strong>CLONE_SIGHAND<\/strong>\uff1a\u5b50\u8fdb\u7a0b\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u4fe1\u53f7\u5904\u7406\u7a0b\u5e8f\u3001\u963b\u585e\u4fe1\u53f7\u548c\u6302\u8d77\u7684\u4fe1\u53f7\u3002\u4f7f\u7528\u8be5\u6807\u5fd7\u5fc5\u987b\u540c\u65f6\u8bbe\u7f6eCLONE_VM\u6807\u5fd7\u3002<\/p>\n<p>\u5982\u679c\u521b\u5efa\u5b50\u8fdb\u7a0b\u65f6\u8bbe\u7f6e\u4e86\u4e0a\u8ff0\u6807\u5fd7\uff0c\u90a3\u4e48\u5b50\u8fdb\u7a0b\u4f1a\u5171\u4eab\u8fd9\u4e9b\u6807\u5fd7\u6240\u4ee3\u8868\u7684\u7236\u8fdb\u7a0b\u8d44\u6e90\u3002<\/p>\n<h4>2.1 \u8fdb\u7a0b\u7684\u521b\u5efa<\/h4>\n<p>\u5728\u7528\u6237\u6001\u7a0b\u5e8f\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7fork()\u3001vfork()\u548cclone()\u4e09\u4e2a\u63a5\u53e3\u51fd\u6570\u521b\u5efa\u8fdb\u7a0b\uff0c\u8fd9\u4e09\u4e2a\u51fd\u6570\u5728\u5e93\u4e2d\u5206\u522b\u5bf9\u5e94\u540c\u540d\u7684\u7cfb\u7edf\u8c03\u7528\u3002\u7cfb\u7edf\u8c03\u7528\u51fd\u6570\u901a\u8fc7128\u53f7\u8f6f\u4e2d\u65ad\u8fdb\u5165\u5185\u6838\u540e\uff0c\u4f1a\u8c03\u7528\u76f8\u5e94\u7684\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\u3002\u8fd9\u4e09\u4e2a\u51fd\u6570\u5bf9\u5e94\u7684\u670d\u52a1\u5386\u7a0b\u5206\u522b\u662fsys_fork()\u3001sys_vfork()\u548csys_clone()\u3002<\/p>\n<pre class=\"brush:c\"> int sys_fork(struct pt_regs *regs)\r\n {\r\n         return do_fork(SIGCHLD, regs-&gt;sp, regs, 0, NULL, NULL);\r\n }\r\n\r\n int sys_vfork(struct pt_regs *regs)\r\n {\r\n         return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs-&gt;sp, regs, 0,\r\n                        NULL, NULL);\r\n }\r\n\r\n long\r\n sys_clone(unsigned long clone_flags, unsigned long newsp,\r\n           void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)\r\n {\r\n         if (!newsp)\r\n                 newsp = regs-&gt;sp;\r\n         return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);\r\n }<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u7cfb\u7edf\u8c03\u7528\u670d\u52a1\u4f8b\u7a0b\u7684\u6e90\u7801\u53ef\u4ee5\u53d1\u73b0\uff0c\u4e09\u4e2a\u670d\u52a1\u5386\u7a0b\u5185\u90e8\u90fd\u8c03\u7528\u4e86do_fork()\uff0c\u53ea\u4e0d\u8fc7\u5dee\u522b\u5728\u4e8e\u7b2c\u4e00\u4e2a\u53c2\u6570\u6240\u4f20\u7684\u503c\u4e0d\u540c\u3002\u8fd9\u4e5f\u6b63\u597d\u5bfc\u81f4\u7531\u8fd9\u4e09\u4e2a\u8fdb\u7a0b\u521b\u5efa\u51fd\u6570\u6240\u521b\u5efa\u7684\u8fdb\u7a0b\u6709\u4e0d\u540c\u7684\u7279\u6027\u3002\u4e0b\u9762\u5bf9\u6bcf\u79cd\u8fdb\u7a0b\u4f5c\u4ee5\u7b80\u5355\u8bf4\u660e\u3002<\/p>\n<p><strong>fork()<\/strong>\uff1a\u7531\u4e8edo_fork()\u4e2dclone_flags\u53c2\u6570\u9664\u4e86\u5b50\u8fdb\u7a0b\u7ed3\u675f\u65f6\u8fd4\u56de\u7ed9\u7236\u8fdb\u7a0b\u7684SIGCHLD\u4fe1\u53f7\u5916\u5e76\u65e0\u5176\u4ed6\u7279\u6027\u6807\u5fd7\uff0c\u56e0\u6b64\u7531fork()\u521b\u5efa\u7684\u8fdb\u7a0b\u4e0d\u4f1a\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u4efb\u4f55\u8d44\u6e90\u3002\u5b50\u8fdb\u7a0b\u4f1a\u5b8c\u5168\u590d\u5236\u7236\u8fdb\u7a0b\u7684\u8d44\u6e90\uff0c\u4e5f\u5c31\u662f\u8bf4\u7236\u5b50\u8fdb\u7a0b\u76f8\u5bf9\u72ec\u7acb\u3002\u4e0d\u8fc7\u7531\u4e8e\u5199\u65f6\u590d\u5236\u6280\u672f\uff08Copy On Write\uff0cCOW\uff09\u7684\u5f15\u5165\uff0c\u5b50\u8fdb\u7a0b\u53ef\u4ee5\u53ea\u8bfb\u7236\u8fdb\u7a0b\u7684\u7269\u7406\u9875\uff0c\u53ea\u6709\u5f53\u4e24\u8005\u4e4b\u4e00\u53bb\u5199\u67d0\u4e2a\u7269\u7406\u9875\u65f6\uff0c\u5185\u6838\u6b64\u65f6\u624d\u4f1a\u5c06\u8fd9\u4e2a\u9875\u7684\u5185\u5bb9\u62f7\u8d1d\u5230\u4e00\u4e2a\u65b0\u7684\u7269\u7406\u9875\uff0c\u5e76\u628a\u8fd9\u4e2a\u65b0\u7684\u7269\u7406\u9875\u5206\u914d\u7ed9\u6b63\u5728\u5199\u7684\u8fdb\u7a0b\u3002<\/p>\n<p><strong>vfork()<\/strong>\uff1ado_fork()\u4e2d\u7684clone_flags\u4f7f\u7528\u4e86CLONE_VFORK\u548cCLONE_VM\u4e24\u4e2a\u6807\u5fd7\u3002CLONE_VFORK\u6807\u5fd7\u4f7f\u5f97\u5b50\u8fdb\u7a0b\u5148\u4e8e\u7236\u8fdb\u7a0b\u6267\u884c\uff0c\u7236\u8fdb\u7a0b\u4f1a\u963b\u585e\u5230\u5b50\u8fdb\u7a0b\u7ed3\u675f\u6216\u6267\u884c\u65b0\u7684\u7a0b\u5e8f\u3002CLONE_VM\u6807\u5fd7\u4f7f\u5f97\u5b50\u8fdb\u7a0b\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u5730\u5740\u7a7a\u95f4\uff08\u7236\u8fdb\u7a0b\u7684\u9875\u8868\u9879\u9664\u5916\uff09\u3002\u5728COW\u6280\u672f\u5f15\u5165\u4e4b\u524d\uff0cvfork()\u9002\u7528\u5b50\u8fdb\u7a0b\u5f62\u6210\u540e\u7acb\u9a6c\u6267\u884cexecv()\u7684\u60c5\u5f62\u3002\u56e0\u6b64\uff0cvfork()\u73b0\u5982\u4eca\u5df2\u7ecf\u6ca1\u6709\u7279\u522b\u7684\u4f7f\u7528\u4e4b\u5904\uff0c\u56e0\u4e3a\u5199\u5b9e\u590d\u5236\u6280\u672f\u5b8c\u5168\u53ef\u4ee5\u53d6\u4ee3\u5b83\u521b\u5efa\u8fdb\u7a0b\u65f6\u6240\u5e26\u6765\u7684\u9ad8\u6548\u6027\u3002<\/p>\n<p><strong>clone()<\/strong>\uff1aclone\u901a\u5e38\u7528\u4e8e\u521b\u5efa\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u3002\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u6807\u5fd7\u53ef\u4ee5\u5bf9\u7236\u5b50\u8fdb\u7a0b\u4e4b\u95f4\u6570\u636e\u7684\u5171\u4eab\u548c\u590d\u5236\u4f5c\u7cbe\u786e\u7684\u63a7\u5236\uff0c\u4e00\u822cflags\u7684\u53d6\u503c\u4e3aCLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND\u3002\u7531\u4e0a\u8ff0\u6807\u5fd7\u53ef\u4ee5\u770b\u5230\uff0c\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\u901a\u5e38\u5171\u4eab\u7236\u8fdb\u7a0b\u7684\u5185\u5b58\u5730\u5740\u7a7a\u95f4\u3001\u7236\u8fdb\u7a0b\u6240\u5728\u6587\u4ef6\u7cfb\u7edf\u7684\u6839\u76ee\u5f55\u4ee5\u53ca\u5de5\u4f5c\u76ee\u5f55\u4fe1\u606f\u3001\u7236\u8fdb\u7a0b\u5f53\u524d\u6253\u5f00\u7684\u6587\u4ef6\u4ee5\u53ca\u7236\u8fdb\u7a0b\u6240\u62e5\u6709\u7684\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u3002<\/p>\n<h4>2.2 \u7ebf\u7a0b\u7684\u521b\u5efa<\/h4>\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u5728\u5185\u6838\u4e2d\u5bf9\u5e94\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff0c\u4e24\u8005\u7684\u5173\u8054\u662f\u901a\u8fc7\u7ebf\u7a0b\u5e93\u5b8c\u6210\u7684\u3002\u56e0\u6b64\u901a\u8fc7pthread_create()\u521b\u5efa\u7684\u7ebf\u7a0b\u6700\u7ec8\u5728\u5185\u6838\u4e2d\u662f\u901a\u8fc7clone()\u5b8c\u6210\u521b\u5efa\u7684\uff0c\u800cclone()\u6700\u7ec8\u8c03\u7528do_fork()\u3002<\/p>\n<h4>2.3 \u5185\u6838\u7ebf\u7a0b\u7684\u521b\u5efa<\/h4>\n<p>\u4e00\u4e2a\u65b0\u5185\u6838\u7ebf\u7a0b\u7684\u521b\u5efa\u662f\u901a\u8fc7\u5728\u73b0\u6709\u7684\u5185\u6838\u7ebf\u7a0b\u4e2d\u4f7f\u7528kernel_thread()\u800c\u521b\u5efa\u7684\uff0c\u5176\u672c\u8d28\u4e5f\u662f\u5411do_fork()\u63d0\u4f9b\u7279\u5b9a\u7684flags\u6807\u5fd7\u800c\u521b\u5efa\u7684\u3002<\/p>\n<pre class=\"brush:c\"> int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)\r\n {\r\n        \/*some register operations*\/\r\n         return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &amp;regs, 0, NULL, NULL);\r\n }<\/pre>\n<p>\u4ece\u4e0a\u9762\u7684\u7ec4\u5408\u7684flag\u53ef\u4ee5\u770b\u51fa\uff0c\u65b0\u7684\u5185\u6838\u7ebf\u7a0b\u81f3\u5c11\u4f1a\u5171\u4eab\u7236\u5185\u6838\u7ebf\u7a0b\u7684\u5185\u5b58\u5730\u5740\u7a7a\u95f4\u3002\u8fd9\u6837\u505a\u5176\u5b9e\u662f\u4e3a\u4e86\u907f\u514d\u8d4b\u503c\u8c03\u7528\u7ebf\u7a0b\u7684\u9875\u8868\uff0c\u56e0\u4e3a\u5185\u6838\u7ebf\u7a0b\u65e0\u8bba\u5982\u4f55\u90fd\u4e0d\u4f1a\u8bbf\u95ee\u7528\u6237\u5730\u5740\u7a7a\u95f4\u3002CLONE_UNTRACED\u6807\u5fd7\u4fdd\u8bc1\u5185\u6838\u7ebf\u7a0b\u4e0d\u4f1a\u88ab\u4efb\u4f55\u8fdb\u7a0b\u6240\u8ddf\u8e2a\uff0c<\/p>\n<h3>3. \u8fdb\u7a0b\u7684\u8c03\u5ea6<\/h3>\n<p>\u7531\u4e8e\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u4f7f\u7528\u7edf\u4e00\u6570\u636e\u7ed3\u6784\u6765\u8868\u793a\uff0c\u56e0\u6b64\u5185\u6838\u5bf9\u8fd9\u4e09\u8005\u5e76\u4e0d\u4f5c\u533a\u5206\uff0c\u4e5f\u4e0d\u4f1a\u4e3a\u5176\u4e2d\u67d0\u4e00\u4e2a\u8bbe\u7acb\u5355\u72ec\u7684\u8c03\u5ea6\u7b97\u6cd5\u3002\u5185\u6838\u5c06\u8fd9\u4e09\u8005\u4e00\u89c6\u540c\u4ec1\uff0c\u8fdb\u884c\u7edf\u4e00\u7684\u8c03\u5ea6\u3002<\/p>\n<p><strong>\u53c2\u8003\u8d44\u6599\uff1a<\/strong><\/p>\n<p><strong>1. \u6df1\u5165\u7406\u89e3Linux\u5185\u6838<\/strong><\/p>\n<p><strong>2. Linux\u5185\u6838\u8bbe\u8ba1\u4e0e\u5b9e\u73b0<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u5185\u6838\u4e2d\uff0c\u5185\u6838\u5c06\u8fdb\u7a0b\u3001\u7ebf\u7a0b\u548c\u5185\u6838\u7ebf\u7a0b\u4e00\u89c6\u540c\u4ec1\uff0c\u5373\u5185\u6838\u4f7f\u7528\u552f\u4e00\u7684\u6570\u636e\u7ed3\u6784task_struct\u6765\u5206\u522b\u8868 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[184,170,118,183,182],"class_list":["post-3254","post","type-post","status-publish","format-standard","hentry","category-linuxc","tag-task_struct","tag-170","tag-118","tag-183","tag-182"],"views":26667,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3254","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=3254"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3254\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3254"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}