{"id":9276,"date":"2024-04-26T14:52:22","date_gmt":"2024-04-26T06:52:22","guid":{"rendered":"\/?p=9276"},"modified":"2024-04-26T15:19:47","modified_gmt":"2024-04-26T07:19:47","slug":"%e8%bf%9b%e7%a8%8b%e4%b8%8e%e7%ba%bf%e7%a8%8b%e7%9a%84%e4%b8%80%e4%b8%aa%e7%ae%80%e5%8d%95%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"\/?p=9276","title":{"rendered":"\u8fdb\u7a0b\u4e0e\u7ebf\u7a0b"},"content":{"rendered":"<h2>\u6982\u5ff5<\/h2>\n<p><a href=\"https:\/\/zh.wikipedia.org\/zh-cn\/\u8fdb\u7a0b\">\u8fdb\u7a0b<\/a>\uff08process\uff09\u548c<a href=\"https:\/\/zh.wikipedia.org\/zh-cn\/\u7ebf\u7a0b\">\u7ebf\u7a0b<\/a>\uff08thread\uff09\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4f46\u662f\u5b83\u4eec\u6bd4\u8f83\u62bd\u8c61\uff0c\u4e0d\u5bb9\u6613\u638c\u63e1\u3002<\/p>\n<p>\u6700\u8fd1\uff0c\u6211\u8bfb\u5230\u4e00\u7bc7<a href=\"http:\/\/www.qnx.com\/developers\/docs\/6.4.1\/neutrino\/getting_started\/s1_procs.html\">\u6750\u6599<\/a>\uff0c\u53d1\u73b0\u6709\u4e00\u4e2a\u5f88\u597d\u7684\u7c7b\u6bd4\uff0c\u53ef\u4ee5\u628a\u5b83\u4eec\u89e3\u91ca\u5730\u6e05\u6670\u6613\u61c2\u3002<\/p>\n<p>1.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042401.jpg\" alt=\"img\" \/><\/p>\n<p>\u8ba1\u7b97\u673a\u7684\u6838\u5fc3\u662fCPU\uff0c\u5b83\u627f\u62c5\u4e86\u6240\u6709\u7684\u8ba1\u7b97\u4efb\u52a1\u3002\u5b83\u5c31\u50cf\u4e00\u5ea7\u5de5\u5382\uff0c\u65f6\u523b\u5728\u8fd0\u884c\u3002<\/p>\n<p>2.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042402.png\" alt=\"img\" \/><\/p>\n<p>\u5047\u5b9a\u5de5\u5382\u7684\u7535\u529b\u6709\u9650\uff0c\u4e00\u6b21\u53ea\u80fd\u4f9b\u7ed9\u4e00\u4e2a\u8f66\u95f4\u4f7f\u7528\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u4e00\u4e2a\u8f66\u95f4\u5f00\u5de5\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u8f66\u95f4\u90fd\u5fc5\u987b\u505c\u5de5\u3002\u80cc\u540e\u7684\u542b\u4e49\u5c31\u662f\uff0c\u5355\u4e2aCPU\u4e00\u6b21\u53ea\u80fd\u8fd0\u884c\u4e00\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>3.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042403.jpg\" alt=\"img\" \/><\/p>\n<p>\u8fdb\u7a0b\u5c31\u597d\u6bd4\u5de5\u5382\u7684\u8f66\u95f4\uff0c\u5b83\u4ee3\u8868CPU\u6240\u80fd\u5904\u7406\u7684\u5355\u4e2a\u4efb\u52a1\u3002\u4efb\u4e00\u65f6\u523b\uff0cCPU\u603b\u662f\u8fd0\u884c\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5176\u4ed6\u8fdb\u7a0b\u5904\u4e8e\u975e\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n<p>4.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042404.jpg\" alt=\"img\" \/><\/p>\n<p>\u4e00\u4e2a\u8f66\u95f4\u91cc\uff0c\u53ef\u4ee5\u6709\u5f88\u591a\u5de5\u4eba\u3002\u4ed6\u4eec\u534f\u540c\u5b8c\u6210\u4e00\u4e2a\u4efb\u52a1\u3002<\/p>\n<p>5.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042405.jpg\" alt=\"img\" \/><\/p>\n<p>\u7ebf\u7a0b\u5c31\u597d\u6bd4\u8f66\u95f4\u91cc\u7684\u5de5\u4eba\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u5305\u62ec\u591a\u4e2a\u7ebf\u7a0b\u3002<\/p>\n<p>6.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042406.png\" alt=\"img\" \/><\/p>\n<p>\u8f66\u95f4\u7684\u7a7a\u95f4\u662f\u5de5\u4eba\u4eec\u5171\u4eab\u7684\uff0c\u6bd4\u5982\u8bb8\u591a\u623f\u95f4\u662f\u6bcf\u4e2a\u5de5\u4eba\u90fd\u53ef\u4ee5\u8fdb\u51fa\u7684\u3002\u8fd9\u8c61\u5f81\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\u662f\u5171\u4eab\u7684\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u5171\u4eab\u5185\u5b58\u3002<\/p>\n<p>7.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042407.jpg\" alt=\"img\" \/><\/p>\n<p>\u53ef\u662f\uff0c\u6bcf\u95f4\u623f\u95f4\u7684\u5927\u5c0f\u4e0d\u540c\uff0c\u6709\u4e9b\u623f\u95f4\u6700\u591a\u53ea\u80fd\u5bb9\u7eb3\u4e00\u4e2a\u4eba\uff0c\u6bd4\u5982\u5395\u6240\u3002\u91cc\u9762\u6709\u4eba\u7684\u65f6\u5019\uff0c\u5176\u4ed6\u4eba\u5c31\u4e0d\u80fd\u8fdb\u53bb\u4e86\u3002\u8fd9\u4ee3\u8868\u4e00\u4e2a\u7ebf\u7a0b\u4f7f\u7528\u67d0\u4e9b\u5171\u4eab\u5185\u5b58\u65f6\uff0c\u5176\u4ed6\u7ebf\u7a0b\u5fc5\u987b\u7b49\u5b83\u7ed3\u675f\uff0c\u624d\u80fd\u4f7f\u7528\u8fd9\u4e00\u5757\u5185\u5b58\u3002<\/p>\n<p>8.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042408.jpg\" alt=\"img\" \/><\/p>\n<p>\u4e00\u4e2a\u9632\u6b62\u4ed6\u4eba\u8fdb\u5165\u7684\u7b80\u5355\u65b9\u6cd5\uff0c\u5c31\u662f\u95e8\u53e3\u52a0\u4e00\u628a\u9501\u3002\u5148\u5230\u7684\u4eba\u9501\u4e0a\u95e8\uff0c\u540e\u5230\u7684\u4eba\u770b\u5230\u4e0a\u9501\uff0c\u5c31\u5728\u95e8\u53e3\u6392\u961f\uff0c\u7b49\u9501\u6253\u5f00\u518d\u8fdb\u53bb\u3002\u8fd9\u5c31\u53eb<a href=\"https:\/\/zh.wikipedia.org\/wiki\/\u4e92\u65a5\u9501\">&quot;\u4e92\u65a5\u9501&quot;<\/a>\uff08Mutual exclusion\uff0c\u7f29\u5199 Mutex\uff09\uff0c\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8bfb\u5199\u67d0\u4e00\u5757\u5185\u5b58\u533a\u57df\u3002<\/p>\n<p>9.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042409.jpg\" alt=\"img\" \/><\/p>\n<p>\u8fd8\u6709\u4e9b\u623f\u95f4\uff0c\u53ef\u4ee5\u540c\u65f6\u5bb9\u7eb3n\u4e2a\u4eba\uff0c\u6bd4\u5982\u53a8\u623f\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4eba\u6570\u5927\u4e8en\uff0c\u591a\u51fa\u6765\u7684\u4eba\u53ea\u80fd\u5728\u5916\u9762\u7b49\u7740\u3002\u8fd9\u597d\u6bd4\u67d0\u4e9b\u5185\u5b58\u533a\u57df\uff0c\u53ea\u80fd\u4f9b\u7ed9\u56fa\u5b9a\u6570\u76ee\u7684\u7ebf\u7a0b\u4f7f\u7528\u3002<\/p>\n<p>10.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042410.jpg\" alt=\"img\" \/><\/p>\n<p>\u8fd9\u65f6\u7684\u89e3\u51b3\u65b9\u6cd5\uff0c\u5c31\u662f\u5728\u95e8\u53e3\u6302n\u628a\u94a5\u5319\u3002\u8fdb\u53bb\u7684\u4eba\u5c31\u53d6\u4e00\u628a\u94a5\u5319\uff0c\u51fa\u6765\u65f6\u518d\u628a\u94a5\u5319\u6302\u56de\u539f\u5904\u3002\u540e\u5230\u7684\u4eba\u53d1\u73b0\u94a5\u5319\u67b6\u7a7a\u4e86\uff0c\u5c31\u77e5\u9053\u5fc5\u987b\u5728\u95e8\u53e3\u6392\u961f\u7b49\u7740\u4e86\u3002\u8fd9\u79cd\u505a\u6cd5\u53eb\u505a<a href=\"https:\/\/en.wikipedia.org\/wiki\/Semaphore_(programming)\">&quot;\u4fe1\u53f7\u91cf&quot;<\/a>\uff08Semaphore\uff09\uff0c\u7528\u6765\u4fdd\u8bc1\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u4f1a\u4e92\u76f8\u51b2\u7a81\u3002<\/p>\n<p>\u4e0d\u96be\u770b\u51fa\uff0cmutex\u662fsemaphore\u7684\u4e00\u79cd\u7279\u6b8a\u60c5\u51b5\uff08n=1\u65f6\uff09\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b8c\u5168\u53ef\u4ee5\u7528\u540e\u8005\u66ff\u4ee3\u524d\u8005\u3002\u4f46\u662f\uff0c\u56e0\u4e3amutex\u8f83\u4e3a\u7b80\u5355\uff0c\u4e14\u6548\u7387\u9ad8\uff0c\u6240\u4ee5\u5728\u5fc5\u987b\u4fdd\u8bc1\u8d44\u6e90\u72ec\u5360\u7684\u60c5\u51b5\u4e0b\uff0c\u8fd8\u662f\u91c7\u7528\u8fd9\u79cd\u8bbe\u8ba1\u3002<\/p>\n<p>11.<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/bg2013042411.png\" alt=\"img\" \/><\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u7684\u8bbe\u8ba1\uff0c\u56e0\u6b64\u53ef\u4ee5\u5f52\u7ed3\u4e3a\u4e09\u70b9\uff1a<\/p>\n<p>\uff081\uff09\u4ee5\u591a\u8fdb\u7a0b\u5f62\u5f0f\uff0c\u5141\u8bb8\u591a\u4e2a\u4efb\u52a1\u540c\u65f6\u8fd0\u884c\uff1b<\/p>\n<p>\uff082\uff09\u4ee5\u591a\u7ebf\u7a0b\u5f62\u5f0f\uff0c\u5141\u8bb8\u5355\u4e2a\u4efb\u52a1\u5206\u6210\u4e0d\u540c\u7684\u90e8\u5206\u8fd0\u884c\uff1b<\/p>\n<p>\uff083\uff09\u63d0\u4f9b\u534f\u8c03\u673a\u5236\uff0c\u4e00\u65b9\u9762\u9632\u6b62\u8fdb\u7a0b\u4e4b\u95f4\u548c\u7ebf\u7a0b\u4e4b\u95f4\u4ea7\u751f\u51b2\u7a81\uff0c\u53e6\u4e00\u65b9\u9762\u5141\u8bb8\u8fdb\u7a0b\u4e4b\u95f4\u548c\u7ebf\u7a0b\u4e4b\u95f4\u5171\u4eab\u8d44\u6e90\u3002<\/p>\n<h2>\u591a\u8fdb\u7a0b\u5207\u6362\u4ee3\u4ef7\u4e3a\u4ec0\u4e48\u6bd4\u591a\u7ebf\u7a0b\u5927<\/h2>\n<p>Python\u7684\u591a\u8fdb\u7a0b\u5207\u6362\u4ee3\u4ef7\u6bd4\u591a\u7ebf\u7a0b\u5927\u7684\u539f\u56e0\u4e3b\u8981\u6e90\u4e8e\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u7279\u6027\u4ee5\u53ca\u5b83\u4eec\u5171\u4eab\u8d44\u6e90\u7684\u65b9\u5f0f\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8fdb\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u8d44\u6e90\u5206\u914d\u7684\u57fa\u672c\u5355\u4f4d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u62e5\u6709\u72ec\u7acb\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u5f53\u8fdb\u884c\u8fdb\u7a0b\u5207\u6362\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u9700\u8981\u4fdd\u5b58\u5f53\u524d\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u73af\u5883\uff08\u5305\u62ecCPU\u5bc4\u5b58\u5668\u72b6\u6001\u3001\u7a0b\u5e8f\u8ba1\u6570\u5668\u3001\u5185\u5b58\u7ba1\u7406\u4fe1\u606f\u3001\u6253\u5f00\u7684\u6587\u4ef6\u72b6\u6001\u7b49\uff09\uff0c\u5e76\u6062\u590d\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u8fd9\u4e2a\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u8fc7\u7a0b\u6d89\u53ca\u5927\u91cf\u7684\u6570\u636e\u4fdd\u5b58\u548c\u6062\u590d\u64cd\u4f5c\uff0c\u56e0\u6b64\u5f00\u9500\u76f8\u5bf9\u8f83\u5927\u3002<\/p>\n<p>\u76f8\u6bd4\u4e4b\u4e0b\uff0c\u7ebf\u7a0b\u662f\u8fdb\u7a0b\u5185\u7684\u6267\u884c\u5355\u5143\uff0c\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u540c\u4e00\u4e2a\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u7cfb\u7edf\u8d44\u6e90\u3002\u7ebf\u7a0b\u5207\u6362\u65f6\uff0c\u7531\u4e8e\u5b83\u4eec\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u50cf\u8fdb\u7a0b\u5207\u6362\u90a3\u6837\u4fdd\u5b58\u548c\u6062\u590d\u6574\u4e2a\u8fdb\u7a0b\u7684\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u7ebf\u7a0b\u5207\u6362\u4e3b\u8981\u6d89\u53ca\u7ebf\u7a0b\u63a7\u5236\u5757\uff08TCB\uff09\u7684\u5207\u6362\u4ee5\u53ca\u5bc4\u5b58\u5668\u7b49\u5c11\u91cf\u4fe1\u606f\u7684\u4fdd\u5b58\u548c\u6062\u590d\uff0c\u56e0\u6b64\u5f00\u9500\u76f8\u5bf9\u8f83\u5c0f\u3002<\/p>\n<p>\u6b64\u5916\uff0cPython\u7684\u591a\u8fdb\u7a0b\u8fd8\u6d89\u53ca\u5230\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff08IPC\uff09\u7684\u95ee\u9898\u3002\u7531\u4e8e\u8fdb\u7a0b\u95f4\u5185\u5b58\u662f\u9694\u79bb\u7684\uff0c\u56e0\u6b64\u4e0d\u540c\u8fdb\u7a0b\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u9700\u8981\u901a\u8fc7\u7279\u5b9a\u7684IPC\u673a\u5236\u6765\u5b9e\u73b0\uff0c\u5982\u7ba1\u9053\u3001\u5171\u4eab\u5185\u5b58\u3001\u6d88\u606f\u961f\u5217\u7b49\u3002\u8fd9\u4e9bIPC\u673a\u5236\u672c\u8eab\u4e5f\u4f1a\u5e26\u6765\u4e00\u5b9a\u7684\u5f00\u9500\u3002\u800c\u7ebf\u7a0b\u95f4\u7531\u4e8e\u5171\u4eab\u8fdb\u7a0b\u5185\u5b58\uff0c\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u5171\u4eab\u6570\u636e\uff0c\u65e0\u9700\u901a\u8fc7IPC\u673a\u5236\uff0c\u56e0\u6b64\u5728\u7ebf\u7a0b\u95f4\u901a\u4fe1\u65b9\u9762\u4e5f\u5177\u6709\u66f4\u9ad8\u7684\u6548\u7387\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u7531\u4e8e\u8fdb\u7a0b\u548c\u7ebf\u7a0b\u5728\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u4e0d\u540c\u7279\u6027\u4ee5\u53ca\u5b83\u4eec\u5171\u4eab\u8d44\u6e90\u7684\u65b9\u5f0f\uff0c\u5bfc\u81f4Python\u7684\u591a\u8fdb\u7a0b\u5207\u6362\u4ee3\u4ef7\u6bd4\u591a\u7ebf\u7a0b\u5927\u3002\u56e0\u6b64\uff0c\u5728\u9009\u62e9\u4f7f\u7528\u591a\u8fdb\u7a0b\u8fd8\u662f\u591a\u7ebf\u7a0b\u65f6\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u9700\u6c42\u8fdb\u884c\u6743\u8861\u3002\u5bf9\u4e8e\u8ba1\u7b97\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7531\u4e8e\u591a\u8fdb\u7a0b\u53ef\u4ee5\u5229\u7528\u591a\u6838CPU\u5e76\u884c\u5904\u7406\uff0c\u56e0\u6b64\u53ef\u80fd\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\uff1b\u800c\u5bf9\u4e8eIO\u5bc6\u96c6\u578b\u4efb\u52a1\uff0c\u7531\u4e8e\u591a\u7ebf\u7a0b\u5728IO\u7b49\u5f85\u65f6\u53ef\u4ee5\u5207\u6362\u6267\u884c\u5176\u4ed6\u7ebf\u7a0b\uff0c\u56e0\u6b64\u53ef\u80fd\u5177\u6709\u66f4\u597d\u7684\u54cd\u5e94\u6027\u3002<\/p>\n<h2>python\u5982\u4f55\u5b9e\u73b0\u4e0d\u540c\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u5f0f\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\u3002\u4e0b\u9762\u6211\u5c06\u5217\u4e3e\u51e0\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u5e76\u7ed3\u5408\u793a\u4f8b\u4ee3\u7801\u6765\u8bf4\u660e\u3002<\/p>\n<h3>1. \u7ba1\u9053\uff08Pipe\uff09<\/h3>\n<p>\u7ba1\u9053\u662f\u8fdb\u7a0b\u95f4\u901a\u4fe1\u7684\u4e00\u79cd\u57fa\u672c\u65b9\u5f0f\uff0c\u5b83\u63d0\u4f9b\u4e86\u5355\u5411\u7684\u3001\u5b57\u8282\u6d41\u7684\u901a\u4fe1\u65b9\u5f0f\u3002\u5728Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Pipe()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u4e00\u4e2a\u7ba1\u9053\u3002<\/p>\n<pre><code class=\"language-python\">from multiprocessing import Process, Pipe  \n\ndef worker(conn):  \n    print(conn.recv())  # \u63a5\u6536\u6570\u636e  \n    conn.send(&quot;Data received and processed&quot;)  # \u53d1\u9001\u6570\u636e  \n    conn.close()  \n\nif __name__ == &#039;__main__&#039;:  \n    parent_conn, child_conn = Pipe()  # \u521b\u5efa\u4e00\u4e2a\u7ba1\u9053  \n    p = Process(target=worker, args=(child_conn,))  # \u521b\u5efa\u5b50\u8fdb\u7a0b  \n    p.start()  \n    parent_conn.send(&quot;Hello from parent&quot;)  # \u53d1\u9001\u6570\u636e\u7ed9\u5b50\u8fdb\u7a0b  \n    print(parent_conn.recv())  # \u63a5\u6536\u5b50\u8fdb\u7a0b\u53d1\u6765\u7684\u6570\u636e  \n    p.join()<\/code><\/pre>\n<h3>2. \u961f\u5217\uff08Queue\uff09<\/h3>\n<p>\u961f\u5217\u662f\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u65b9\u5f0f\uff0c\u5b83\u652f\u6301\u591a\u4e2a\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\uff0c\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u548c\u8fdb\u7a0b\u5b89\u5168\u7684\u6d88\u606f\u4f20\u9012\u3002<\/p>\n<pre><code class=\"language-python\">from multiprocessing import Process, Queue  \n\ndef worker(q):  \n    print(q.get())  # \u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e  \n    q.put(&quot;Data processed&quot;)  # \u5c06\u6570\u636e\u653e\u5165\u961f\u5217  \n\nif __name__ == &#039;__main__&#039;:  \n    q = Queue()  # \u521b\u5efa\u4e00\u4e2a\u961f\u5217  \n    q.put(&quot;Hello from main process&quot;)  # \u5c06\u6570\u636e\u653e\u5165\u961f\u5217  \n    p = Process(target=worker, args=(q,))  # \u521b\u5efa\u5b50\u8fdb\u7a0b  \n    p.start()  \n    print(q.get())  # \u4ece\u961f\u5217\u4e2d\u83b7\u53d6\u6570\u636e  \n    p.join()<\/code><\/pre>\n<h3>3. \u5171\u4eab\u5185\u5b58\uff08Value, Array\uff09<\/h3>\n<p>\u9664\u4e86\u57fa\u4e8e\u6d88\u606f\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u5171\u4eab\u5185\u5b58\u6765\u5b9e\u73b0\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002Python\u7684<code>multiprocessing<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>Value<\/code>\u548c<code>Array<\/code>\u6570\u636e\u7ed3\u6784\uff0c\u5b83\u4eec\u53ef\u4ee5\u5728\u591a\u4e2a\u8fdb\u7a0b\u95f4\u5171\u4eab\u3002<\/p>\n<pre><code class=\"language-python\">from multiprocessing import Process, Value  \nimport ctypes  \n\ndef worker(shared_value):  \n    shared_value.value = 10  # \u4fee\u6539\u5171\u4eab\u503c  \n\nif __name__ == &#039;__main__&#039;:  \n    shared_value = Value(ctypes.c_int, 0)  # \u521b\u5efa\u4e00\u4e2a\u5171\u4eab\u7684\u6574\u6570\u503c  \n    p = Process(target=worker, args=(shared_value,))  # \u521b\u5efa\u5b50\u8fdb\u7a0b  \n    p.start()  \n    p.join()  \n    print(shared_value.value)  # \u6253\u5370\u4fee\u6539\u540e\u7684\u5171\u4eab\u503c<\/code><\/pre>\n<h3>4. \u5957\u63a5\u5b57\uff08Socket\uff09<\/h3>\n<p>\u5957\u63a5\u5b57\u662f\u4e0d\u540c\u8fdb\u7a0b\uff08\u751a\u81f3\u4e0d\u540c\u673a\u5668\uff09\u95f4\u901a\u4fe1\u7684\u4e00\u79cd\u901a\u7528\u65b9\u6cd5\u3002\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u7528\u4e8e\u672c\u5730\u8fdb\u7a0b\u95f4\u901a\u4fe1\uff0c\u8fd8\u53ef\u4ee5\u7528\u4e8e\u7f51\u7edc\u901a\u4fe1\u3002<\/p>\n<pre><code class=\"language-python\">import socket  \nfrom multiprocessing import Process  \n\ndef server_program():  \n    host = socket.gethostname()  \n    port = 5000  \n    server_socket = socket.socket()  \n    server_socket.bind((host, port))  \n    server_socket.listen(1)  \n    conn, address = server_socket.accept()  \n    print(&quot;Connection from: &quot; + str(address))  \n    while True:  \n        data = conn.recv(1024).decode()  \n        if not data:  \n            break  \n        print(&quot;from connected user: &quot; + str(data))  \n        data = input(&#039; -&gt; &#039;)  \n        conn.send(data.encode())  \n    conn.close()  \n\ndef client_program():  \n    host = socket.gethostname()  \n    port = 5000  \n    client_socket = socket.socket()  \n    client_socket.connect((host, port))  \n    message = input(&quot; -&gt; &quot;)  \n    while message.lower().strip() != &#039;bye&#039;:  \n        client_socket.send(message.encode())  \n        data = client_socket.recv(1024).decode()  \n        print(&#039;received from server: &#039; + data)  \n        message = input(&quot; -&gt; &quot;)  \n    client_socket.close()  \n\nif __name__ == &#039;__main__&#039;:  \n    p1 = Process(target=server_program)  \n    p1.start()  \n    p2 = Process(target=client_program)  \n    p2.start()  \n    p1.join()  \n    p2.join()<\/code><\/pre>\n<p>\u6bcf\u79cd\u901a\u4fe1\u65b9\u5f0f\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9700\u8981\u6839\u636e\u5b9e\u9645\u5e94\u7528\u573a\u666f\u6765\u9009\u62e9\u6700\u9002\u5408\u7684\u901a\u4fe1\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u7ba1\u9053\u548c\u961f\u5217\u9002\u7528\u4e8e\u7236\u5b50\u8fdb\u7a0b\u95f4\u7684\u901a\u4fe1\uff0c\u800c\u5957\u63a5\u5b57\u5219\u66f4\u9002\u5408\u4e8e\u8de8\u673a\u5668\u6216\u8de8\u7f51\u7edc\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u3002<\/p>\n<h2>python\u5982\u4f55\u5b9e\u73b0\u4e0d\u540c\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1<\/h2>\n<p>\u5728Python\u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u4e0d\u540c\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u3001<code>threading<\/code>\u6a21\u5757\u7684<code>Event<\/code>\u548c<code>Condition<\/code>\u5bf9\u8c61\uff0c\u4ee5\u53ca\u5171\u4eab\u5185\u5b58\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<h3>1. \u4f7f\u7528<code>queue<\/code>\u6a21\u5757<\/h3>\n<p><code>queue<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u3002<\/p>\n<pre><code class=\"language-python\">import threading  \nimport queue  \nimport time  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u751f\u4ea7\u8005\u7ebf\u7a0b  \ndef producer(q):  \n    for item in range(5):  \n        print(f&#039;\u751f\u4ea7\u8005\u751f\u4ea7\u4e86 {item}&#039;)  \n        q.put(item)  \n        time.sleep(1)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c  \n    q.put(None)  # \u4f7f\u7528None\u4f5c\u4e3a\u7ed3\u675f\u4fe1\u53f7  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u6d88\u8d39\u8005\u7ebf\u7a0b  \ndef consumer(q):  \n    while True:  \n        item = q.get()  \n        if item is None:  \n            break  # \u63a5\u6536\u5230\u7ed3\u675f\u4fe1\u53f7\uff0c\u9000\u51fa\u5faa\u73af  \n        print(f&#039;\u6d88\u8d39\u8005\u6d88\u8d39\u4e86 {item}&#039;)  \n        time.sleep(2)  # \u6a21\u62df\u8017\u65f6\u64cd\u4f5c  \n\n# \u521b\u5efa\u4e00\u4e2a\u961f\u5217  \nq = queue.Queue()  \n\n# \u521b\u5efa\u5e76\u542f\u52a8\u751f\u4ea7\u8005\u7ebf\u7a0b  \nt1 = threading.Thread(target=producer, args=(q,))  \nt1.start()  \n\n# \u521b\u5efa\u5e76\u542f\u52a8\u6d88\u8d39\u8005\u7ebf\u7a0b  \nt2 = threading.Thread(target=consumer, args=(q,))  \nt2.start()  \n\n# \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f  \nt1.join()  \nt2.join()<\/code><\/pre>\n<h3>2. \u4f7f\u7528<code>threading.Event<\/code><\/h3>\n<p><code>Event<\/code>\u5bf9\u8c61\u7528\u4e8e\u7ebf\u7a0b\u95f4\u7684\u7b80\u5355\u901a\u4fe1\u3002\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u7b49\u5f85\u4e00\u4e2a\u4e8b\u4ef6\u88ab\u8bbe\u7f6e\uff0c\u800c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u5728\u9002\u5f53\u7684\u65f6\u5019\u8bbe\u7f6e\u8fd9\u4e2a\u4e8b\u4ef6\u3002<\/p>\n<pre><code class=\"language-python\">import threading  \nimport time  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u5de5\u4f5c\u7ebf\u7a0b  \ndef worker(event):  \n    print(&#039;\u5de5\u4f5c\u7ebf\u7a0b\u7b49\u5f85\u4e8b\u4ef6\u88ab\u8bbe\u7f6e...&#039;)  \n    event.wait()  # \u7b49\u5f85\u4e8b\u4ef6\u88ab\u8bbe\u7f6e  \n    print(&#039;\u4e8b\u4ef6\u88ab\u8bbe\u7f6e\u4e86\uff0c\u5de5\u4f5c\u7ebf\u7a0b\u5f00\u59cb\u5de5\u4f5c...&#039;)  \n\n# \u521b\u5efa\u4e00\u4e2aEvent\u5bf9\u8c61  \nevent = threading.Event()  \n\n# \u521b\u5efa\u5e76\u542f\u52a8\u5de5\u4f5c\u7ebf\u7a0b  \nt = threading.Thread(target=worker, args=(event,))  \nt.start()  \n\n# \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4  \ntime.sleep(2)  \nprint(&#039;\u4e3b\u7ebf\u7a0b\u8bbe\u7f6e\u4e8b\u4ef6...&#039;)  \nevent.set()  # \u8bbe\u7f6e\u4e8b\u4ef6  \n\n# \u7b49\u5f85\u5de5\u4f5c\u7ebf\u7a0b\u7ed3\u675f  \nt.join()<\/code><\/pre>\n<h3>3. \u4f7f\u7528<code>threading.Condition<\/code><\/h3>\n<p><code>Condition<\/code>\u5bf9\u8c61\u5141\u8bb8\u7ebf\u7a0b\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\uff0c\u6216\u8005\u901a\u77e5\u5176\u4ed6\u7ebf\u7a0b\u67d0\u4e2a\u6761\u4ef6\u5df2\u7ecf\u6210\u7acb\u3002<\/p>\n<pre><code class=\"language-python\">import threading  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u5e26\u6709\u6761\u4ef6\u7684\u7ebf\u7a0b  \ndef condition_thread(cond):  \n    with cond:  \n        print(&#039;\u7ebf\u7a0b\u7b49\u5f85\u6761\u4ef6...&#039;)  \n        cond.wait()  # \u7b49\u5f85\u6761\u4ef6\u6210\u7acb  \n        print(&#039;\u6761\u4ef6\u6210\u7acb\u4e86\uff0c\u7ebf\u7a0b\u7ee7\u7eed\u6267\u884c...&#039;)  \n\n# \u521b\u5efa\u4e00\u4e2aCondition\u5bf9\u8c61  \ncond = threading.Condition()  \n\n# \u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b  \nt = threading.Thread(target=condition_thread, args=(cond,))  \nt.start()  \n\n# \u4e3b\u7ebf\u7a0b\u7b49\u5f85\u4e00\u6bb5\u65f6\u95f4  \ntime.sleep(2)  \n\n# \u901a\u77e5\u7ebf\u7a0b\u6761\u4ef6\u5df2\u7ecf\u6210\u7acb  \nwith cond:  \n    print(&#039;\u4e3b\u7ebf\u7a0b\u901a\u77e5\u6761\u4ef6\u6210\u7acb...&#039;)  \n    cond.notify()  # \u901a\u77e5\u7b49\u5f85\u7684\u7ebf\u7a0b  \n\n# \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f  \nt.join()<\/code><\/pre>\n<h3>4. \u4f7f\u7528\u5171\u4eab\u5185\u5b58<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\u3002\u8fd9\u901a\u5e38\u6d89\u53ca\u5230\u4f7f\u7528<code>threading.Lock<\/code>\u6216\u5176\u4ed6\u540c\u6b65\u673a\u5236\u6765\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<pre><code class=\"language-python\">import threading  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u5171\u4eab\u6570\u636e  \nshared_data = {&#039;value&#039;: 0}  \nlock = threading.Lock()  \n\n# \u5b9a\u4e49\u4e00\u4e2a\u589e\u52a0\u5171\u4eab\u6570\u636e\u503c\u7684\u7ebf\u7a0b  \ndef increment_thread():  \n    global shared_data  \n    for _ in range(100000):  \n        with lock:  \n            shared_data[&#039;value&#039;] += 1  \n\n# \u521b\u5efa\u5e76\u542f\u52a8\u591a\u4e2a\u7ebf\u7a0b  \nthreads = []  \nfor _ in range(10):  \n    t = threading.Thread(target=increment_thread)  \n    t.start()  \n    threads.append(t)  \n\n# \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u7ed3\u675f  \nfor t in threads:  \n    t.join()  \n\nprint(f&#039;\u5171\u4eab\u6570\u636e\u7684\u6700\u7ec8\u503c: {shared_data[&quot;value&quot;]}&#039;)<\/code><\/pre>\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u4f7f\u7528\u5171\u4eab\u5185\u5b58\u65f6\uff0c\u8981\u7279\u522b\u5c0f\u5fc3\u6570\u636e\u7ade\u4e89\u548c\u6b7b\u9501\u7b49\u95ee\u9898\u3002\u786e\u4fdd\u4f7f\u7528\u9002\u5f53\u7684\u9501\u6216\u5176\u4ed6\u540c\u6b65\u673a\u5236\u6765\u4fdd\u62a4\u5171\u4eab\u6570\u636e\u3002<\/p>\n<p>\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u4f60\u7684\u901a\u4fe1\u65b9\u5f0f\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u901a\u5e38\u5efa\u8bae\u4f7f\u7528<code>queue<\/code>\u6a21\u5757\u6765\u5b9e\u73b0\u7ebf\u7a0b\u95f4\u7684\u901a\u4fe1\uff0c\u56e0\u4e3a\u5b83\u63d0\u4f9b\u4e86\u7ebf\u7a0b\u5b89\u5168\u7684\u961f\u5217\u64cd\u4f5c\uff0c\u4f7f\u5f97\u901a\u4fe1\u66f4\u52a0\u7b80\u5355\u548c\u53ef\u9760\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u5ff5 \u8fdb\u7a0b\uff08process\uff09\u548c\u7ebf\u7a0b\uff08thread\uff09\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4f46\u662f\u5b83\u4eec\u6bd4\u8f83\u62bd\u8c61\uff0c\u4e0d\u5bb9\u6613\u638c\u63e1\u3002 \u6700\u8fd1\uff0c\u6211\u8bfb\u5230\u4e00\u7bc7\u6750\u6599\uff0c\u53d1\u73b0\u6709\u4e00\u4e2a   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[32],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9276"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9276"}],"version-history":[{"count":3,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9276\/revisions"}],"predecessor-version":[{"id":9279,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9276\/revisions\/9279"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9276"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}