{"id":254,"date":"2025-08-13T17:17:22","date_gmt":"2025-08-13T09:17:22","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=254"},"modified":"2025-11-24T16:38:39","modified_gmt":"2025-11-24T08:38:39","slug":"linux%e8%bf%9b%e7%a8%8b%e6%b1%a0%e5%ae%9e%e7%8e%b0%e4%b8%8e%e5%8e%9f%e7%90%86%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=254","title":{"rendered":"Linux\u8fdb\u7a0b\u6c60\u5b9e\u73b0\u4e0e\u539f\u7406\u89e3\u6790"},"content":{"rendered":"\n<p>\u5177\u4f53\u4ee3\u7801\u5728gitee\u4ed3\u5e93\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/processPool\">https:\/\/gitee.com\/tgwTTT\/linux-learning-dai\/tree\/master\/processPool<\/a><\/p>\n\n\n\n<p><strong>\u4e00\u3001\u80cc\u666f\u4e0e\u9700\u6c42<\/strong><\/p>\n\n\n\n<p>\u5728\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u6216\u6279\u91cf\u4efb\u52a1\u5904\u7406\u573a\u666f\u4e0b\uff0c\u9891\u7e41\u521b\u5efa\u548c\u9500\u6bc1\u8fdb\u7a0b\u4f1a\u5e26\u6765\u8f83\u5927\u7684\u7cfb\u7edf\u5f00\u9500\u3002\u8fdb\u7a0b\u6c60\u901a\u8fc7\u9884\u5148\u521b\u5f53\u7136\u53ef\u4ee5\uff01\u4e0b\u9762\u662f\u4e00\u7bc7\u66f4\u53e3\u8bed\u5316\u3001\u6613\u8bfb\u7684\u535a\u5ba2\uff0c\u7ed3\u5408\u4f60\u7684\u4ee3\u7801\u8bb2\u89e3\u8fdb\u7a0b\u6c60\u7684\u539f\u7406\u548c\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bbe\u8ba1\u601d\u8def<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">1.channel\u548cchannelManager<\/h3>\n\n\n\n<p>\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u548c\u4e3b\u8fdb\u7a0b\u4e4b\u95f4\u7528\u7ba1\u9053\u901a\u4fe1\u3002<code>channel<\/code> \u7c7b\u5c31\u662f\u7ba1\u9053\u7684\u201c\u4fe1\u4f7f\u201d\uff0c\u8d1f\u8d23\u628a\u4efb\u52a1\u7801\u53d1\u7ed9\u5b50\u8fdb\u7a0b\u3002<code>channelManager<\/code> \u5c31\u50cf\u4e2a\u8c03\u5ea6\u5458\uff0c\u7ba1\u7406\u6240\u6709\u4fe1\u9053\uff0c\u8f6e\u6d41\u5206\u914d\u4efb\u52a1\uff0c\u4fdd\u8bc1\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u90fd\u6709\u6d3b\u513f\u5e72\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. processPool<\/h3>\n\n\n\n<p>processPool \u8d1f\u8d23\u521b\u5efa\u5b50\u8fdb\u7a0b\u3001\u5206\u53d1\u4efb\u52a1\u3001\u56de\u6536\u8d44\u6e90\u3002\u4e3b\u8fdb\u7a0b\u7528 <code>Run()<\/code> \u65b9\u6cd5\u5faa\u73af\u5206\u53d1\u4efb\u52a1\uff0c\u5b50\u8fdb\u7a0b\u7528 <code>Work()<\/code> \u65b9\u6cd5\u63a5\u6536\u4efb\u52a1\u7801\uff0c\u7136\u540e\u6267\u884c\u5bf9\u5e94\u7684\u64cd\u4f5c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. TaskManager<\/h3>\n\n\n\n<p>\u8fd9\u4e2a\u7c7b\u8d1f\u8d23\u6ce8\u518c\u5404\u79cd\u4efb\u52a1\uff0c\u6bd4\u5982\u6253\u5370\u65e5\u5fd7\u3001\u4e0b\u8f7d\u3001\u4e0a\u4f20\u3001\u5220\u9664\u3001\u538b\u7f29\u3002\u4e3b\u8fdb\u7a0b\u968f\u673a\u751f\u6210\u4efb\u52a1\u7801\uff0c\u5206\u53d1\u7ed9\u5b50\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u6536\u5230\u7801\u540e\u5c31\u53bb\u6267\u884c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4ee3\u7801\u62c6\u89e3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u521b\u5efa\u8fdb\u7a0b\u6c60<\/h3>\n\n\n\n<p>\u4e3b\u8fdb\u7a0b\u4f1a\u5faa\u73af\u521b\u5efa\u7ba1\u9053\u548c\u5b50\u8fdb\u7a0b\u3002\u6bcf\u4e2a\u5b50\u8fdb\u7a0b\u53ea\u4fdd\u7559\u7ba1\u9053\u7684\u8bfb\u7aef\uff0c\u4e3b\u8fdb\u7a0b\u53ea\u4fdd\u7559\u5199\u7aef\u3002\u8fd9\u6837\u53ef\u4ee5\u907f\u514d\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e71\u7528\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>for(int i=0;i<_process_num;i++){\n    int pipefd[2]={0};\n    pipe(pipefd);\n    pid_t subid=fork();\n    if(subid==0){\n        close(pipefd[1]);\n        Work(pipefd[0]);\n        close(pipefd[0]);\n        exit(0);\n    }else{\n        close(pipefd[0]);\n        _cm.Insert(pipefd[1],subid);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3b\u8fdb\u7a0b\u5206\u53d1\u4efb\u52a1<\/h3>\n\n\n\n<p>\u4e3b\u8fdb\u7a0b\u6bcf\u9694\u4e00\u79d2\u5206\u53d1\u4e00\u4e2a\u4efb\u52a1\u7801\uff0c\u8f6e\u6d41\u7ed9\u5404\u4e2a\u5b50\u8fdb\u7a0b\u3002\u5206\u53d1\u5341\u6b21\u540e\u5c31\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int cnt=0;\nwhile(true){\n    if(cnt++>10){\n        stop();\n        break;\n    }\n    int taskcode=_tm.Code();\n    channel &c=_cm.Select();\n    c.Send(taskcode);\n    sleep(1);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b50\u8fdb\u7a0b\u63a5\u6536\u4efb\u52a1<\/h3>\n\n\n\n<p>\u5b50\u8fdb\u7a0b\u4e0d\u65ad\u4ece\u7ba1\u9053\u8bfb\u4efb\u52a1\u7801\uff0c\u6536\u5230\u5c31\u6267\u884c\u5bf9\u5e94\u4efb\u52a1\u3002\u4e3b\u8fdb\u7a0b\u5173\u95ed\u7ba1\u9053\u540e\uff0c\u5b50\u8fdb\u7a0b\u5c31\u9000\u51fa\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>while(true){\n    int code=0;\n    ssize_t n=read(rfd,&code,sizeof(code));\n    if(n>0){\n        _tm.Execute(code);\n    }else if(n==0){\n        break;\n    }else{\n        break;\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8d44\u6e90\u56de\u6536<\/h3>\n\n\n\n<p>\u4e3b\u8fdb\u7a0b\u9000\u51fa\u524d\u4f1a\u5173\u95ed\u6240\u6709\u7ba1\u9053\u5199\u7aef\uff0c\u5e76\u56de\u6536\u6240\u6709\u5b50\u8fdb\u7a0b\uff0c\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void stop(){\n    _cm.stopsubprocess();\n    _cm.waitsubprocess();\n}<\/code><\/pre>\n\n\n\n<p>\u4e09\u3001\u5173\u952e\u5b9e\u73b0\u7ec6\u8282<br>\u7ba1\u9053\u901a\u4fe1<br>\u4e3b\u8fdb\u7a0b\u53ea\u4fdd\u7559\u7ba1\u9053\u5199\u7aef\uff0c\u5b50\u8fdb\u7a0b\u53ea\u4fdd\u7559\u8bfb\u7aef\uff0c\u907f\u514d\u6587\u4ef6\u63cf\u8ff0\u7b26\u6cc4\u6f0f\u548c\u8bef\u7528\u3002<\/p>\n\n\n\n<p>\u8fdb\u7a0b\u521b\u5efa\u4e0e\u5206\u79bb<br>fork() \u540e\uff0c\u5b50\u8fdb\u7a0b\u6267\u884c Work() \u5e76\u5728\u7ed3\u675f\u540e exit(0)\uff0c\u9632\u6b62\u5b50\u8fdb\u7a0b\u7ee7\u7eed\u6267\u884c\u4e3b\u8fdb\u7a0b\u903b\u8f91\u3002<\/p>\n\n\n\n<p>\u8d44\u6e90\u56de\u6536<br>\u4e3b\u8fdb\u7a0b\u5728\u9000\u51fa\u524d\u5173\u95ed\u6240\u6709\u7ba1\u9053\u5199\u7aef\uff0c\u5e76\u901a\u8fc7 waitpid \u56de\u6536\u6240\u6709\u5b50\u8fdb\u7a0b\uff0c\u907f\u514d\u50f5\u5c38\u8fdb\u7a0b\u3002<\/p>\n\n\n\n<p>\u9519\u8bef\u5904\u7406<br>\u7ba1\u9053\u5199\u5165\u5931\u8d25\u65f6\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u4fbf\u4e8e\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u622a\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"496\" height=\"639\" src=\"http:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-10.png\" alt=\"\" class=\"wp-image-256\" srcset=\"https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-10.png 496w, https:\/\/www.tgwttt.xyz\/wp-content\/uploads\/2025\/08\/image-10-233x300.png 233w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/figure>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff01\uff01<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5177\u4f53\u4ee3\u7801\u5728gitee\u4ed3\u5e93\uff1ahttps:\/\/gitee.com\/tgwTTT\/linux-learning-da&#8230;<\/p>\n","protected":false},"author":1,"featured_media":256,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[5],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=254"}],"version-history":[{"count":5,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":851,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/254\/revisions\/851"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/media\/256"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}