{"id":37841,"date":"2025-05-18T06:12:08","date_gmt":"2025-05-17T22:12:08","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/37841.html"},"modified":"2025-05-18T06:12:08","modified_gmt":"2025-05-17T22:12:08","slug":"linux%e5%a4%9a%e7%ba%bf%e7%a8%8b%e9%ab%98%e5%b9%b6%e5%8f%91%e6%9c%8d%e5%8a%a1%e5%99%a8%e8%ae%be%e8%ae%a1%e4%b8%8e%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/37841.html","title":{"rendered":"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0"},"content":{"rendered":"<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b766d12.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u662f\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u7684\u5173\u952e\u6280\u672f&#xff0c;\u5b83\u6d89\u53ca\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3001\u9ad8\u5e76\u53d1\u5904\u7406\u3001\u7ebf\u7a0b\u6c60\u7ba1\u7406\u3001\u540c\u6b65\u4e0e\u4e92\u65a5\u3001\u6027\u80fd\u4f18\u5316\u7b49\u6838\u5fc3\u6982\u5ff5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u6784\u5efa\u9ad8\u6548\u7684\u670d\u52a1\u5668&#xff0c;\u5305\u62ec\u7ebf\u7a0b\u7ba1\u7406\u7b56\u7565\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u964d\u4f4e\u5f00\u9500\u3001\u91c7\u7528epoll\u6280\u672f\u63d0\u9ad8I\/O\u6548\u7387\u3001\u786e\u4fdd\u6570\u636e\u5b89\u5168\u7684\u540c\u6b65\u673a\u5236\u3001\u4fe1\u53f7\u91cf\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\u3001\u5185\u5b58\u4e0e\u8d1f\u8f7d\u4f18\u5316\u4ee5\u53ca\u5f02\u6b65I\/O\u6a21\u578b\u3002\u4e86\u89e3\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u662f\u5f00\u53d1\u9ad8\u6027\u80fd\u3001\u9ad8\u53ef\u7528\u6027\u670d\u52a1\u5668\u7684\u57fa\u7840\u3002 <img decoding=\"async\" alt=\"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b770aa1.png\" \/> <\/p>\n<h2> 1. Linux\u591a\u7ebf\u7a0b\u7f16\u7a0b\u57fa\u7840 <\/h2>\n<p> Linux\u591a\u7ebf\u7a0b\u7f16\u7a0b\u662f\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u8bbe\u8ba1\u7684\u6838\u5fc3\u90e8\u5206&#xff0c;\u5b83\u5141\u8bb8\u7a0b\u5e8f\u5728\u540c\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4efb\u52a1\u3002\u8fd9\u4e9b\u4efb\u52a1\u901a\u8fc7\u7ebf\u7a0b\u6765\u5b9e\u73b0&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u8d44\u6e90&#xff0c;\u4f46\u53c8\u53ef\u4ee5\u72ec\u7acb\u6267\u884c\u81ea\u5df1\u7684\u4efb\u52a1\u3002\u5728\u672c\u7ae0\u4e2d&#xff0c;\u6211\u4eec\u5c06\u4ece\u6700\u57fa\u672c\u7684\u7ebf\u7a0b\u521b\u5efa\u4e0e\u6267\u884c\u5f00\u59cb&#xff0c;\u9010\u6b65\u6df1\u5165\u63a2\u8ba8\u7ebf\u7a0b\u7684\u540c\u6b65\u3001\u901a\u4fe1\u548c\u7ba1\u7406\u7b49\u591a\u7ebf\u7a0b\u7f16\u7a0b\u7684\u5173\u952e\u6982\u5ff5\u548c\u5b9e\u8df5\u3002 <\/p>\n<h3> 1.1 \u7ebf\u7a0b\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f18\u52bf <\/h3>\n<p> \u5728Linux\u73af\u5883\u4e0b&#xff0c;\u7ebf\u7a0b\u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u8f7b\u91cf\u7ea7\u7684\u8fdb\u7a0b&#xff0c;\u5b83\u4eec\u4ee5\u66f4\u4f4e\u7684\u8d44\u6e90\u6d88\u8017\u63d0\u4f9b\u4e86\u5e76\u53d1\u6267\u884c\u7684\u80fd\u529b\u3002\u4e00\u4e2a\u8fdb\u7a0b\u53ef\u4ee5\u62e5\u6709\u591a\u4e2a\u7ebf\u7a0b&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u5171\u4eab\u8fdb\u7a0b\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u5176\u4ed6\u8d44\u6e90&#xff0c;\u8fd9\u4f7f\u5f97\u7ebf\u7a0b\u95f4\u901a\u4fe1\u53d8\u5f97\u66f4\u4e3a\u9ad8\u6548\u3002 <\/p>\n<h4> 1.1.1 \u7ebf\u7a0b\u7684\u5b9a\u4e49\u548c\u5de5\u4f5c\u539f\u7406 <\/h4>\n<p> \u7ebf\u7a0b\u7684\u5b9a\u4e49\u6d89\u53ca\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u652f\u6301&#xff0c;\u5b83\u662f\u4e00\u79cd\u80fd\u591f\u88ab\u7cfb\u7edf\u72ec\u7acb\u8c03\u5ea6\u548c\u5206\u6d3e\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5728Linux\u7cfb\u7edf\u4e2d&#xff0c;\u7ebf\u7a0b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5b9e\u4f53&#xff0c;\u5176\u8c03\u5ea6\u5b8c\u5168\u7531\u7cfb\u7edf\u51b3\u5b9a\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6&#xff0c;\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u5fc5\u8981\u7684\u8d44\u6e90&#xff0c;\u5982\u7ebf\u7a0bID\u3001\u5bc4\u5b58\u5668\u96c6\u3001\u6808\u3001\u72b6\u6001\u7b49\u3002\u7ebf\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u548c\u4f18\u5148\u7ea7\u8c03\u5ea6&#xff0c;\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u673a\u4f1a\u6267\u884c\u3002 <\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>void *thread_function(void *arg) {<br \/>\n    printf(&#034;Hello, World from thread %lu!\\\\n&#034;, pthread_self());<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    pthread_t thread_id;<br \/>\n    int res;<\/p>\n<p>    res &#061; pthread_create(&amp;thread_id, NULL, thread_function, NULL);<br \/>\n    if (res !&#061; 0) {<br \/>\n        perror(&#034;Thread creation failed&#034;);<br \/>\n        exit(EXIT_FAILURE);<br \/>\n    }<\/p>\n<p>    \/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    pthread_join(thread_id, NULL);<\/p>\n<p>    printf(&#034;Thread finished execution.\\\\n&#034;);<br \/>\n    exit(EXIT_SUCCESS);<br \/>\n}<\/p>\n<h4> 1.1.2 \u7ebf\u7a0b\u4e0e\u8fdb\u7a0b\u7684\u6bd4\u8f83 <\/h4>\n<p> \u4e0e\u4f20\u7edf\u7684\u8fdb\u7a0b\u6a21\u578b\u76f8\u6bd4&#xff0c;\u7ebf\u7a0b\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u4e3a\u8f7b\u91cf\u7ea7\u7684\u5e76\u53d1\u6267\u884c\u65b9\u5f0f\u3002\u521b\u5efa\u548c\u5207\u6362\u7ebf\u7a0b\u7684\u6210\u672c\u8fdc\u4f4e\u4e8e\u8fdb\u7a0b&#xff0c;\u56e0\u6b64\u7ebf\u7a0b\u66f4\u9002\u5408\u4e8e\u5b9e\u73b0\u591a\u4efb\u52a1\u5e76\u884c\u5904\u7406\u3002\u591a\u7ebf\u7a0b\u6a21\u578b\u4e2d&#xff0c;\u7ebf\u7a0b\u95f4\u5171\u4eab\u5185\u5b58\u5730\u5740\u7a7a\u95f4&#xff0c;\u4f7f\u5f97\u6570\u636e\u5171\u4eab\u53d8\u5f97\u975e\u5e38\u5bb9\u6613&#xff1b;\u800c\u591a\u8fdb\u7a0b\u6a21\u578b\u5219\u9700\u8981\u590d\u6742\u7684\u8fdb\u7a0b\u95f4\u901a\u4fe1\u673a\u5236\u3002 <\/p>\n<p> \u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u5e76\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u7b49\u5f85\u5b83\u5b8c\u6210&#xff0c;\u4ee5\u6b64\u7406\u89e3\u7ebf\u7a0b\u7684\u57fa\u672c\u521b\u5efa\u548c\u6267\u884c\u8fc7\u7a0b\u3002\u8fd9\u662f\u63a2\u7d22\u7ebf\u7a0b\u7f16\u7a0b\u7684\u7b2c\u4e00\u6b65&#xff0c;\u4e5f\u662f\u6784\u5efa\u66f4\u590d\u6742\u591a\u7ebf\u7a0b\u5e94\u7528\u7684\u57fa\u77f3\u3002\u968f\u7740\u672c\u7ae0\u7684\u6df1\u5165&#xff0c;\u6211\u4eec\u4f1a\u9010\u6e10\u638c\u63e1\u7ebf\u7a0b\u540c\u6b65\u3001\u901a\u4fe1\u4ee5\u53ca\u7ebf\u7a0b\u6c60\u7ba1\u7406\u7b49\u9ad8\u7ea7\u6982\u5ff5\u3002 <\/p>\n<h2> 2. \u7ebf\u7a0b\u6c60\u7684\u8bbe\u8ba1\u4e0e\u7ba1\u7406 <\/h2>\n<h4> 2.1 \u7ebf\u7a0b\u6c60\u7684\u57fa\u672c\u6982\u5ff5\u548c\u4f18\u52bf <\/h4>\n<h5> 2.1.1 \u7ebf\u7a0b\u6c60\u7684\u5b9a\u4e49\u548c\u5de5\u4f5c\u539f\u7406 <\/h5>\n<p> \u7ebf\u7a0b\u6c60&#xff08;Thread Pool&#xff09;\u662f\u4e00\u79cd\u591a\u7ebf\u7a0b\u5904\u7406\u5f62\u5f0f&#xff0c;\u5b83\u9884\u521b\u5efa\u4e00\u5b9a\u6570\u91cf\u7684\u7ebf\u7a0b\u653e\u5165\u6c60\u4e2d&#xff0c;\u5f53\u9700\u8981\u6267\u884c\u65b0\u7684\u4efb\u52a1\u65f6&#xff0c;\u65e0\u9700\u521b\u5efa\u65b0\u7ebf\u7a0b&#xff0c;\u800c\u662f\u76f4\u63a5\u4ece\u6c60\u4e2d\u83b7\u53d6\u7ebf\u7a0b\u4f7f\u7528\u3002\u7ebf\u7a0b\u6c60\u4e2d\u7684\u7ebf\u7a0b\u6267\u884c\u5b8c\u6bd5\u540e\u5e76\u4e0d\u9500\u6bc1&#xff0c;\u800c\u662f\u8fd4\u56de\u7ebf\u7a0b\u6c60\u4e2d\u7b49\u5f85\u4e0b\u4e00\u6b21\u7684\u8c03\u7528\u3002 <\/p>\n<p> \u7ebf\u7a0b\u6c60\u7684\u5de5\u4f5c\u539f\u7406\u4e3b\u8981\u901a\u8fc7\u4ee5\u4e0b\u51e0\u4e2a\u6838\u5fc3\u90e8\u5206\u5b9e\u73b0&#xff1a; &#8211;  \u4efb\u52a1\u961f\u5217  &#xff1a;\u5b58\u50a8\u5f85\u5904\u7406\u7684\u4efb\u52a1&#xff0c;\u7ebf\u7a0b\u6c60\u6839\u636e\u961f\u5217\u4e2d\u4efb\u52a1\u6570\u91cf\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6570\u91cf\u3002 &#8211;  \u5de5\u4f5c\u7ebf\u7a0b  &#xff1a;\u6267\u884c\u4efb\u52a1\u7684\u7ebf\u7a0b&#xff0c;\u5b83\u4eec\u4ece\u4efb\u52a1\u961f\u5217\u4e2d\u83b7\u53d6\u4efb\u52a1\u5e76\u6267\u884c\u3002 &#8211;  \u4efb\u52a1\u8c03\u5ea6  &#xff1a;\u5206\u914d\u4efb\u52a1\u7ed9\u7a7a\u95f2\u7684\u5de5\u4f5c\u7ebf\u7a0b&#xff0c;\u907f\u514d\u7ebf\u7a0b\u95f4\u7ade\u4e89\u548c\u9891\u7e41\u7684\u4e0a\u4e0b\u6587\u5207\u6362\u3002 <\/p>\n<p> \u5728Linux\u73af\u5883\u4e0b&#xff0c;\u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\u901a\u5e38\u4f9d\u8d56\u4e8e\u7ebf\u7a0b\u5e93&#xff08;\u5982NPTL&#xff09;&#xff0c;\u4ee5\u53ca\u7279\u5b9a\u7684\u7cfb\u7edf\u8c03\u7528\u548c\u51fd\u6570\u3002 <\/p>\n<h5> 2.1.2 \u7ebf\u7a0b\u6c60\u4e0e\u76f4\u63a5\u521b\u5efa\u7ebf\u7a0b\u7684\u6bd4\u8f83 <\/h5>\n<p> \u76f4\u63a5\u521b\u5efa\u7ebf\u7a0b\u6a21\u5f0f\u4e0b&#xff0c;\u6bcf\u6b21\u6709\u65b0\u4efb\u52a1\u65f6\u5c31\u521b\u5efa\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u4efb\u52a1\u6267\u884c\u5b8c\u6bd5\u540e\u7ebf\u7a0b\u9500\u6bc1\u3002\u8fd9\u79cd\u65b9\u5f0f\u867d\u7136\u7b80\u5355\u76f4\u89c2&#xff0c;\u4f46\u5728\u9ad8\u5e76\u53d1\u573a\u666f\u4e0b\u5b58\u5728\u6027\u80fd\u74f6\u9888\u548c\u8d44\u6e90\u6d6a\u8d39\u3002 <\/p>\n<p> \u4e0e\u4e4b\u76f8\u6bd4&#xff0c;\u7ebf\u7a0b\u6c60\u5177\u6709\u4ee5\u4e0b\u4f18\u52bf&#xff1a; &#8211;  \u51cf\u5c11\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u7684\u5f00\u9500  &#xff1a;\u7ebf\u7a0b\u521b\u5efa\u548c\u9500\u6bc1\u9700\u8981\u6d88\u8017\u7cfb\u7edf\u8d44\u6e90&#xff0c;\u7ebf\u7a0b\u6c60\u901a\u8fc7\u590d\u7528\u7ebf\u7a0b\u964d\u4f4e\u8fd9\u90e8\u5206\u5f00\u9500\u3002 &#8211;  \u63d0\u9ad8\u54cd\u5e94\u901f\u5ea6  &#xff1a;\u65b0\u4efb\u52a1\u5230\u6765\u65f6&#xff0c;\u65e0\u9700\u7b49\u5f85\u7ebf\u7a0b\u521b\u5efa&#xff0c;\u76f4\u63a5\u4f7f\u7528\u7a7a\u95f2\u7ebf\u7a0b\u5904\u7406\u3002 &#8211;  \u7b80\u5316\u8d44\u6e90\u7ba1\u7406  &#xff1a;\u7ebf\u7a0b\u751f\u547d\u5468\u671f\u7531\u6c60\u63a7\u5236&#xff0c;\u7b80\u5316\u4e86\u7ebf\u7a0b\u7684\u7ba1\u7406\u548c\u7ef4\u62a4\u5de5\u4f5c\u3002 &#8211;  \u63a7\u5236\u5e76\u53d1\u6570  &#xff1a;\u53ef\u4ee5\u6709\u6548\u63a7\u5236\u5e76\u53d1\u6570&#xff0c;\u907f\u514d\u7cfb\u7edf\u8d44\u6e90\u8fc7\u5ea6\u6d88\u8017\u5bfc\u81f4\u7684\u6027\u80fd\u95ee\u9898\u3002 <\/p>\n<h4> 2.2 \u7ebf\u7a0b\u6c60\u7684\u5b9e\u73b0\u673a\u5236 <\/h4>\n<h5> 2.2.1 \u5de5\u4f5c\u961f\u5217\u7684\u6784\u5efa\u4e0e\u7ba1\u7406 <\/h5>\n<p> \u7ebf\u7a0b\u6c60\u4e2d\u7684\u5de5\u4f5c\u961f\u5217\u662f\u5b58\u653e\u5f85\u5904\u7406\u4efb\u52a1\u7684\u5bb9\u5668&#xff0c;\u5b83\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u6570\u636e\u7ed3\u6784\u5b9e\u73b0&#xff0c;\u5e38\u89c1\u7684\u6709\u961f\u5217\u3001\u94fe\u8868\u7b49\u3002\u5de5\u4f5c\u961f\u5217\u7684\u6784\u5efa\u4e0e\u7ba1\u7406\u76f4\u63a5\u5f71\u54cd\u7ebf\u7a0b\u6c60\u7684\u6027\u80fd\u548c\u4efb\u52a1\u5904\u7406\u80fd\u529b\u3002 <\/p>\n<p> \u5728\u5b9e\u73b0\u5de5\u4f5c\u961f\u5217\u65f6&#xff0c;\u5e94\u5f53\u8003\u8651\u4ee5\u4e0b\u51e0\u4e2a\u65b9\u9762&#xff1a; &#8211;  \u7ebf\u7a0b\u5b89\u5168  &#xff1a;\u786e\u4fdd\u4efb\u52a1\u961f\u5217\u7684\u5165\u961f\u548c\u51fa\u961f\u64cd\u4f5c\u7ebf\u7a0b\u5b89\u5168\u3002 &#8211;  \u963b\u585e\u673a\u5236  &#xff1a;\u652f\u6301\u963b\u585e\u7b49\u5f85&#xff0c;\u5f53\u961f\u5217\u4e3a\u7a7a\u65f6&#xff0c;\u7ebf\u7a0b\u5e94\u963b\u585e\u76f4\u5230\u6709\u65b0\u4efb\u52a1\u5230\u6765\u3002 &#8211;  \u4efb\u52a1\u5206\u914d\u7b56\u7565  &#xff1a;\u5408\u7406\u5b89\u6392\u4efb\u52a1\u5206\u914d\u7ed9\u7ebf\u7a0b\u7684\u7b56\u7565&#xff0c;\u5982\u8f6e\u8be2\u3001\u968f\u673a\u3001\u4f18\u5148\u7ea7\u7b49\u3002 <\/p>\n<p> \u4ee3\u7801\u793a\u4f8b&#xff1a; <\/p>\n<p>\/\/ \u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u961f\u5217\u5b9e\u73b0&#xff0c;\u7528\u4e8e\u7ba1\u7406\u7ebf\u7a0b\u6c60\u4e2d\u7684\u4efb\u52a1\u3002<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>typedef struct {<br \/>\n    \/\/ \u961f\u5217\u5143\u7d20\u7c7b\u578b\u4e3a\u51fd\u6570\u6307\u9488&#xff0c;\u6307\u5411\u9700\u8981\u6267\u884c\u7684\u4efb\u52a1<br \/>\n    void *(*task)(void *);<br \/>\n    struct task_queue *next;<br \/>\n} task_node_t;<\/p>\n<p>typedef struct {<br \/>\n    \/\/ \u961f\u5217\u5934\u6307\u9488<br \/>\n    task_node_t *front;<br \/>\n    \/\/ \u961f\u5217\u5c3e\u6307\u9488<br \/>\n    task_node_t *rear;<br \/>\n    \/\/ \u4e92\u65a5\u9501&#xff0c;\u7528\u4e8e\u7ebf\u7a0b\u540c\u6b65<br \/>\n    pthread_mutex_t lock;<br \/>\n    \/\/ \u6761\u4ef6\u53d8\u91cf&#xff0c;\u7528\u4e8e\u963b\u585e\u548c\u5524\u9192\u7ebf\u7a0b<br \/>\n    pthread_cond_t cond;<br \/>\n} task_queue_t;<\/p>\n<p>\/\/ \u51fd\u6570\u7528\u4e8e\u521d\u59cb\u5316\u4efb\u52a1\u961f\u5217<br \/>\nvoid init_task_queue(task_queue_t *queue) {<br \/>\n    queue-&gt;front &#061; queue-&gt;rear &#061; NULL;<br \/>\n    pthread_mutex_init(&amp;queue-&gt;lock, NULL);<br \/>\n    pthread_cond_init(&amp;queue-&gt;cond, NULL);<br \/>\n}<\/p>\n<p>\/\/ \u5176\u4ed6\u961f\u5217\u64cd\u4f5c\u51fd\u6570&#xff0c;\u5982\u5165\u961f\u3001\u51fa\u961f\u3001\u9500\u6bc1\u7b49&#8230;<\/p>\n<h5> 2.2.2 \u7ebf\u7a0b\u6c60\u53c2\u6570\u914d\u7f6e\u4e0e\u6027\u80fd\u5f71\u54cd <\/h5>\n<p> \u7ebf\u7a0b\u6c60\u7684\u6027\u80fd\u5f88\u5927\u7a0b\u5ea6\u4e0a\u4f9d\u8d56\u4e8e\u5176\u53c2\u6570\u7684\u914d\u7f6e\u3002\u5408\u7406\u914d\u7f6e\u53c2\u6570\u80fd\u591f\u5e2e\u52a9\u7ebf\u7a0b\u6c60\u8fbe\u5230\u6700\u4f73\u5de5\u4f5c\u72b6\u6001&#xff0c;\u5305\u62ec\u7ebf\u7a0b\u6570\u91cf\u3001\u4efb\u52a1\u961f\u5217\u5927\u5c0f\u3001\u7ebf\u7a0b\u4f18\u5148\u7ea7\u7b49\u3002 <\/p>\n<p> \u7ebf\u7a0b\u6c60\u53c2\u6570\u901a\u5e38\u5305\u62ec&#xff1a; &#8211;  \u6838\u5fc3\u7ebf\u7a0b\u6570  &#xff1a;\u7ebf\u7a0b\u6c60\u7ef4\u6301\u7684\u6700\u5c0f\u7ebf\u7a0b\u6570\u3002 &#8211;  \u6700\u5927\u7ebf\u7a0b\u6570  &#xff1a;\u7ebf\u7a0b\u6c60\u80fd\u591f\u521b\u5efa\u7684\u6700\u5927\u7ebf\u7a0b\u6570\u3002 &#8211;  \u7a7a\u95f2\u7ebf\u7a0b\u8d85\u65f6\u65f6\u95f4  &#xff1a;\u7ebf\u7a0b\u7a7a\u95f2\u591a\u5c11\u65f6\u95f4\u540e\u81ea\u52a8\u9500\u6bc1\u3002 &#8211;  \u4efb\u52a1\u961f\u5217\u5bb9\u91cf  &#xff1a;\u4efb\u52a1\u961f\u5217\u80fd\u591f\u5b58\u653e\u4efb\u52a1\u7684\u6700\u5927\u6570\u91cf\u3002 <\/p>\n<p> \u53c2\u6570\u914d\u7f6e\u7684\u5f71\u54cd&#xff1a; &#8211;  \u7ebf\u7a0b\u6570\u8fc7\u5c11  &#xff1a;\u53ef\u80fd\u5bfc\u81f4\u4efb\u52a1\u5904\u7406\u4e0d\u53ca\u65f6&#xff0c;\u5bfc\u81f4\u961f\u5217\u5806\u79ef\u3002 &#8211;  \u7ebf\u7a0b\u6570\u8fc7\u591a  &#xff1a;\u4f1a\u5bfc\u81f4\u4e0a\u4e0b\u6587\u5207\u6362\u589e\u52a0&#xff0c;\u589e\u52a0\u7cfb\u7edf\u5f00\u9500\u3002 &#8211;  \u4efb\u52a1\u961f\u5217\u8fc7\u5c0f  &#xff1a;\u53ef\u80fd\u5bfc\u81f4\u4efb\u52a1\u88ab\u62d2\u7edd&#xff0c;\u5f71\u54cd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002 <\/p>\n<h4> 2.3 \u7ebf\u7a0b\u6c60\u7684\u9ad8\u7ea7\u5e94\u7528 <\/h4>\n<h5> 2.3.1 \u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u5927\u5c0f\u7b56\u7565 <\/h5>\n<p> \u7ebf\u7a0b\u6c60\u7684\u5927\u5c0f\u8c03\u6574\u662f\u4fdd\u8bc1\u5176\u6027\u80fd\u7684\u5173\u952e\u3002\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6c60\u5927\u5c0f\u7b56\u7565\u9700\u8981\u8003\u8651\u5f53\u524d\u7684\u5de5\u4f5c\u8d1f\u8f7d\u60c5\u51b5&#xff0c;\u4f8b\u5982\u5728\u9ad8\u8d1f\u8f7d\u65f6\u589e\u52a0\u7ebf\u7a0b\u6570&#xff0c;\u4f4e\u8d1f\u8f7d\u65f6\u51cf\u5c11\u7ebf\u7a0b\u6570\u3002 <\/p>\n<p> \u52a8\u6001\u8c03\u6574\u7b56\u7565\u7684\u5b9e\u73b0\u65b9\u6cd5\u5305\u62ec&#xff1a; &#8211;  \u57fa\u4e8e\u4efb\u52a1\u961f\u5217\u957f\u5ea6  &#xff1a;\u5f53\u4efb\u52a1\u961f\u5217\u957f\u5ea6\u8d85\u8fc7\u9884\u8bbe\u9608\u503c\u65f6&#xff0c;\u589e\u52a0\u7ebf\u7a0b\u6570\u3002 &#8211;  \u57fa\u4e8e\u7cfb\u7edf\u8d1f\u8f7d  &#xff1a;\u901a\u8fc7\u76d1\u6d4bCPU\u3001\u5185\u5b58\u7b49\u8d44\u6e90\u4f7f\u7528\u60c5\u51b5&#xff0c;\u52a8\u6001\u8c03\u6574\u7ebf\u7a0b\u6570\u3002 &#8211;  \u57fa\u4e8e\u9884\u6d4b\u6a21\u578b  &#xff1a;\u4f7f\u7528\u5386\u53f2\u6570\u636e\u5efa\u6a21&#xff0c;\u9884\u6d4b\u672a\u6765\u8d1f\u8f7d\u5e76\u636e\u6b64\u8c03\u6574\u7ebf\u7a0b\u6570\u3002 <\/p>\n<p> \u793a\u4f8b\u4ee3\u7801&#xff1a; <\/p>\n<p>void adjust_pool_size(task_queue_t *queue) {<br \/>\n    pthread_mutex_lock(&amp;queue-&gt;lock);<br \/>\n    \/\/ \u68c0\u67e5\u961f\u5217\u957f\u5ea6&#xff0c;\u5982\u679c\u8d85\u8fc7\u9608\u503c&#xff0c;\u5219\u589e\u52a0\u7ebf\u7a0b\u6570<br \/>\n    if (get_queue_length(queue) &gt; QUEUE_THRESHOLD) {<br \/>\n        add_threads(queue);<br \/>\n    }<br \/>\n    \/\/ \u5982\u679c\u7ebf\u7a0b\u6570\u8fc7\u591a&#xff0c;\u4e14\u961f\u5217\u957f\u65f6\u95f4\u4e3a\u7a7a&#xff0c;\u5219\u51cf\u5c11\u7ebf\u7a0b\u6570<br \/>\n    if (get_idle_threads(queue) &gt; IDLE_THRESHOLD &amp;&amp; get_queue_length(queue) &#061;&#061; 0) {<br \/>\n        reduce_threads(queue);<br \/>\n    }<br \/>\n    pthread_mutex_unlock(&amp;queue-&gt;lock);<br \/>\n}<\/p>\n<h5> 2.3.2 \u7ebf\u7a0b\u6c60\u6545\u969c\u5904\u7406\u548c\u8d44\u6e90\u56de\u6536 <\/h5>\n<p> \u5728\u7ebf\u7a0b\u6c60\u7684\u4f7f\u7528\u8fc7\u7a0b\u4e2d&#xff0c;\u96be\u514d\u4f1a\u51fa\u73b0\u7ebf\u7a0b\u6216\u4efb\u52a1\u6267\u884c\u5931\u8d25\u7684\u60c5\u51b5\u3002\u6545\u969c\u5904\u7406\u548c\u8d44\u6e90\u56de\u6536\u673a\u5236\u80fd\u591f\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002 <\/p>\n<p> \u5e38\u89c1\u7684\u6545\u969c\u5904\u7406\u548c\u8d44\u6e90\u56de\u6536\u65b9\u6cd5\u5305\u62ec&#xff1a; &#8211;  \u7ebf\u7a0b\u5f02\u5e38\u76d1\u63a7  &#xff1a;\u76d1\u63a7\u7ebf\u7a0b\u6267\u884c\u72b6\u6001&#xff0c;\u4e00\u65e6\u51fa\u73b0\u5f02\u5e38&#xff0c;\u7acb\u5373\u56de\u6536\u8be5\u7ebf\u7a0b\u3002 &#8211;  \u4efb\u52a1\u6267\u884c\u8d85\u65f6\u5904\u7406  &#xff1a;\u7ed9\u4efb\u52a1\u8bbe\u7f6e\u8d85\u65f6\u65f6\u95f4&#xff0c;\u8d85\u65f6\u672a\u5b8c\u6210\u7684\u4efb\u52a1\u5e94\u88ab\u53d6\u6d88\u3002 &#8211;  \u8d44\u6e90\u91ca\u653e\u673a\u5236  &#xff1a;\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u88ab\u6b63\u786e\u9500\u6bc1&#xff0c;\u5e76\u91ca\u653e\u6240\u6709\u5206\u914d\u7684\u8d44\u6e90\u3002 <\/p>\n<p> \u793a\u4f8b\u4ee3\u7801&#xff1a; <\/p>\n<p>void shutdown_pool(task_queue_t *queue) {<br \/>\n    pthread_mutex_lock(&amp;queue-&gt;lock);<br \/>\n    \/\/ \u53d6\u6d88\u6240\u6709\u6b63\u5728\u6267\u884c\u7684\u4efb\u52a1<br \/>\n    cancel_tasks(queue);<br \/>\n    \/\/ \u7b49\u5f85\u6240\u6709\u7ebf\u7a0b\u5b8c\u6210\u4efb\u52a1\u5e76\u9000\u51fa<br \/>\n    wait_for_threads_to_finish(queue);<br \/>\n    \/\/ \u9500\u6bc1\u4efb\u52a1\u961f\u5217\u548c\u76f8\u5173\u8d44\u6e90<br \/>\n    destroy_task_queue(queue);<br \/>\n    pthread_mutex_unlock(&amp;queue-&gt;lock);<br \/>\n}<\/p>\n<p> \u901a\u8fc7\u8fd9\u4e9b\u7ae0\u8282\u7684\u4ecb\u7ecd&#xff0c;\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u7ebf\u7a0b\u6c60\u7684\u8bbe\u8ba1\u4e0e\u7ba1\u7406\u6d89\u53ca\u5230\u8bb8\u591a\u5c42\u9762\u7684\u8003\u8651&#xff0c;\u4ece\u57fa\u672c\u6982\u5ff5\u3001\u5de5\u4f5c\u539f\u7406\u5230\u5b9e\u73b0\u673a\u5236\u3001\u9ad8\u7ea7\u5e94\u7528&#xff0c;\u6bcf\u4e00\u6b65\u90fd\u9700\u8981\u7ec6\u5fc3\u7684\u8bbe\u8ba1\u548c\u4f18\u5316&#xff0c;\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7684\u9ad8\u6548\u7a33\u5b9a\u8fd0\u884c\u3002 <\/p>\n<h2> 3. \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684I\/O\u591a\u8def\u590d\u7528\u6280\u672f <\/h2>\n<p> \u968f\u7740\u7f51\u7edc\u5e94\u7528\u7684\u666e\u53ca\u548c\u4e92\u8054\u7f51\u670d\u52a1\u7684\u53d1\u5c55&#xff0c;\u5bf9\u4e8e\u670d\u52a1\u5668\u6765\u8bf4&#xff0c;\u5904\u7406\u5927\u91cf\u7684\u5e76\u53d1\u8fde\u63a5\u5df2\u7ecf\u6210\u4e3a\u4e86\u57fa\u672c\u9700\u6c42\u3002\u5728\u8fd9\u6837\u7684\u80cc\u666f\u4e0b&#xff0c;\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u4fbf\u662f\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e2d\u7684\u5173\u952e\u6280\u672f\u4e4b\u4e00\u3002\u672c\u7ae0\u5c06\u6df1\u5165\u63a2\u8ba8I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u539f\u7406&#xff0c;\u4ee5\u53ca\u5982\u4f55\u5728Linux\u73af\u5883\u4e0b\u9ad8\u6548\u5730\u5e94\u7528\u8fd9\u4e00\u6280\u672f\u3002 <\/p>\n<h3> 3.1 I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u539f\u7406 <\/h3>\n<h4> 3.1.1 \u540c\u6b65I\/O\u4e0e\u5f02\u6b65I\/O\u7684\u57fa\u672c\u6982\u5ff5 <\/h4>\n<p> \u5728\u8ba8\u8bba\u591a\u8def\u590d\u7528\u4e4b\u524d&#xff0c;\u6709\u5fc5\u8981\u5148\u4e86\u89e3\u540c\u6b65I\/O\u4e0e\u5f02\u6b65I\/O\u7684\u533a\u522b\u3002\u540c\u6b65I\/O\u6307\u7684\u662f\u5728I\/O\u64cd\u4f5c\u5b8c\u6210\u524d&#xff0c;\u8c03\u7528\u5b83\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b\u5c06\u88ab\u963b\u585e&#xff0c;\u76f4\u5230\u64cd\u4f5c\u5b8c\u6210\u624d\u80fd\u7ee7\u7eed\u6267\u884c\u540e\u7eed\u4ee3\u7801\u3002\u800c\u5f02\u6b65I\/O\u5219\u662f\u6307I\/O\u64cd\u4f5c\u5728\u540e\u53f0\u5b8c\u6210&#xff0c;\u4e0d\u4f1a\u963b\u585e\u8c03\u7528\u5b83\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b&#xff0c;\u64cd\u4f5c\u5b8c\u6210\u540e\u4f1a\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u901a\u77e5\u8c03\u7528\u8005\u3002 <\/p>\n<h4> 3.1.2 \u591a\u8def\u590d\u7528\u7684\u5b9e\u73b0\u65b9\u5f0f\u548c\u9002\u7528\u573a\u666f <\/h4>\n<p> \u591a\u8def\u590d\u7528\u6280\u672f\u5141\u8bb8\u591a\u4e2aI\/O\u64cd\u4f5c\u540c\u65f6\u5728\u540c\u4e00\u4e2a\u7ebf\u7a0b\u4e2d\u7b49\u5f85&#xff0c;\u5f53\u67d0\u4e2aI\/O\u6761\u4ef6\u51c6\u5907\u597d\u65f6&#xff08;\u5982\u6570\u636e\u53ef\u8bfb\u6216\u53ef\u5199&#xff09;&#xff0c;\u7cfb\u7edf\u5c31\u901a\u77e5\u76f8\u5e94\u7684\u7ebf\u7a0b\u8fdb\u884c\u5904\u7406\u3002\u5e38\u89c1\u7684\u591a\u8def\u590d\u7528\u5b9e\u73b0\u65b9\u5f0f\u6709select\u3001poll\u548cepoll\u7b49\u3002 <\/p>\n<h5> \u8868\u683c&#xff1a;\u540c\u6b65I\/O\u4e0e\u5f02\u6b65I\/O\u4ee5\u53ca\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u6bd4\u8f83 <\/h5>\n<p> | \u7279\u6027 | \u540c\u6b65I\/O | \u5f02\u6b65I\/O | \u591a\u8def\u590d\u7528\u6280\u672f | | &#8212;&#8212;&#8212;&#8212;&#8211; | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- | | \u6267\u884c\u963b\u585e | \u662f | \u5426 | \u5426&#xff08;\u4f9d\u8d56\u4e8e\u5b9e\u73b0\u65b9\u5f0f&#xff0c;\u5982epoll\u975e\u963b\u585e&#xff09;| | I\/O\u64cd\u4f5c\u901a\u77e5\u65b9\u5f0f| \u901a\u8fc7\u8fd4\u56de\u7801\u6216\u8005\u5f02\u5e38\u5904\u7406 | \u901a\u8fc7\u56de\u8c03\u51fd\u6570\u6216\u8005\u4e8b\u4ef6\u901a\u77e5 | \u901a\u8fc7\u4e8b\u4ef6\u901a\u77e5 | | \u9002\u7528\u573a\u666f | I\/O\u5bc6\u96c6\u578b\u4efb\u52a1 | \u9ad8\u5ef6\u8fdf\u7f51\u7edc\u8bf7\u6c42 | \u591a\u7528\u6237\u73af\u5883\u4e0b\u7684\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1 | <\/p>\n<p> I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7279\u522b\u9002\u5408\u4e8e\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u7684\u60c5\u51b5\u3002\u4f8b\u5982&#xff0c;\u5728\u4e00\u4e2a\u9700\u8981\u540c\u65f6\u5904\u7406\u591a\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u7684\u7f51\u7edc\u670d\u52a1\u5668\u4e2d&#xff0c;\u4f7f\u7528\u591a\u8def\u590d\u7528\u6280\u672f&#xff0c;\u53ef\u4ee5\u907f\u514d\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u4e00\u4e2a\u5355\u72ec\u7684\u7ebf\u7a0b\u6216\u8fdb\u7a0b&#xff0c;\u4ece\u800c\u51cf\u5c11\u7cfb\u7edf\u8d44\u6e90\u7684\u6d88\u8017&#xff0c;\u5e76\u4e14\u63d0\u9ad8\u7a0b\u5e8f\u7684\u6269\u5c55\u6027\u3002 <\/p>\n<h3> 3.2 Linux\u4e0bepoll\u7684\u5de5\u4f5c\u673a\u5236\u4e0e\u5e94\u7528 <\/h3>\n<p> Linux\u63d0\u4f9b\u4e86\u591a\u79cdI\/O\u591a\u8def\u590d\u7528\u7684API&#xff0c;\u5176\u4e2depoll\u662f\u4e00\u79cd\u9ad8\u6548\u7684I\/O\u4e8b\u4ef6\u901a\u77e5\u673a\u5236\u3002\u76f8\u6bd4\u4f20\u7edf\u7684select\u548cpoll&#xff0c;epoll\u5728\u5904\u7406\u5927\u91cf\u5e76\u53d1\u8fde\u63a5\u65f6\u5177\u6709\u66f4\u9ad8\u7684\u6027\u80fd\u3002 <\/p>\n<h4> 3.2.1 epoll API\u8be6\u89e3 <\/h4>\n<p> epoll\u901a\u8fc7epoll_create\u3001epoll_ctl\u548cepoll_wait\u4e09\u4e2a\u4e3b\u8981\u7684\u7cfb\u7edf\u8c03\u7528\u6765\u5b9e\u73b0\u5176\u529f\u80fd\u3002\u9996\u5148&#xff0c;\u901a\u8fc7epoll_create\u521b\u5efa\u4e00\u4e2aepoll\u5b9e\u4f8b&#xff0c;\u63a5\u7740\u4f7f\u7528epoll_ctl\u5411\u8be5\u5b9e\u4f8b\u4e2d\u6dfb\u52a0\u3001\u4fee\u6539\u6216\u5220\u9664\u4e8b\u4ef6&#xff0c;\u6700\u540e\u901a\u8fc7epoll_wait\u6765\u7b49\u5f85\u4e8b\u4ef6\u7684\u53d1\u751f\u3002 <\/p>\n<h5> \u793a\u4f8b\u4ee3\u7801\u5757&#xff1a;epoll\u57fa\u7840\u4f7f\u7528 <\/h5>\n<p>#include &lt;sys\/epoll.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<\/p>\n<p>\/\/ \u521b\u5efaepoll\u5b9e\u4f8b<br \/>\nint epollfd &#061; epoll_create1(0);<br \/>\nif (epollfd &#061;&#061; -1) {<br \/>\n    perror(&#034;epoll_create&#034;);<br \/>\n    exit(EXIT_FAILURE);<br \/>\n}<\/p>\n<p>\/\/ \u5b9a\u4e49\u8981\u76d1\u542c\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u548c\u4e8b\u4ef6<br \/>\nstruct epoll_event ev, events[10];<br \/>\nint listen_sock &#061; \/* &#8230; *\/;<\/p>\n<p>\/\/ \u521d\u59cb\u5316\u4e8b\u4ef6\u7ed3\u6784\u4f53<br \/>\nev.events &#061; EPOLLIN;<br \/>\nev.data.fd &#061; listen_sock;<\/p>\n<p>\/\/ \u6dfb\u52a0\u4e8b\u4ef6\u5230epoll\u5b9e\u4f8b<br \/>\nif (epoll_ctl(epollfd, EPOLL_CTL_ADD, listen_sock, &amp;ev) &#061;&#061; -1) {<br \/>\n    perror(&#034;epoll_ctl: listen_sock&#034;);<br \/>\n    exit(EXIT_FAILURE);<br \/>\n}<\/p>\n<p>\/\/ \u4e3b\u5faa\u73af<br \/>\nfor (;;) {<br \/>\n    int n &#061; epoll_wait(epollfd, events, 10, -1);<br \/>\n    \/\/ &#8230; \u5904\u7406\u4e8b\u4ef6 &#8230;<br \/>\n}<\/p>\n<h4> 3.2.2 \u9ad8\u6548\u4e8b\u4ef6\u5904\u7406\u6a21\u578b\u7684\u6784\u5efa <\/h4>\n<p> \u6784\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u4e8b\u4ef6\u5904\u7406\u6a21\u578b&#xff0c;\u9700\u8981\u5bf9epoll\u7684\u4f7f\u7528\u8fdb\u884c\u4f18\u5316&#xff0c;\u5982\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u4e8b\u4ef6\u89e6\u53d1\u3001\u5408\u7406\u7ba1\u7406\u4e8b\u4ef6\u7c7b\u578b\u548c\u6570\u91cf\u3001\u4ee5\u53ca\u6709\u6548\u7684\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u7b49\u3002\u5408\u7406\u5229\u7528LT&#xff08;Level Triggered&#xff09;\u548cET&#xff08;Edge Triggered&#xff09;\u4e24\u79cd\u6a21\u5f0f&#xff0c;\u53ef\u4ee5\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6027\u80fd\u3002LT\u6a21\u5f0f\u4e0b&#xff0c;\u53ea\u8981\u6709\u4e8b\u4ef6\u672a\u5904\u7406&#xff0c;\u6bcf\u6b21epoll_wait\u90fd\u4f1a\u8fd4\u56de&#xff0c;\u800cET\u6a21\u5f0f\u53ea\u5728\u4e8b\u4ef6\u72b6\u6001\u53d8\u5316\u65f6\u89e6\u53d1\u4e00\u6b21\u3002 <\/p>\n<h3> 3.3 I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u6027\u80fd\u6d4b\u8bd5 <\/h3>\n<p> \u8981\u8bc4\u4f30\u548c\u4f18\u5316I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u6027\u80fd&#xff0c;\u6211\u4eec\u9700\u8981\u4e86\u89e3\u5982\u4f55\u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5&#xff0c;\u4ee5\u53ca\u5982\u4f55\u89e3\u8bfb\u6d4b\u8bd5\u7ed3\u679c\u3002 <\/p>\n<h4> 3.3.1 \u6027\u80fd\u6d4b\u8bd5\u65b9\u6cd5\u4e0e\u5de5\u5177 <\/h4>\n<p> \u8fdb\u884c\u6027\u80fd\u6d4b\u8bd5\u901a\u5e38\u4f1a\u7528\u5230\u4e00\u4e9b\u6807\u51c6\u5de5\u5177&#xff0c;\u5982sysbench\u3001iperf\u4ee5\u53ca\u81ea\u5b9a\u4e49\u7684\u57fa\u51c6\u6d4b\u8bd5\u7a0b\u5e8f\u3002\u8fd9\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6d4b\u91cf\u5728\u4e0d\u540c\u8d1f\u8f7d\u4e0b\u7684\u7cfb\u7edf\u8868\u73b0&#xff0c;\u5305\u62ec\u6bcf\u79d2\u5904\u7406\u7684\u8bf7\u6c42\u6570\u91cf\u3001\u54cd\u5e94\u65f6\u95f4\u3001CPU\u548c\u5185\u5b58\u4f7f\u7528\u7387\u7b49\u5173\u952e\u6307\u6807\u3002 <\/p>\n<h4> 3.3.2 \u4e0d\u540cI\/O\u6a21\u578b\u7684\u5bf9\u6bd4\u5206\u6790 <\/h4>\n<p> \u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d&#xff0c;\u6211\u4eec\u901a\u5e38\u4f1a\u6bd4\u8f83select\u3001poll\u548cepoll\u8fd9\u4e09\u79cdI\/O\u6a21\u578b\u7684\u6027\u80fd\u3002\u901a\u8fc7\u63a7\u5236\u53d8\u91cf\u7684\u65b9\u5f0f&#xff0c;\u5982\u76f8\u540c\u7684\u6d4b\u8bd5\u73af\u5883\u548c\u8d1f\u8f7d\u6761\u4ef6&#xff0c;\u89c2\u5bdf\u5b83\u4eec\u5728\u4e0d\u540c\u5e76\u53d1\u8fde\u63a5\u6570\u4e0b\u7684\u8868\u73b0\u5dee\u5f02\u3002 <\/p>\n<h5> \u8868\u683c&#xff1a;\u4e0d\u540cI\/O\u6a21\u578b\u6027\u80fd\u5bf9\u6bd4 <\/h5>\n<p> | \u6d4b\u8bd5\u9879 | select | poll | epoll ET | epoll LT | | &#8212;&#8212;&#8212;&#8211;|:&#8212;&#8212;&#8212;-:|:&#8212;&#8212;&#8212;&#8212;:|:&#8212;&#8212;&#8212;&#8212;&#8211;:|:&#8212;&#8212;&#8212;&#8212;&#8211;:| | \u8fde\u63a5\u6570 | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | | CPU\u4f7f\u7528\u7387 | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | | \u54cd\u5e94\u65f6\u95f4 | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | | \u541e\u5410\u91cf | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | \u7ed3\u679c\u5c55\u793a | <\/p>\n<p> \u901a\u8fc7\u6027\u80fd\u6d4b\u8bd5\u548c\u5206\u6790&#xff0c;\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u5e94\u7528\u573a\u666f\u9009\u62e9\u6700\u5408\u9002\u7684I\/O\u6a21\u578b&#xff0c;\u5b9e\u73b0\u6027\u80fd\u7684\u6700\u4f18\u5316\u3002\u5728\u672c\u7ae0\u8282\u4e2d&#xff0c;\u6211\u4eec\u6df1\u5165\u4e86\u89e3\u4e86I\/O\u591a\u8def\u590d\u7528\u6280\u672f\u7684\u539f\u7406\u53ca\u5176\u5728Linux\u4e0b\u7684\u5177\u4f53\u5e94\u7528&#xff0c;\u7279\u522b\u662fepoll\u673a\u5236\u7684\u5b9e\u73b0\u548c\u6027\u80fd\u6d4b\u8bd5\u65b9\u6cd5\u3002\u8fd9\u5bf9\u4e8e\u8bbe\u8ba1\u548c\u4f18\u5316\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u67b6\u6784\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<h2> 4. \u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u540c\u6b65\u4e0e\u4e92\u65a5 <\/h2>\n<h3> 4.1 \u7ebf\u7a0b\u540c\u6b65\u673a\u5236\u7684\u539f\u7406 <\/h3>\n<h4> 4.1.1 \u4e34\u754c\u533a\u4e0e\u8d44\u6e90\u7ade\u4e89\u95ee\u9898 <\/h4>\n<p> \u5728\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d&#xff0c;\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u8bd5\u56fe\u540c\u65f6\u8bbf\u95ee\u548c\u4fee\u6539\u5171\u4eab\u8d44\u6e90&#xff0c;\u5bfc\u81f4\u6570\u636e\u7ade\u4e89\u548c\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5&#xff0c;\u8fd9\u5c31\u5f62\u6210\u4e86\u4e34\u754c\u533a&#xff08;Critical Section&#xff09;\u3002\u5728\u4e34\u754c\u533a\u4e2d&#xff0c;\u5fc5\u987b\u4fdd\u8bc1\u4efb\u4f55\u65f6\u5019\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90&#xff0c;\u8fd9\u5c31\u662f\u540c\u6b65\u95ee\u9898\u7684\u6838\u5fc3\u6240\u5728\u3002 <\/p>\n<p> \u4e3a\u4e86\u907f\u514d\u4e34\u754c\u533a\u51b2\u7a81&#xff0c;\u9700\u8981\u4f7f\u7528\u540c\u6b65\u673a\u5236\u6765\u786e\u4fdd\u7ebf\u7a0b\u4e4b\u95f4\u7684\u64cd\u4f5c\u987a\u5e8f\u548c\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u5e38\u89c1\u7684\u540c\u6b65\u673a\u5236\u5305\u62ec\u4e92\u65a5\u9501&#xff08;Mutex&#xff09;\u3001\u6761\u4ef6\u53d8\u91cf&#xff08;Condition Variable&#xff09;\u3001\u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;\u7b49\u3002 <\/p>\n<h4> 4.1.2 \u540c\u6b65\u4e0e\u4e92\u65a5\u5de5\u5177\u7684\u9009\u62e9\u6807\u51c6 <\/h4>\n<p> \u9009\u62e9\u5408\u9002\u7684\u540c\u6b65\u5de5\u5177\u5bf9\u4e8e\u4fdd\u8bc1\u7a0b\u5e8f\u6b63\u786e\u6027\u548c\u6548\u7387\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9009\u62e9\u540c\u6b65\u5de5\u5177\u65f6\u9700\u8981\u8003\u8651\u7684\u6807\u51c6&#xff1a; <\/p>\n<ul>\n<li>  \u7c92\u5ea6  &#xff1a;\u9009\u62e9\u540c\u6b65\u673a\u5236\u65f6\u8981\u8003\u8651\u8bbf\u95ee\u5171\u4eab\u8d44\u6e90\u7684\u9891\u7387\u548c\u7c92\u5ea6\u3002\u7ec6\u7c92\u5ea6\u9501\u53ef\u4ee5\u63d0\u4f9b\u66f4\u597d\u7684\u5e76\u53d1&#xff0c;\u4f46\u589e\u52a0\u4e86\u590d\u6742\u6027\u548c\u5f00\u9500\u3002 <\/li>\n<li>  \u6027\u80fd  &#xff1a;\u4e0d\u540c\u540c\u6b65\u673a\u5236\u6709\u4e0d\u540c\u7684\u6027\u80fd\u5f71\u54cd\u3002\u4f8b\u5982&#xff0c;\u81ea\u65cb\u9501\u9002\u7528\u4e8e\u77ed\u65f6\u95f4\u6301\u9501\u7684\u573a\u666f&#xff0c;\u800c\u4e92\u65a5\u9501\u9002\u7528\u4e8e\u6301\u9501\u65f6\u95f4\u8f83\u957f\u7684\u60c5\u51b5\u3002 <\/li>\n<li>  \u8d44\u6e90\u72b6\u6001  &#xff1a;\u5982\u679c\u9700\u8981\u63a7\u5236\u5bf9\u67d0\u4e9b\u6761\u4ef6\u7684\u8bbf\u95ee&#xff0c;\u6761\u4ef6\u53d8\u91cf\u53ef\u80fd\u662f\u4e00\u4e2a\u66f4\u597d\u7684\u9009\u62e9\u3002 <\/li>\n<li>  \u7ebf\u7a0b\u95f4\u5173\u7cfb  &#xff1a;\u4fe1\u53f7\u91cf\u53ef\u4ee5\u7528\u4e8e\u63a7\u5236\u5bf9\u8d44\u6e90\u7684\u6700\u5927\u8bbf\u95ee\u6570\u6216\u8005\u5b9e\u73b0\u590d\u6742\u7684\u540c\u6b65\u5173\u7cfb\u3002 <\/li>\n<\/ul>\n<h3> 4.2 pthread_mutex_t\u7684\u4f7f\u7528\u4e0e\u4f18\u5316 <\/h3>\n<h4> 4.2.1 \u4e92\u65a5\u9501\u7684\u7c7b\u578b\u548c\u4f7f\u7528\u573a\u666f <\/h4>\n<p> \u4e92\u65a5\u9501\u662f\u6700\u57fa\u672c\u7684\u540c\u6b65\u673a\u5236\u4e4b\u4e00&#xff0c;\u7528\u4e8e\u4fdd\u62a4\u4e34\u754c\u533a\u3002\u5728pthread\u5e93\u4e2d&#xff0c;\u4e92\u65a5\u9501\u7531pthread_mutex_t\u7c7b\u578b\u8868\u793a\u3002\u4e92\u65a5\u9501\u6709\u4e24\u79cd\u7c7b\u578b&#xff1a; <\/p>\n<ul>\n<li>  \u666e\u901a\u9501  &#xff08;PTHREAD_MUTEX_NORMAL&#xff09;&#xff1a;\u4e0d\u8981\u6c42\u6301\u9501\u7ebf\u7a0b\u5728\u89e3\u9501\u4e4b\u524d\u62e5\u6709\u9501&#xff0c;\u53ef\u80fd\u5f15\u8d77\u6b7b\u9501\u3002 <\/li>\n<li>  \u9012\u5f52\u9501  &#xff08;PTHREAD_MUTEX_RECURSIVE&#xff09;&#xff1a;\u5141\u8bb8\u540c\u4e00\u7ebf\u7a0b\u591a\u6b21\u52a0\u9501&#xff0c;\u76f4\u5230\u89e3\u9501\u76f8\u540c\u7684\u6b21\u6570\u540e\u624d\u91ca\u653e\u9501&#xff0c;\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u64cd\u4f5c\u3002 <\/li>\n<\/ul>\n<p> \u4e92\u65a5\u9501\u7684\u4f7f\u7528\u573a\u666f\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e&#xff1a; <\/p>\n<ul>\n<li>  \u4fdd\u62a4\u5171\u4eab\u6570\u636e  &#xff1a;\u5728\u8bbf\u95ee\u5171\u4eab\u53d8\u91cf\u65f6\u4f7f\u7528\u4e92\u65a5\u9501\u4fdd\u8bc1\u5176\u539f\u5b50\u6027\u3002 <\/li>\n<li>  \u5e8f\u5217\u5316\u8bbf\u95ee  &#xff1a;\u63a7\u5236\u5bf9\u67d0\u4e9b\u8d44\u6e90\u6216\u670d\u52a1\u7684\u8bbf\u95ee\u987a\u5e8f\u3002 <\/li>\n<\/ul>\n<h4> 4.2.2 \u6b7b\u9501\u7684\u907f\u514d\u548c\u89e3\u51b3\u7b56\u7565 <\/h4>\n<p> \u6b7b\u9501\u662f\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u4e00\u4e2a\u4e25\u91cd\u7684\u95ee\u9898&#xff0c;\u5f53\u4e24\u4e2a\u6216\u4e24\u4e2a\u4ee5\u4e0a\u7684\u7ebf\u7a0b\u6c38\u8fdc\u76f8\u4e92\u7b49\u5f85\u5bf9\u65b9\u91ca\u653e\u8d44\u6e90\u65f6&#xff0c;\u5c31\u4f1a\u53d1\u751f\u6b7b\u9501\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u907f\u514d\u548c\u89e3\u51b3\u6b7b\u9501\u7684\u7b56\u7565&#xff1a; <\/p>\n<ul>\n<li>  \u9075\u5faa\u9501\u5b9a\u987a\u5e8f  &#xff1a;\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u6309\u7167\u76f8\u540c\u7684\u987a\u5e8f\u83b7\u53d6\u9501&#xff0c;\u4ece\u800c\u907f\u514d\u5faa\u73af\u7b49\u5f85\u6761\u4ef6\u3002 <\/li>\n<li>  \u9501\u5b9a\u65f6\u9650  &#xff1a;\u7ed9\u9501\u8bbe\u7f6e\u4e00\u4e2a\u65f6\u9650&#xff0c;\u5f53\u7ebf\u7a0b\u65e0\u6cd5\u5728\u6307\u5b9a\u65f6\u95f4\u5185\u83b7\u5f97\u9501\u65f6&#xff0c;\u5c31\u653e\u5f03\u5e76\u91cd\u8bd5\u3002 <\/li>\n<li>  \u9501\u5347\u7ea7  &#xff1a;\u5148\u83b7\u53d6\u8f83\u8f7b\u91cf\u7ea7\u7684\u9501&#xff0c;\u7136\u540e\u518d\u5347\u7ea7\u5230\u66f4\u91cd\u91cf\u7ea7\u7684\u9501&#xff0c;\u51cf\u5c11\u6301\u6709\u9ad8\u6210\u672c\u9501\u7684\u65f6\u95f4\u3002 <\/li>\n<\/ul>\n<h3> 4.3 \u5176\u4ed6\u540c\u6b65\u5de5\u5177\u7684\u6bd4\u8f83\u4e0e\u5e94\u7528 <\/h3>\n<h4> 4.3.1 \u6761\u4ef6\u53d8\u91cf\u7684\u4f7f\u7528\u548c\u6848\u4f8b\u5206\u6790 <\/h4>\n<p> \u6761\u4ef6\u53d8\u91cf&#xff08;Condition Variable&#xff09;\u901a\u5e38\u4e0e\u4e92\u65a5\u9501\u7ed3\u5408\u4f7f\u7528&#xff0c;\u5141\u8bb8\u7ebf\u7a0b\u5728\u67d0\u4e2a\u6761\u4ef6\u4e0d\u6210\u7acb\u65f6\u7b49\u5f85&#xff08;\u963b\u585e&#xff09;&#xff0c;\u76f4\u5230\u5176\u4ed6\u7ebf\u7a0b\u6539\u53d8\u6761\u4ef6\u5e76\u53d1\u51fa\u901a\u77e5\u3002\u6761\u4ef6\u53d8\u91cf\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b\u3002 <\/p>\n<p> \u4ee5\u4e0b\u662f\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u7684\u4f2a\u4ee3\u7801\u793a\u4f8b&#xff1a; <\/p>\n<p>pthread_mutex_t mutex;<br \/>\npthread_cond_t cond;<\/p>\n<p>pthread_mutex_lock(&amp;mutex);<br \/>\nwhile (resource_count &#061;&#061; 0) { \/\/ \u68c0\u67e5\u6761\u4ef6<br \/>\n    pthread_cond_wait(&amp;cond, &amp;mutex); \/\/ \u7b49\u5f85\u6761\u4ef6\u53d8\u91cf<br \/>\n}<br \/>\nresource_count&#8211;; \/\/ \u4fee\u6539\u6761\u4ef6<br \/>\npthread_mutex_unlock(&amp;mutex);<\/p>\n<h4> 4.3.2 \u4fe1\u53f7\u91cf\u4e0e\u5176\u4ed6\u540c\u6b65\u5de5\u5177\u7684\u7efc\u5408\u6bd4\u8f83 <\/h4>\n<p> \u4fe1\u53f7\u91cf&#xff08;Semaphore&#xff09;\u662f\u4e00\u79cd\u63d0\u4f9b\u4e0d\u540c\u7ebf\u7a0b\u95f4\u540c\u6b65\u624b\u6bb5\u7684\u673a\u5236&#xff0c;\u5b83\u7ef4\u62a4\u7740\u4e00\u7ec4\u865a\u62df\u7684\u201c\u8bb8\u53ef\u8bc1\u201d\u3002\u4fe1\u53f7\u91cf\u9002\u7528\u4e8e\u63a7\u5236\u5bf9\u4e00\u5b9a\u6570\u91cf\u8d44\u6e90\u7684\u8bbf\u95ee&#xff0c;\u6216\u8005\u5b9e\u73b0\u590d\u6742\u7684\u540c\u6b65\u6a21\u5f0f\u3002 <\/p>\n<p> \u4fe1\u53f7\u91cf\u4e0e\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u7684\u6bd4\u8f83&#xff1a; <\/p>\n<ul>\n<li>  \u4e92\u65a5\u9501  \u4e3b\u8981\u7528\u4e8e\u4fdd\u62a4\u5171\u4eab\u8d44\u6e90&#xff0c;\u800c\u4fe1\u53f7\u91cf\u5219\u66f4\u9002\u7528\u4e8e\u63a7\u5236\u8d44\u6e90\u6570\u91cf\u9650\u5236\u3002 <\/li>\n<li>  \u6761\u4ef6\u53d8\u91cf  \u5728\u7b49\u5f85\u67d0\u4e2a\u6761\u4ef6\u6210\u7acb\u65f6\u4f7f\u7528&#xff0c;\u800c\u4fe1\u53f7\u91cf\u4e0d\u9700\u8981\u7b49\u5f85\u6761\u4ef6&#xff0c;\u800c\u662f\u76f4\u63a5\u63a7\u5236\u8d44\u6e90\u6570\u91cf\u3002 <\/li>\n<\/ul>\n<p> \u4fe1\u53f7\u91cf\u4e0e\u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u76f8\u6bd4&#xff0c;\u5176\u4f7f\u7528\u573a\u666f\u66f4\u52a0\u5e7f\u6cdb&#xff0c;\u4f46\u76f8\u5e94\u5730&#xff0c;\u5b9e\u73b0\u4e5f\u66f4\u52a0\u590d\u6742\u3002\u5728\u8bbe\u8ba1\u9ad8\u5e76\u53d1\u7cfb\u7edf\u65f6&#xff0c;\u6b63\u786e\u9009\u62e9\u5e76\u4f7f\u7528\u8fd9\u4e9b\u540c\u6b65\u5de5\u5177&#xff0c;\u5bf9\u4e8e\u7ef4\u62a4\u7cfb\u7edf\u7a33\u5b9a\u6027\u548c\u63d0\u9ad8\u6027\u80fd\u81f3\u5173\u91cd\u8981\u3002 <\/p>\n<p> \u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u540c\u6b65\u4e0e\u4e92\u65a5\u673a\u5236&#xff0c;\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528  pthread_mutex_t  \u4e92\u65a5\u9501\u548c\u6761\u4ef6\u53d8\u91cf\u5b9e\u73b0\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u7684\u793a\u4f8b\u4ee3\u7801&#xff1a; <\/p>\n<p>#include &lt;pthread.h&gt;<br \/>\n#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<\/p>\n<p>#define BUFFER_SIZE 10<\/p>\n<p>int buffer[BUFFER_SIZE];<br \/>\nint count &#061; 0;<br \/>\nint read_pos &#061; 0;<br \/>\nint write_pos &#061; 0;<\/p>\n<p>pthread_mutex_t mutex;<br \/>\npthread_cond_t cond;<\/p>\n<p>void* producer(void* arg) {<br \/>\n    while (1) {<br \/>\n        pthread_mutex_lock(&amp;mutex);<br \/>\n        while (count &#061;&#061; BUFFER_SIZE) {<br \/>\n            pthread_cond_wait(&amp;cond, &amp;mutex);<br \/>\n        }<br \/>\n        buffer[write_pos] &#061; rand() % 100;<br \/>\n        write_pos &#061; (write_pos &#043; 1) % BUFFER_SIZE;<br \/>\n        count&#043;&#043;;<br \/>\n        printf(&#034;Produced: %d\\\\n&#034;, buffer[write_pos]);<br \/>\n        pthread_cond_signal(&amp;cond);<br \/>\n        pthread_mutex_unlock(&amp;mutex);<br \/>\n        sleep(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>void* consumer(void* arg) {<br \/>\n    while (1) {<br \/>\n        pthread_mutex_lock(&amp;mutex);<br \/>\n        while (count &#061;&#061; 0) {<br \/>\n            pthread_cond_wait(&amp;cond, &amp;mutex);<br \/>\n        }<br \/>\n        int data &#061; buffer[read_pos];<br \/>\n        read_pos &#061; (read_pos &#043; 1) % BUFFER_SIZE;<br \/>\n        count&#8211;;<br \/>\n        printf(&#034;Consumed: %d\\\\n&#034;, data);<br \/>\n        pthread_cond_signal(&amp;cond);<br \/>\n        pthread_mutex_unlock(&amp;mutex);<br \/>\n        sleep(1);<br \/>\n    }<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    pthread_t prod, cons;<br \/>\n    pthread_mutex_init(&amp;mutex, NULL);<br \/>\n    pthread_cond_init(&amp;cond, NULL);<\/p>\n<p>    pthread_create(&amp;prod, NULL, producer, NULL);<br \/>\n    pthread_create(&amp;cons, NULL, consumer, NULL);<\/p>\n<p>    pthread_join(prod, NULL);<br \/>\n    pthread_join(cons, NULL);<\/p>\n<p>    pthread_mutex_destroy(&amp;mutex);<br \/>\n    pthread_cond_destroy(&amp;cond);<br \/>\n    return 0;<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u73af\u5f62\u7f13\u51b2\u533a\u4ee5\u53ca\u751f\u4ea7\u8005\u548c\u6d88\u8d39\u8005\u7ebf\u7a0b\u3002\u6211\u4eec\u4f7f\u7528\u4e92\u65a5\u9501\u6765\u4fdd\u62a4\u5bf9\u5171\u4eab\u7f13\u51b2\u533a\u7684\u8bbf\u95ee&#xff0c;\u5e76\u4f7f\u7528\u6761\u4ef6\u53d8\u91cf\u6765\u901a\u77e5\u751f\u4ea7\u8005\u6216\u6d88\u8d39\u8005\u7ebf\u7a0b\u4f55\u65f6\u8fdb\u884c\u64cd\u4f5c\u3002\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u5982\u4f55\u4f7f\u7528\u540c\u6b65\u673a\u5236\u786e\u4fdd\u7a0b\u5e8f\u7684\u6b63\u786e\u6027\u3002 <\/p>\n<h2> 5. \u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316\u4e0e\u9ad8\u53ef\u7528\u6027\u8bbe\u8ba1 <\/h2>\n<h3> 5.1 \u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316\u7b56\u7565 <\/h3>\n<p> \u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316\u662f\u4fdd\u969c\u7cfb\u7edf\u9ad8\u6548\u7a33\u5b9a\u8fd0\u884c\u7684\u5173\u952e\u3002\u9488\u5bf9\u6027\u80fd\u74f6\u9888&#xff0c;\u6709\u591a\u79cd\u4f18\u5316\u7b56\u7565\u53ef\u4ee5\u5b9e\u65bd\u3002 <\/p>\n<h4> 5.1.1 \u5185\u5b58\u7ba1\u7406\u4f18\u5316\u6280\u672f <\/h4>\n<p> \u5185\u5b58\u662f\u8ba1\u7b97\u673a\u4e2d\u6700\u91cd\u8981\u7684\u8d44\u6e90\u4e4b\u4e00&#xff0c;\u4f18\u5316\u5185\u5b58\u7ba1\u7406\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u6027\u80fd\u3002 <\/p>\n<p>\/\/ \u793a\u4f8b\u4ee3\u7801&#xff1a;\u5185\u5b58\u5206\u914d\u4f18\u5316<br \/>\nvoid* buffer &#061; malloc(SIZE);  \/\/ \u4f7f\u7528mallinfo\u6765\u83b7\u53d6\u5206\u914d\u524d\u540e\u7684\u5185\u5b58\u4f7f\u7528\u60c5\u51b5<br \/>\nif (buffer !&#061; NULL) {<br \/>\n    memset(buffer, 0, SIZE);<br \/>\n    \/\/ \u5904\u7406\u5185\u5b58\u6570\u636e&#8230;<br \/>\n    free(buffer);  \/\/ \u91ca\u653e\u5185\u5b58\u4ee5\u4f9b\u540e\u7eed\u4f7f\u7528<br \/>\n}<\/p>\n<p> \u901a\u8fc7  mallinfo  \u51fd\u6570\u53ef\u4ee5\u83b7\u5f97\u5185\u5b58\u5206\u914d\u524d\u540e\u7684\u8be6\u7ec6\u7edf\u8ba1\u4fe1\u606f&#xff0c;\u5e2e\u52a9\u5f00\u53d1\u8005\u8fdb\u884c\u6027\u80fd\u5206\u6790\u3002 <\/p>\n<h4> 5.1.2 \u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u6280\u672f\u624b\u6bb5 <\/h4>\n<p> \u4e0a\u4e0b\u6587\u5207\u6362\u662f\u591a\u4efb\u52a1\u7cfb\u7edf\u4e2d\u7684\u5e38\u89c1\u73b0\u8c61&#xff0c;\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u5207\u6362\u53ef\u4ee5\u63d0\u5347\u6548\u7387\u3002 <\/p>\n<p>\/\/ \u793a\u4f8b\u4ee3\u7801&#xff1a;\u51cf\u5c11\u4e0a\u4e0b\u6587\u5207\u6362\u7684\u65b9\u6cd5<br \/>\nvoid reduce_context_switches() {<br \/>\n    std::vector&lt;pthread_t&gt; threads;<br \/>\n    for (int i &#061; 0; i &lt; NUM_THREADS; &#043;&#043;i) {<br \/>\n        pthread_create(&amp;threads[i], NULL, worker_function, NULL);<br \/>\n    }<\/p>\n<p>    for (auto&amp; t : threads) {<br \/>\n        pthread_join(t, NULL);<br \/>\n    }<br \/>\n}<\/p>\n<p>void* worker_function(void* arg) {<br \/>\n    while (true) {<br \/>\n        \/\/ \u6267\u884c\u4efb\u52a1&#8230;<br \/>\n    }<br \/>\n}<\/p>\n<p> \u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d&#xff0c;\u907f\u514d\u521b\u5efa\u8fc7\u591a\u7ebf\u7a0b&#xff0c;\u4ece\u800c\u51cf\u5c11\u7ebf\u7a0b\u95f4\u7684\u7ade\u4e89\u548c\u4e0a\u4e0b\u6587\u5207\u6362\u3002 <\/p>\n<h4> 5.1.3 \u63d0\u9ad8\u7cfb\u7edf\u5e76\u53d1\u5904\u7406\u80fd\u529b\u7684\u65b9\u6cd5 <\/h4>\n<p> \u7cfb\u7edf\u5e76\u53d1\u5904\u7406\u80fd\u529b\u7684\u63d0\u5347\u662f\u6027\u80fd\u4f18\u5316\u7684\u91cd\u70b9&#xff0c;\u6d89\u53ca\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u6a21\u578b\u7684\u5408\u7406\u4f7f\u7528\u3002 <\/p>\n<p># \u793a\u4f8b\u4ee3\u7801&#xff1a;\u4f7f\u7528Python\u591a\u8fdb\u7a0b\u63d0\u9ad8\u5e76\u53d1\u5904\u7406\u80fd\u529b<br \/>\nfrom multiprocessing import Process, cpu_count<\/p>\n<p>def worker_process(task):<br \/>\n    # \u6267\u884c\u5177\u4f53\u4efb\u52a1<br \/>\n    print(f&#034;Processing {task}&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    tasks &#061; [i for i in range(100)]<br \/>\n    processes &#061; []<br \/>\n    for task in tasks:<br \/>\n        p &#061; Process(target&#061;worker_process, args&#061;(task,))<br \/>\n        processes.append(p)<br \/>\n        p.start()<br \/>\n    for p in processes:<br \/>\n        p.join()<\/p>\n<p> \u591a\u8fdb\u7a0b\u7684\u4f7f\u7528\u53ef\u4ee5\u5145\u5206\u5229\u7528CPU\u8d44\u6e90&#xff0c;\u5c24\u5176\u662f\u5728\u591a\u6838\u5904\u7406\u5668\u4e0a\u3002 <\/p>\n<h3> 5.2 \u9ad8\u53ef\u7528\u6027\u8bbe\u8ba1\u7b56\u7565 <\/h3>\n<p> \u9ad8\u53ef\u7528\u6027\u662f\u8861\u91cf\u7cfb\u7edf\u53ef\u9760\u6027\u7684\u91cd\u8981\u6307\u6807&#xff0c;\u5b83\u8981\u6c42\u7cfb\u7edf\u5728\u5404\u79cd\u60c5\u51b5\u4e0b\u90fd\u80fd\u6301\u7eed\u63d0\u4f9b\u670d\u52a1\u3002 <\/p>\n<h4> 5.2.1 \u8d1f\u8f7d\u5747\u8861\u6280\u672f\u7684\u539f\u7406\u4e0e\u5e94\u7528 <\/h4>\n<p> \u8d1f\u8f7d\u5747\u8861\u662f\u9ad8\u53ef\u7528\u6027\u8bbe\u8ba1\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u73af&#xff0c;\u5b83\u80fd\u6709\u6548\u5206\u914d\u8bf7\u6c42\u5230\u4e0d\u540c\u7684\u670d\u52a1\u5668\u3002 <\/p>\n<p># \u793a\u4f8b\u914d\u7f6e&#xff1a;Nginx\u4f5c\u4e3a\u8d1f\u8f7d\u5747\u8861\u5668\u7684\u914d\u7f6e<br \/>\nhttp {<br \/>\n    upstream backend {<br \/>\n        server backend1.example.com;<br \/>\n        server backend2.example.com;<br \/>\n        server backend3.example.com;<br \/>\n    }<\/p>\n<p>    server {<br \/>\n        location \/ {<br \/>\n            proxy_pass http:\/\/backend;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p> \u901a\u8fc7\u914d\u7f6eNginx\u7684  upstream  \u6a21\u5757&#xff0c;\u53ef\u4ee5\u5c06\u8bf7\u6c42\u5206\u53d1\u5230\u540e\u7aef\u591a\u4e2a\u670d\u52a1\u5668\u3002 <\/p>\n<h4> 5.2.2 \u707e\u96be\u6062\u590d\u4e0e\u6570\u636e\u5907\u4efd\u7b56\u7565 <\/h4>\n<p> \u4e3a\u786e\u4fdd\u7cfb\u7edf\u7684\u9ad8\u53ef\u7528\u6027&#xff0c;\u707e\u96be\u6062\u590d\u548c\u6570\u636e\u5907\u4efd\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\u3002 <\/p>\n<p># \u793a\u4f8b\u811a\u672c&#xff1a;\u4f7f\u7528rsync\u8fdb\u884c\u6570\u636e\u5907\u4efd<br \/>\n#!\/bin\/bash<br \/>\nDEST_DIR&#061;&#034;\/path\/to\/backup&#034;<br \/>\nSOURCE_DIR&#061;&#034;\/path\/to\/source&#034;<br \/>\nrsync -av $SOURCE_DIR $DEST_DIR<\/p>\n<p> \u5229\u7528  rsync  \u8fdb\u884c\u5b9a\u671f\u7684\u589e\u91cf\u5907\u4efd&#xff0c;\u662f\u6570\u636e\u5907\u4efd\u7684\u4e00\u79cd\u5e38\u89c1\u505a\u6cd5\u3002 <\/p>\n<h3> 5.3 \u5f02\u6b65I\/O\u6a21\u578b\u4e0e\u9ad8\u6548\u7f51\u7edc\u534f\u8bae <\/h3>\n<p> \u5728\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u8bbe\u8ba1\u4e2d&#xff0c;\u5f02\u6b65I\/O\u6a21\u578b\u548c\u9ad8\u6548\u7f51\u7edc\u534f\u8bae\u7684\u9009\u62e9\u5c24\u4e3a\u5173\u952e\u3002 <\/p>\n<h4> 5.3.1 AIO\u3001libevent\u3001libev\u7684\u539f\u7406\u4e0e\u6bd4\u8f83 <\/h4>\n<p> \u5f02\u6b65I\/O\u6a21\u578b\u5982AIO&#xff0c;\u4ee5\u53ca\u4e8b\u4ef6\u9a71\u52a8\u5e93\u5982libevent\u548clibev&#xff0c;\u4e3a\u6784\u5efa\u9ad8\u6027\u80fd\u7f51\u7edc\u5e94\u7528\u63d0\u4f9b\u4e86\u4fbf\u5229\u3002 <\/p>\n<p>\/\/ \u793a\u4f8b\u4ee3\u7801&#xff1a;\u4f7f\u7528libevent\u5b9e\u73b0\u4e8b\u4ef6\u9a71\u52a8\u7684\u7f51\u7edc\u901a\u4fe1<br \/>\nstruct event_base *base;<br \/>\nstruct evconnlistener *listener;<br \/>\nvoid accept_callback(struct evconnlistener *listener, evutil_socket_t fd, struct sockaddr *address, int socklen, void *user_data) {<br \/>\n    \/\/ \u63a5\u53d7\u8fde\u63a5\u5e76\u5904\u7406\u8bf7\u6c42<br \/>\n}<\/p>\n<p>int main() {<br \/>\n    base &#061; event_base_new();<br \/>\n    listener &#061; evconnlistener_new_bind(base, accept_callback, NULL, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, -1, (struct sockaddr*)&amp;local, sizeof(local));<\/p>\n<p>    event_base_dispatch(base);<br \/>\n    evconnlistener_free(listener);<br \/>\n    event_base_free(base);<br \/>\n    return 0;<br \/>\n}<\/p>\n<h4> 5.3.2 \u9ad8\u6548\u7f51\u7edc\u534f\u8bae\u7684\u9009\u62e9\u4e0e\u4f18\u5316\u5b9e\u4f8b <\/h4>\n<p> \u9009\u62e9\u5408\u9002\u7684\u7f51\u7edc\u534f\u8bae\u5e76\u8fdb\u884c\u4f18\u5316&#xff0c;\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7f51\u7edc\u5e94\u7528\u7684\u6027\u80fd\u3002 <\/p>\n<p>\/\/ \u793a\u4f8b\u4ee3\u7801&#xff1a;\u4f7f\u7528Go\u8bed\u8a00\u7684\u9ad8\u6548HTTP\u670d\u52a1\u5b9e\u73b0<br \/>\npackage main<\/p>\n<p>import (<br \/>\n    &#034;net\/http&#034;<br \/>\n    &#034;github.com\/prometheus\/client_golang\/prometheus\/promhttp&#034;<br \/>\n)<\/p>\n<p>func main() {<br \/>\n    http.Handle(&#034;\/metrics&#034;, promhttp.Handler())<br \/>\n    http.ListenAndServe(&#034;:8080&#034;, nil)<br \/>\n}<\/p>\n<p> Go\u8bed\u8a00\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684HTTP\u670d\u52a1\u5668\u652f\u6301&#xff0c;\u914d\u5408  promhttp  \u7b49\u5e93\u53ef\u4ee5\u8f7b\u677e\u642d\u5efa\u6027\u80fd\u4f18\u79c0\u7684\u76d1\u63a7\u670d\u52a1\u3002 <\/p>\n<p> \u901a\u8fc7\u4ee5\u4e0a\u7b56\u7565&#xff0c;\u6211\u4eec\u80fd\u591f\u6df1\u5165\u7406\u89e3\u670d\u52a1\u5668\u6027\u80fd\u4f18\u5316\u4e0e\u9ad8\u53ef\u7528\u6027\u8bbe\u8ba1\u7684\u5173\u952e\u8981\u70b9&#xff0c;\u4ece\u800c\u4fdd\u969cIT\u7cfb\u7edf\u7684\u6301\u7eed\u7a33\u5b9a\u8fd0\u884c\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b766d12.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p>\n<p> \u7b80\u4ecb&#xff1a;Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u662f\u9ad8\u6027\u80fd\u7f51\u7edc\u670d\u52a1\u7684\u5173\u952e\u6280\u672f&#xff0c;\u5b83\u6d89\u53ca\u591a\u7ebf\u7a0b\u7f16\u7a0b\u3001\u9ad8\u5e76\u53d1\u5904\u7406\u3001\u7ebf\u7a0b\u6c60\u7ba1\u7406\u3001\u540c\u6b65\u4e0e\u4e92\u65a5\u3001\u6027\u80fd\u4f18\u5316\u7b49\u6838\u5fc3\u6982\u5ff5\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u6784\u5efa\u9ad8\u6548\u7684\u670d\u52a1\u5668&#xff0c;\u5305\u62ec\u7ebf\u7a0b\u7ba1\u7406\u7b56\u7565\u3001\u4f7f\u7528\u7ebf\u7a0b\u6c60\u964d\u4f4e\u5f00\u9500\u3001\u91c7\u7528epoll\u6280\u672f\u63d0\u9ad8I\/O\u6548\u7387\u3001\u786e\u4fdd\u6570\u636e\u5b89\u5168\u7684\u540c\u6b65\u673a\u5236\u3001\u4fe1\u53f7\u91cf\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\u3001\u5185\u5b58\u4e0e\u8d1f\u8f7d\u4f18\u5316\u4ee5\u53ca\u5f02\u6b65I\/O\u6a21\u578b\u3002\u4e86\u89e3\u8fd9\u4e9b\u77e5\u8bc6\u70b9\u662f\u5f00\u53d1\u9ad8\u6027\u80fd\u3001\u9ad8\u53ef\u7528\u6027\u670d\u52a1\u5668\u7684\u57fa\u7840\u3002 <\/p>\n<p>  \u672c\u6587\u8fd8\u6709\u914d\u5957\u7684\u7cbe\u54c1\u8d44\u6e90&#xff0c;\u70b9\u51fb\u83b7\u53d6  <img decoding=\"async\" alt=\"menu-r.4af5f7ec.gif\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b766d12.gif\" style=\"width:16px;margin-left:4px;vertical-align:text-bottom;cursor:text\" \/> <\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb851\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u7ebf\u7a0b\u7684\u5b9a\u4e49\u6d89\u53ca\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u652f\u6301\uff0c\u5b83\u662f\u4e00\u79cd\u80fd\u591f\u88ab\u7cfb\u7edf\u72ec\u7acb\u8c03\u5ea6\u548c\u5206\u6d3e\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5b9e\u4f53\uff0c\u5176\u8c03\u5ea6\u5b8c\u5168\u7531\u7cfb\u7edf\u51b3\u5b9a\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u5fc5\u8981\u7684\u8d44\u6e90\uff0c\u5982\u7ebf\u7a0bID\u3001\u5bc4\u5b58\u5668\u96c6\u3001\u6808\u3001\u72b6\u6001\u7b49\u3002\u7ebf\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u548c\u4f18\u5148\u7ea7\u8c03\u5ea6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u673a\u4f1a\u6267\u884c\u3002int res;if (res!= 0) {\/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u3002<\/p>\n","protected":false},"author":2,"featured_media":37839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"topic":[],"class_list":["post-37841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/37841.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb851\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u7ebf\u7a0b\u7684\u5b9a\u4e49\u6d89\u53ca\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u652f\u6301\uff0c\u5b83\u662f\u4e00\u79cd\u80fd\u591f\u88ab\u7cfb\u7edf\u72ec\u7acb\u8c03\u5ea6\u548c\u5206\u6d3e\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5b9e\u4f53\uff0c\u5176\u8c03\u5ea6\u5b8c\u5168\u7531\u7cfb\u7edf\u51b3\u5b9a\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u5fc5\u8981\u7684\u8d44\u6e90\uff0c\u5982\u7ebf\u7a0bID\u3001\u5bc4\u5b58\u5668\u96c6\u3001\u6808\u3001\u72b6\u6001\u7b49\u3002\u7ebf\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u548c\u4f18\u5148\u7ea7\u8c03\u5ea6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u673a\u4f1a\u6267\u884c\u3002int res;if (res!= 0) {\/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/37841.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-17T22:12:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b766d12.gif\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37841.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/37841.html\",\"name\":\"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-17T22:12:08+00:00\",\"dateModified\":\"2025-05-17T22:12:08+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/37841.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/37841.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/37841.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/37841.html","og_locale":"zh_CN","og_type":"article","og_title":"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb851\u6b21\uff0c\u70b9\u8d5e22\u6b21\uff0c\u6536\u85cf17\u6b21\u3002\u7ebf\u7a0b\u7684\u5b9a\u4e49\u6d89\u53ca\u64cd\u4f5c\u7cfb\u7edf\u5185\u6838\u7684\u652f\u6301\uff0c\u5b83\u662f\u4e00\u79cd\u80fd\u591f\u88ab\u7cfb\u7edf\u72ec\u7acb\u8c03\u5ea6\u548c\u5206\u6d3e\u7684\u57fa\u672c\u5355\u4f4d\u3002\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u7ebf\u7a0b\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u8fdb\u7a0b\u5b9e\u4f53\uff0c\u5176\u8c03\u5ea6\u5b8c\u5168\u7531\u7cfb\u7edf\u51b3\u5b9a\u3002\u7ebf\u7a0b\u5728\u521b\u5efa\u65f6\uff0c\u7cfb\u7edf\u4f1a\u4e3a\u5176\u5206\u914d\u5fc5\u8981\u7684\u8d44\u6e90\uff0c\u5982\u7ebf\u7a0bID\u3001\u5bc4\u5b58\u5668\u96c6\u3001\u6808\u3001\u72b6\u6001\u7b49\u3002\u7ebf\u7a0b\u7684\u5de5\u4f5c\u539f\u7406\u57fa\u4e8e\u65f6\u95f4\u7247\u8f6e\u8f6c\u548c\u4f18\u5148\u7ea7\u8c03\u5ea6\uff0c\u786e\u4fdd\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u673a\u4f1a\u6267\u884c\u3002int res;if (res!= 0) {\/\/ \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/37841.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-17T22:12:08+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250517221207-682909b766d12.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/37841.html","url":"https:\/\/www.wsisp.com\/helps\/37841.html","name":"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-17T22:12:08+00:00","dateModified":"2025-05-17T22:12:08+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/37841.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/37841.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/37841.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Linux\u591a\u7ebf\u7a0b\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u8bbe\u8ba1\u4e0e\u5b9e\u73b0"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=37841"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/37841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/37839"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=37841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=37841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=37841"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=37841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}