{"id":204,"date":"2020-08-22T22:08:48","date_gmt":"2020-08-22T14:08:48","guid":{"rendered":"https:\/\/kishere.gq\/?p=204"},"modified":"2021-01-14T10:53:43","modified_gmt":"2021-01-14T02:53:43","slug":"linux%e4%b9%8b%e8%b7%af-%e5%a4%9a%e7%ba%bf%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/blog.kishere.cn\/?p=204","title":{"rendered":"Linux\u4e4b\u8def\u2014\u2014\u591a\u7ebf\u7a0b"},"content":{"rendered":"<p>@<a href=\"\u591a\u7ebf\u7a0b\">toc<\/a><\/p>\n<h1>Linux\u4e2d\u7684\u7ebf\u7a0b\u4e0e\u591a\u7ebf\u7a0b<\/h1>\n<p>\u5728\u4e00\u4e2a\u7a0b\u5e8f\u91cc\u7684\u4e00\u4e2a\u6267\u884c\u8def\u7ebf\u5c31\u53eb\u505a\u7ebf\u7a0b(thread).\u66f4\u51c6\u786e\u7684\u5b9a\u4e49\u662f\uff1a\u7ebf\u7a0b\u662f\u201c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u90e8\u7684\u63a7\u5236\u5e8f\u5217\u201d. <strong>\u4e00\u5207\u8fdb\u7a0b\u81f3\u5c11\u90fd\u6709\u4e00\u4e2a\u6267\u884c\u7ebf\u7a0b, \u7ebf\u7a0b\u5728\u8fdb\u7a0b\u5185\u90e8\u8fd0\u884c<\/strong>, \u672c\u8d28\u662f\u5728\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u5185\u8fd0\u884c. \u5728Linux\u7cfb\u7edf\u4e2d, \u5728CPU\u773c\u4e2d, \u770b\u5230\u7684PCB\u90fd\u8981\u6bd4\u4f20\u7edf\u7684\u8fdb\u7a0b\u66f4\u52a0\u8f7b\u91cf\u5316. \u900f\u8fc7\u8fdb\u7a0b\u865a\u62df\u5730\u5740\u7a7a\u95f4, \u53ef\u4ee5\u770b\u5230\u8fdb\u7a0b\u7684\u5927\u90e8\u5206\u8d44\u6e90, \u5c06\u8fdb\u7a0b\u8d44\u6e90\u5408\u7406\u5206\u914d\u7ed9\u6bcf\u4e2a\u6267\u884c\u6d41, \u5c31\u5f62\u6210\u4e86\u7ebf\u7a0b\u6267\u884c\u6d41.<br \/>\n\u591a\u7ebf\u7a0b\u5c31\u662f\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u542b\u6709\u591a\u4e2a\u7ebf\u7a0b, \u5206\u522b\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c. \u6bd4\u5982\u5728\u5199\u535a\u5ba2\u65f6\u7528\u7f51\u9875\u64ad\u653e\u5668\u64ad\u653e\u300a\u5ff5\u8bd7\u4e4b\u738b\u300b. \u5408\u7406\u8fd0\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u8ba9\u6211\u4eec\u7684\u7528\u6237\u4f53\u9a8c\u5f97\u5230\u63d0\u5347. \u4e5f\u53ef\u4ee5\u63d0\u9ad8CPU\u7684\u8ba1\u7b97\u6548\u7387<\/p>\n<h2>\u7ebf\u7a0b\u7684\u7279\u70b9<\/h2>\n<p><strong>\u4f18\u70b9:<\/strong><br \/>\n<strong>1.<\/strong> \u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6240\u9700\u7684\u8d44\u6e90\u8981\u6bd4\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u5c0f\u5f97\u591a, \u4e0e\u8fdb\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u76f8\u6bd4, \u7ebf\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u505a\u7684\u5de5\u4f5c\u8981\u5c11\u5f88\u591a.<br \/>\n <strong>2.<\/strong> \u7ebf\u7a0b\u5360\u7528\u7684\u8d44\u6e90\u8981\u6bd4\u8fdb\u7a0b\u5c11\u5f88\u591a, \u80fd\u5145\u5206\u5229\u7528\u591a\u5904\u7406\u5668\u7684\u53ef\u5e76\u884c\u6570\u91cf, \u5728\u7b49\u5f85\u6162\u901fI\/O\u64cd\u4f5c\u7ed3\u675f\u7684\u540c\u65f6, \u7a0b\u5e8f\u53ef\u6267\u884c\u5176\u4ed6\u7684\u8ba1\u7b97\u4efb\u52a1.<br \/>\n <strong>3.<\/strong> \u8ba1\u7b97\u5bc6\u96c6\u578b\u5e94\u7528, \u4e3a\u4e86\u80fd\u5728\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e0a\u8fd0\u884c, \u5c06\u8ba1\u7b97\u5206\u89e3\u5230\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u73b0.<br \/>\n <strong>4.<\/strong> I\/O\u5bc6\u96c6\u578b\u5e94\u7528, \u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd, \u5c06I\/O\u64cd\u4f5c\u91cd\u53e0. \u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u7b49\u5f85\u4e0d\u540c\u7684I\/O\u64cd\u4f5c.<br \/>\n<strong>\u7f3a\u70b9:<\/strong><br \/>\n<strong>5.<\/strong> \u591a\u4e2a\u8ba1\u7b97\u5bc6\u96c6\u578b\u4f1a\u5360\u7528\u8f83\u591aCPU\u8d44\u6e90, \u8d85\u8fc7\u6838\u6570\u5bfc\u81f4\u6027\u80fd\u635f\u5931, \u589e\u52a0CPU\u7684\u5f00\u9500.<br \/>\n<strong>6.<\/strong> \u7ebf\u7a0b\u95f4\u6ca1\u6709\u4fdd\u62a4, \u53ef\u80fd\u4f1a\u5171\u4eab\u4e86\u4e0d\u5e94\u8be5\u5171\u4eab\u7684\u53d8\u91cf. \u4f7f\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u964d\u4f4e.<br \/>\n<strong>7.<\/strong> \u7ebf\u7a0b\u4e2d\u8c03\u7528\u67d0\u4e9b\u7cfb\u7edf\u51fd\u6570, \u4f1a\u5bf9\u6574\u4e2a\u8fdb\u7a0b\u751f\u6548, \u7ebf\u7a0b\u4e2d\u51fa\u73b0\u5f02\u5e38, \u53ef\u80fd\u4f1a\u5f71\u54cd\u6574\u4e2a\u8fdb\u7a0b.<\/p>\n<h2>\u7ebf\u7a0b\u7684\u5171\u4eab\u8d44\u6e90<\/h2>\n<p>\u7ebf\u7a0b\u5171\u7528\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4, \u5176\u4e2d\u7684<strong>\u6570\u636e\u6bb5, \u4ee3\u7801\u6bb5<\/strong>\u90fd\u662f\u5171\u4eab\u7684, \u5982\u679c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570, \u5728\u5404\u7ebf\u7a0b\u4e2d\u90fd\u53ef\u4ee5\u8c03\u7528, \u5982\u679c\u7535\u8111\u6905\u4e86\u5168\u5c40\u53d8\u91cf, \u90a3\u4e48\u6240\u6709\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u8bbf\u95ee.<br \/>\n\u540c\u65f6<strong>\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868, \u6bcf\u79cd\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f, \u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55, \u7528\u6237ID\u548c\u7ec4ID<\/strong>\u90fd\u662f\u5171\u4eab\u7684.<\/p>\n<h2>\u7ebf\u7a0b\u63a7\u5236<\/h2>\n<p>\u7ebf\u7a0b\u7684\u63a7\u5236\u51fd\u6570\u5927\u591a\u6570\u4ee5<strong>pthread<\/strong>\u5f00\u5934, \u5305\u542b\u5728&lt;pthread.h>\u5934\u6587\u4ef6\u4e2d.<\/p>\n<h3>\u521b\u5efa\u7ebf\u7a0b<\/h3>\n<pre><code class=\"language-cpp line-numbers\">#include&lt;pthread.h&gt;\nint pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg)\n\/\/\u7f16\u8bd1\u65f6\u9700\u8981\u52a0\u4e0a-lpthread\u9009\u9879\n<\/code><\/pre>\n<p><strong>\u53c2\u6570:<\/strong><\/p>\n<ul>\n<li>thread:\u8fd4\u56de\u7ebf\u7a0bID<\/li>\n<li>attr:\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u5c5e\u6027\uff0cattr\u4e3aNULL\u8868\u793a\u4f7f\u7528\u9ed8\u8ba4\u5c5e\u6027<\/li>\n<li>start_routine:\u662f\u4e2a\u51fd\u6570\u5730\u5740\uff0c\u7ebf\u7a0b\u542f\u52a8\u540e\u8981\u6267\u884c\u7684\u51fd\u6570<\/li>\n<li>arg:\u4f20\u7ed9\u7ebf\u7a0b\u542f\u52a8\u51fd\u6570\u7684\u53c2\u6570<\/li>\n<li>\u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de0\uff1b\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7801<\/li>\n<\/ul>\n<p>\u521b\u5efa\u7ebf\u7a0b\u7684\u4ee3\u7801:<\/p>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;unistd.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;pthread.h&gt;\nvoid *rout(void *arg) {\n    int i;\n    for( ; ; ) {\n        printf(\"I'am thread 1\\n\");\n        sleep(2);\n    }\n}\nint main( void ){\n    pthread_t tid;\n    int ret;\n    if ( (ret=pthread_create(&amp;tid, NULL, rout, NULL)) != 0 ) {\n        fprintf(stderr, \"pthread_create : %s\\n\", strerror(ret));\n        exit(EXIT_FAILURE);\n    }\n    int i;\n    for(; ; ) {\n        printf(\"I'am main thread\\n\");\n        sleep(1);\n    }\n    return 0;\n}\n\/\/\u6267\u884c\u7ed3\u679c\u4e3a:\u6bcf\u6253\u5370\u4e24\u4e2amain thread \u5c31\u6253\u5370\u4e00\u4e2athread1\n\/\/pthreads\u51fd\u6570\u51fa\u9519\u65f6\u4e0d\u4f1a\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cferrno\uff08\u800c\u5927\u90e8\u5206\u5176\u4ed6POSIX\u51fd\u6570\u4f1a\u8fd9\u6837\u505a\uff09\n\/\/\u800c\u662f\u5c06\u9519\u8bef\u4ee3\u7801\u901a\u8fc7\u8fd4\u56de\u503c\u8fd4\u56de\n<\/code><\/pre>\n<p>\u83b7\u53d6\u7ebf\u7a0bID<\/p>\n<pre><code class=\"language-cpp line-numbers\">pthread_t pthread_self(void);\n<\/code><\/pre>\n<p>pthread_t\u5728Linux\u4e0b\u7684\u672c\u8d28\u5c31\u662f\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e0a\u7684\u4e00\u4e2a\u5730\u5740. \u6307\u5411\u5171\u4eab\u533a\u4e2d\u7684\u52a8\u6001\u5e93\u4e2d\u7684\u4e00\u4e2a\u5730\u5740.<\/p>\n<h3>\u7ec8\u6b62\u7ebf\u7a0b<\/h3>\n<p>\u7ec8\u6b62\u7ebf\u7a0b\u6709\u4e09\u79cd\u65b9\u5f0f:<\/p>\n<ol>\n<li>\u4ece\u7ebf\u7a0b\u51fd\u6570\u4e2dreturn\u8fd4\u56de, \u4f46\u662f\u5bf9\u4e8e\u4e3b\u8fdb\u7a0b\u6765\u8bf4, main\u8fd4\u56de\u5c31\u610f\u5473\u7740\u7ed3\u675f\u8fdb\u7a0b.<\/li>\n<li>\u7ebf\u7a0b\u8c03\u7528pthread_exit(); \u7ec8\u6b62\u81ea\u5df1.<\/li>\n<li>\u7ebf\u7a0b\u8c03\u7528pthread_cancel()\u7ec8\u6b62\u540c\u4e00\u8fdb\u7a0b\u7684\u4e00\u4e2a\u7ebf\u7a0b.<\/li>\n<\/ol>\n<h3>\u7ebf\u7a0b\u7b49\u5f85<\/h3>\n<p>\u7ebf\u7a0b\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e, \u5176\u7a7a\u95f4\u5e76\u6ca1\u6709\u5b8c\u5168\u91ca\u653e, \u800c\u662f\u4f9d\u65e7\u7559\u5728\u5730\u5740\u7a7a\u95f4\u4e2d. \u8c03\u7528\u7ebf\u7a0b\u7b49\u5f85\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u6302\u8d77\u76f4\u5230\u7ebf\u7a0b\u9000\u51fa<\/p>\n<pre><code class=\"language-cpp line-numbers\">int pthread_join(pthread_t thread, void **value_ptr);\n<\/code><\/pre>\n<p>\u8c03\u7528pthread_join\u51fd\u6570\u7684\u7ebf\u7a0b\u5c06\u6302\u8d77\u7b49\u5f85, \u76f4\u5230id\u4e3athread\u7684\u7ebf\u7a0b\u7ec8\u6b62.<br \/>\n &#8211; thread:\u7ebf\u7a0bID<br \/>\n &#8211; value_ptr:\u5b83\u6307\u5411\u4e00\u4e2a\u6307\u9488\uff0c\u540e\u8005\u6307\u5411<strong>\u7ebf\u7a0b\u7684\u8fd4\u56de\u503c<\/strong>(\u4e0d\u662f\u672c\u51fd\u6570\u7684\u8fd4\u56de\u503c), \u5bf9\u4e8e\u4e0d\u540c\u7684\u8fdb\u7a0b\u7ed3\u675f\u65b9\u5f0f, \u5f97\u5230\u7684\u7ec8\u6b62\u72b6\u6001\u4e0d\u540c.<br \/>\n &#8211; \u8fd4\u56de\u503c: \u6210\u529f\u8fd4\u56de0, \u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7801.<\/p>\n<h3>\u5206\u79bb\u7ebf\u7a0b<\/h3>\n<p>\u65b0\u521b\u5efa\u7684\u7ebf\u7a0b\u90fd\u662fjionable\u7684\u7ebf\u7a0b\u9000\u51fa\u540e\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u7b49\u5f85, \u5426\u5219\u65e0\u6cd5\u91ca\u653e\u8d44\u6e90, \u9020\u6210\u7cfb\u7edf\u6cc4\u6f0f.<br \/>\n\u6709\u65f6\u5019\u6211\u4eec\u5e76\u4e0d\u5173\u5fc3\u7ebf\u7a0b\u7684\u8fd4\u56de\u503c, \u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u8ba9\u7cfb\u7edf\u5728\u7ebf\u7a0b\u7ed3\u675f\u540e, \u81ea\u52a8\u91ca\u653e\u8d44\u6e90.<\/p>\n<pre><code class=\"language-cpp line-numbers\">int pthread_detach(pthread_t thread);\/\/\u5bf9\u540c\u8fdb\u7a0b\u7684\u7ebf\u7a0b\u8fdb\u884c\u5206\u79bb\npthread_detach(pthread_self());\/\/\u5bf9\u81ea\u5df1\u8fdb\u884c\u5206\u79bb\n<\/code><\/pre>\n<h2>\u7ebf\u7a0b\u4e92\u65a5<\/h2>\n<ul>\n<li>\u4e34\u754c\u8d44\u6e90: \u591a\u7ebf\u7a0b\u6267\u884c\u6d41\u5171\u4eab\u7684\u8d44\u6e90\u5c31\u53eb\u505a\u4e34\u754c\u8d44\u6e90<\/li>\n<li>\u4e34\u754c\u533a: \u6bcf\u4e2a\u7ebf\u7a0b\u5185\u90e8, \u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u7684\u4ee3\u7801\uff0c\u5c31\u53eb\u505a\u4e34\u754c\u533a<\/li>\n<li>\u4e92\u65a5: \u4efb\u4f55\u65f6\u523b, \u4e92\u65a5\u4fdd\u8bc1\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u6267\u884c\u6d41\u8fdb\u5165\u4e34\u754c\u533a, \u8bbf\u95ee\u4e34\u754c\u8d44\u6e90, \u901a\u5e38\u5bf9\u4e34\u754c\u8d44\u6e90\u8d77\u4fdd\u62a4\u4f5c\u7528<\/li>\n<li>\u539f\u5b50\u6027: \u4e0d\u4f1a\u88ab\u4efb\u4f55\u8c03\u5ea6\u673a\u5236\u6253\u65ad\u7684\u64cd\u4f5c, \u8be5\u64cd\u4f5c\u53ea\u6709\u4e24\u6001, \u8981\u4e48\u5b8c\u6210, \u8981\u4e48\u672a\u5b8c\u6210<\/li>\n<\/ul>\n<h3>\u4e92\u65a5\u91cf<\/h3>\n<p>\u5927\u90e8\u5206\u60c5\u51b5\uff0c\u7ebf\u7a0b\u4f7f\u7528\u7684\u6570\u636e\u90fd\u662f\u5c40\u90e8\u53d8\u91cf\uff0c\u53d8\u91cf\u7684\u5730\u5740\u7a7a\u95f4\u5728\u7ebf\u7a0b\u6808\u7a7a\u95f4\u5185\uff0c\u8fd9\u79cd\u60c5\u51b5\uff0c\u53d8\u91cf\u5f52\u5c5e\u5355\u4e2a\u7ebf\u7a0b\uff0c\u5176\u4ed6\u7ebf\u7a0b\u65e0\u6cd5\u83b7\u5f97\u8fd9\u79cd\u53d8\u91cf\u3002<br \/>\n\u4f46\u6709\u65f6\u5019\uff0c\u5f88\u591a\u53d8\u91cf\u90fd\u9700\u8981\u5728\u7ebf\u7a0b\u95f4\u5171\u4eab\uff0c\u8fd9\u6837\u7684\u53d8\u91cf\u79f0\u4e3a\u5171\u4eab\u53d8\u91cf\uff0c\u53ef\u4ee5\u901a\u8fc7\u6570\u636e\u7684\u5171\u4eab\uff0c\u5b8c\u6210\u7ebf\u7a0b \u4e4b\u95f4\u7684\u4ea4\u4e92\u3002\u4f46\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u7684\u64cd\u4f5c\u5171\u4eab\u53d8\u91cf\uff0c\u4f1a\u5e26\u6765\u4e00\u4e9b\u95ee\u9898\u3002<br \/>\n\u6bd4\u5982:<\/p>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\nint ticket = 100;\nvoid *route(void *arg){\n    char *id = (char*)arg;\n    while ( 1 ) {\n        if ( ticket &gt; 0 ) {\n            usleep(1000);\n            printf(\"%s sells ticket:%d\\n\", id, ticket);\n            ticket--;\n        } else {\n            break;\n        }\n    }\n}\nint main( void ){\n    pthread_t t1, t2, t3, t4;\n\n    pthread_create(&amp;t1, NULL, route, \"thread 1\");\n    pthread_create(&amp;t2, NULL, route, \"thread 2\");\n    pthread_create(&amp;t3, NULL, route, \"thread 3\");\n    pthread_create(&amp;t4, NULL, route, \"thread 4\");\n    pthread_join(t1, NULL);\n    pthread_join(t2, NULL);\n    pthread_join(t3, NULL);\n    pthread_join(t4, NULL);\n}\n\u4e00\u6b21\u6267\u884c\u7ed3\u679c\uff1a\nthread 4 sells ticket:100\n...\nthread 4 sells ticket:1\nthread 2 sells ticket:0\nthread 1 sells ticket:-1\nthread 3 sells ticket:-2\n<\/code><\/pre>\n<p>\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u9700\u8981\u6ee1\u8db3\u4e09\u4e2a\u6761\u4ef6:<\/p>\n<ul>\n<li>\u4ee3\u7801\u5fc5\u987b\u8981\u6709\u4e92\u65a5\u884c\u4e3a: \u5f53\u4ee3\u7801\u8fdb\u5165\u4e34\u754c\u533a\u6267\u884c\u65f6, \u4e0d\u5141\u8bb8\u5176\u4ed6\u7ebf\u7a0b\u8fdb\u5165\u8be5\u4e34\u754c\u533a.<\/li>\n<li>\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8981\u6c42\u6267\u884c\u4e34\u754c\u533a\u7684\u4ee3\u7801, \u5e76\u4e14\u4e34\u754c\u533a\u6ca1\u6709\u7ebf\u7a0b\u5728\u6267\u884c, \u90a3\u4e48\u53ea\u80fd\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u8fdb\u5165\u8be5\u4e34\u754c\u533a.<\/li>\n<li>\u5982\u679c\u7ebf\u7a0b\u4e0d\u5728\u4e34\u754c\u533a\u4e2d\u6267\u884c, \u90a3\u4e48\u8be5\u7ebf\u7a0b\u4e0d\u80fd\u963b\u6b62\u5176\u4ed6\u7ebf\u7a0b\u8fdb\u5165\u4e34\u754c\u533a.<\/li>\n<\/ul>\n<h3>\u521d\u59cb\u5316\u4e92\u65a5\u91cf<\/h3>\n<p>\u521d\u59cb\u5316\u4e92\u65a5\u91cf\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n<ol>\n<li>\u65b9\u6cd51, \u9759\u6001\u5206\u914d:<\/li>\n<\/ol>\n<pre><code class=\"language-cpp line-numbers\">pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER\n<\/code><\/pre>\n<ol start=\"2\">\n<li>\u65b9\u6cd52\uff0c\u52a8\u6001\u5206\u914d:<\/li>\n<\/ol>\n<pre><code class=\"language-cpp line-numbers\">int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t\n*restrict attr);\n<\/code><\/pre>\n<p><strong>\u53c2\u6570\uff1a<\/strong><br \/>\n &#8211; mutex\uff1a\u8981\u521d\u59cb\u5316\u7684\u4e92\u65a5\u91cf<br \/>\n &#8211; attr\uff1aNULL<\/p>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n#include &lt;unistd.h&gt;\n#include &lt;pthread.h&gt;\n#include &lt;sched.h&gt;\nint ticket = 100;\npthread_mutex_t mutex;\nvoid *route(void *arg){\n    char *id = (char*)arg;\n    while ( 1 ) {\n        pthread_mutex_lock(&amp;mutex);\n        if ( ticket &gt; 0 ) {\n            usleep(1000);\n            printf(\"%s sells ticket:%d\\n\", id, ticket);\n            ticket--;\n            pthread_mutex_unlock(&amp;mutex);\n            \/\/ sched_yield(); \u653e\u5f03CPU\n        } \n        else {\n            pthread_mutex_unlock(&amp;mutex);\n            break;\n        }\n    }\n}\nint main( void )\n{\n    pthread_t t1, t2, t3, t4;\n    pthread_mutex_init(&amp;mutex, NULL);\n\n    pthread_create(&amp;t1, NULL, route, \"thread 1\");\n    pthread_create(&amp;t2, NULL, route, \"thread 2\");\n    pthread_create(&amp;t3, NULL, route, \"thread 3\");\n    pthread_create(&amp;t4, NULL, route, \"thread 4\");\n    pthread_join(t1, NULL);\n    pthread_join(t2, NULL);\n    pthread_join(t3, NULL);\n    pthread_join(t4, NULL);\n\n    pthread_mutex_destroy(&amp;mutex);\n}\n<\/code><\/pre>\n<h2>\u6b7b\u9501<\/h2>\n<p>\u6b7b\u9501\u662f\u6307\u5728\u4e00\u7ec4\u8fdb\u7a0b\u4e2d\u7684\u5404\u4e2a\u8fdb\u7a0b\u5747\u5360\u6709\u4e0d\u4f1a\u91ca\u653e\u7684\u8d44\u6e90\uff0c\u4f46\u56e0\u4e92\u76f8\u7533\u8bf7\u88ab\u5176\u4ed6\u8fdb\u7a0b\u6240\u7ad9\u7528\u4e0d\u4f1a\u91ca\u653e\u7684\u8d44\u6e90\u800c\u5904\u4e8e\u7684\u4e00\u79cd\u6c38\u4e45\u7b49\u5f85\u72b6\u6001.<\/p>\n<h3>\u6b7b\u9501\u7684\u56db\u4e2a\u6761\u4ef6<\/h3>\n<ul>\n<li>\u4e92\u65a5\u6761\u4ef6\uff1a\u4e00\u4e2a\u8d44\u6e90\u6bcf\u6b21\u53ea\u80fd\u88ab\u4e00\u4e2a\u6267\u884c\u6d41\u4f7f\u7528<\/li>\n<li>\u8bf7\u6c42\u4e0e\u4fdd\u6301\u6761\u4ef6\uff1a\u4e00\u4e2a\u6267\u884c\u6d41\u56e0\u8bf7\u6c42\u8d44\u6e90\u800c\u963b\u585e\u65f6\uff0c\u5bf9\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\u4fdd\u6301\u4e0d\u653e<\/li>\n<li>\u4e0d\u5265\u593a\u6761\u4ef6:\u4e00\u4e2a\u6267\u884c\u6d41\u5df2\u83b7\u5f97\u7684\u8d44\u6e90\uff0c\u5728\u672b\u4f7f\u7528\u5b8c\u4e4b\u524d\uff0c\u4e0d\u80fd\u5f3a\u884c\u5265\u593a<\/li>\n<li>\u5faa\u73af\u7b49\u5f85\u6761\u4ef6:\u82e5\u5e72\u6267\u884c\u6d41\u4e4b\u95f4\u5f62\u6210\u4e00\u79cd\u5934\u5c3e\u76f8\u63a5\u7684\u5faa\u73af\u7b49\u5f85\u8d44\u6e90\u7684\u5173\u7cfb<\/li>\n<\/ul>\n<h3>\u5982\u4f55\u907f\u514d\u6b7b\u9501<\/h3>\n<ul>\n<li>\u7834\u574f\u6b7b\u9501\u7684\u56db\u4e2a\u5fc5\u8981\u6761\u4ef6<\/li>\n<li>\u52a0\u9501\u987a\u5e8f\u4e00\u81f4<\/li>\n<li>\u907f\u514d\u9501\u672a\u91ca\u653e\u7684\u573a\u666f<\/li>\n<li>\u8d44\u6e90\u4e00\u6b21\u6027\u5206\u914d<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>@toc Linux\u4e2d\u7684\u7ebf\u7a0b\u4e0e\u591a\u7ebf\u7a0b \u5728\u4e00\u4e2a\u7a0b\u5e8f\u91cc\u7684\u4e00\u4e2a\u6267\u884c\u8def\u7ebf\u5c31\u53eb\u505a\u7ebf\u7a0b(thread).\u66f4\u51c6\u786e\u7684\u5b9a\u4e49\u662f\uff1a\u7ebf\u7a0b\u662f\u201c\u4e00\u4e2a\u8fdb\u7a0b\u5185\u90e8\u7684\u63a7\u5236\u5e8f\u5217\u201d. \u4e00\u5207\u8fdb\u7a0b\u81f3\u5c11\u90fd\u6709\u4e00\u4e2a\u6267\u884c\u7ebf\u7a0b, \u7ebf\u7a0b\u5728\u8fdb\u7a0b\u5185\u90e8\u8fd0\u884c, \u672c\u8d28\u662f\u5728\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u5185\u8fd0\u884c. \u5728Linux\u7cfb\u7edf\u4e2d, \u5728CPU\u773c\u4e2d, \u770b\u5230\u7684PCB\u90fd\u8981\u6bd4\u4f20\u7edf\u7684\u8fdb\u7a0b\u66f4\u52a0\u8f7b\u91cf\u5316. \u900f\u8fc7\u8fdb\u7a0b\u865a\u62df\u5730\u5740\u7a7a\u95f4, \u53ef\u4ee5\u770b\u5230\u8fdb\u7a0b\u7684\u5927\u90e8\u5206\u8d44\u6e90, \u5c06\u8fdb\u7a0b\u8d44\u6e90\u5408\u7406\u5206\u914d\u7ed9\u6bcf\u4e2a\u6267\u884c\u6d41, \u5c31\u5f62\u6210\u4e86\u7ebf\u7a0b\u6267\u884c\u6d41. \u591a\u7ebf\u7a0b\u5c31\u662f\u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d\u542b\u6709\u591a\u4e2a\u7ebf\u7a0b, \u5206\u522b\u6267\u884c\u4e0d\u540c\u7684\u52a8\u4f5c. \u6bd4\u5982\u5728\u5199\u535a\u5ba2\u65f6\u7528\u7f51\u9875\u64ad\u653e\u5668\u64ad\u653e\u300a\u5ff5\u8bd7\u4e4b\u738b\u300b. \u5408\u7406\u8fd0\u7528\u591a\u7ebf\u7a0b\u53ef\u4ee5\u8ba9\u6211\u4eec\u7684\u7528\u6237\u4f53\u9a8c\u5f97\u5230\u63d0\u5347. \u4e5f\u53ef\u4ee5\u63d0\u9ad8CPU\u7684\u8ba1\u7b97\u6548\u7387 \u7ebf\u7a0b\u7684\u7279\u70b9 \u4f18\u70b9: 1. \u521b\u5efa\u4e00\u4e2a\u65b0\u7ebf\u7a0b\u6240\u9700\u7684\u8d44\u6e90\u8981\u6bd4\u521b\u5efa\u4e00\u4e2a\u65b0\u8fdb\u7a0b\u5c0f\u5f97\u591a, \u4e0e\u8fdb\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u76f8\u6bd4, \u7ebf\u7a0b\u4e4b\u95f4\u7684\u5207\u6362\u9700\u8981\u64cd\u4f5c\u7cfb\u7edf\u505a\u7684\u5de5\u4f5c\u8981\u5c11\u5f88\u591a. 2. \u7ebf\u7a0b\u5360\u7528\u7684\u8d44\u6e90\u8981\u6bd4\u8fdb\u7a0b\u5c11\u5f88\u591a, \u80fd\u5145\u5206\u5229\u7528\u591a\u5904\u7406\u5668\u7684\u53ef\u5e76\u884c\u6570\u91cf, \u5728\u7b49\u5f85\u6162\u901fI\/O\u64cd\u4f5c\u7ed3\u675f\u7684\u540c\u65f6, \u7a0b\u5e8f\u53ef\u6267\u884c\u5176\u4ed6\u7684\u8ba1\u7b97\u4efb\u52a1. 3. \u8ba1\u7b97\u5bc6\u96c6\u578b\u5e94\u7528, \u4e3a\u4e86\u80fd\u5728\u591a\u5904\u7406\u5668\u7cfb\u7edf\u4e0a\u8fd0\u884c, \u5c06\u8ba1\u7b97\u5206\u89e3\u5230\u591a\u4e2a\u7ebf\u7a0b\u4e2d\u5b9e\u73b0. 4. I\/O\u5bc6\u96c6\u578b\u5e94\u7528, \u4e3a\u4e86\u63d0\u9ad8\u6027\u80fd, \u5c06I\/O\u64cd\u4f5c\u91cd\u53e0. \u7ebf\u7a0b\u53ef\u4ee5\u540c\u65f6\u7b49\u5f85\u4e0d\u540c\u7684I\/O\u64cd\u4f5c. \u7f3a\u70b9: 5. \u591a\u4e2a\u8ba1\u7b97\u5bc6\u96c6\u578b\u4f1a\u5360\u7528\u8f83\u591aCPU\u8d44\u6e90, \u8d85\u8fc7\u6838\u6570\u5bfc\u81f4\u6027\u80fd\u635f\u5931, \u589e\u52a0CPU\u7684\u5f00\u9500. 6. \u7ebf\u7a0b\u95f4\u6ca1\u6709\u4fdd\u62a4, \u53ef\u80fd\u4f1a\u5171\u4eab\u4e86\u4e0d\u5e94\u8be5\u5171\u4eab\u7684\u53d8\u91cf. \u4f7f\u7a0b\u5e8f\u7684\u5b89\u5168\u6027\u964d\u4f4e. 7. \u7ebf\u7a0b\u4e2d\u8c03\u7528\u67d0\u4e9b\u7cfb\u7edf\u51fd\u6570, \u4f1a\u5bf9\u6574\u4e2a\u8fdb\u7a0b\u751f\u6548, \u7ebf\u7a0b\u4e2d\u51fa\u73b0\u5f02\u5e38, \u53ef\u80fd\u4f1a\u5f71\u54cd\u6574\u4e2a\u8fdb\u7a0b. \u7ebf\u7a0b\u7684\u5171\u4eab\u8d44\u6e90 \u7ebf\u7a0b\u5171\u7528\u8fdb\u7a0b\u7684\u5730\u5740\u7a7a\u95f4, \u5176\u4e2d\u7684\u6570\u636e\u6bb5, \u4ee3\u7801\u6bb5\u90fd\u662f\u5171\u4eab\u7684, \u5982\u679c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570, \u5728\u5404\u7ebf\u7a0b\u4e2d\u90fd\u53ef\u4ee5\u8c03\u7528, \u5982\u679c\u7535\u8111\u6905\u4e86\u5168\u5c40\u53d8\u91cf, \u90a3\u4e48\u6240\u6709\u7ebf\u7a0b\u90fd\u53ef\u4ee5\u8bbf\u95ee. \u540c\u65f6\u6587\u4ef6\u63cf\u8ff0\u7b26\u8868, \u6bcf\u79cd\u4fe1\u53f7\u7684\u5904\u7406\u65b9\u5f0f, \u5f53\u524d\u7684\u5de5\u4f5c\u76ee\u5f55, \u7528\u6237ID\u548c\u7ec4ID\u90fd\u662f\u5171\u4eab\u7684. \u7ebf\u7a0b\u63a7\u5236 \u7ebf\u7a0b\u7684\u63a7\u5236\u51fd\u6570\u5927\u591a\u6570\u4ee5pthread\u5f00\u5934, \u5305\u542b\u5728&lt;pthread.h>\u5934\u6587\u4ef6\u4e2d. \u521b\u5efa\u7ebf\u7a0b #include&lt;pthread.h&gt; int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg) \/\/\u7f16\u8bd1\u65f6\u9700\u8981\u52a0\u4e0a-lpthread\u9009\u9879 \u53c2\u6570: thread:\u8fd4\u56de\u7ebf\u7a0bID attr:\u8bbe\u7f6e\u7ebf\u7a0b\u7684\u5c5e\u6027\uff0cattr\u4e3aNULL\u8868\u793a\u4f7f\u7528\u9ed8\u8ba4\u5c5e\u6027 start_routine:\u662f\u4e2a\u51fd\u6570\u5730\u5740\uff0c\u7ebf\u7a0b\u542f\u52a8\u540e\u8981\u6267\u884c\u7684\u51fd\u6570 arg:\u4f20\u7ed9\u7ebf\u7a0b\u542f\u52a8\u51fd\u6570\u7684\u53c2\u6570 \u8fd4\u56de\u503c\uff1a\u6210\u529f\u8fd4\u56de0\uff1b\u5931\u8d25\u8fd4\u56de\u9519\u8bef\u7801 \u521b\u5efa\u7ebf\u7a0b\u7684\u4ee3\u7801: #include &lt;unistd.h&gt; #include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;pthread.h&gt; void *rout(void *arg) { int i; for( ; ; ) { printf(&#8220;I&#8217;am thread 1\\n&#8221;); sleep(2); } } int main( void ){ pthread_t tid; int ret; if ( (ret=pthread_create(&amp;tid, NULL, rout, NULL)) != 0 ) { fprintf(stderr, &#8220;pthread_create : %s\\n&#8221;, strerror(ret)); exit(EXIT_FAILURE); } int i; for(; ; ) { printf(&#8220;I&#8217;am main thread\\n&#8221;); sleep(1); } return 0; } \/\/\u6267\u884c\u7ed3\u679c\u4e3a:\u6bcf\u6253\u5370\u4e24\u4e2amain thread \u5c31\u6253\u5370\u4e00\u4e2athread1 \/\/pthreads\u51fd\u6570\u51fa\u9519\u65f6\u4e0d\u4f1a\u8bbe\u7f6e\u5168\u5c40\u53d8\u91cferrno\uff08\u800c\u5927\u90e8\u5206\u5176\u4ed6POSIX\u51fd\u6570\u4f1a\u8fd9\u6837\u505a\uff09 \/\/\u800c\u662f\u5c06\u9519\u8bef\u4ee3\u7801\u901a\u8fc7\u8fd4\u56de\u503c\u8fd4\u56de \u83b7\u53d6\u7ebf\u7a0bID pthread_t pthread_self(void); pthread_t\u5728Linux\u4e0b\u7684\u672c\u8d28\u5c31\u662f\u8fdb\u7a0b\u5730\u5740\u7a7a\u95f4\u4e0a\u7684\u4e00\u4e2a\u5730\u5740. \u6307\u5411\u5171\u4eab\u533a\u4e2d\u7684\u52a8\u6001\u5e93\u4e2d\u7684\u4e00\u4e2a\u5730\u5740. \u7ec8\u6b62\u7ebf\u7a0b \u7ec8\u6b62\u7ebf\u7a0b\u6709\u4e09\u79cd\u65b9\u5f0f: \u4ece\u7ebf\u7a0b\u51fd\u6570\u4e2dreturn\u8fd4\u56de, \u4f46\u662f\u5bf9\u4e8e\u4e3b\u8fdb\u7a0b\u6765\u8bf4, main\u8fd4\u56de\u5c31\u610f\u5473\u7740\u7ed3\u675f\u8fdb\u7a0b. \u7ebf\u7a0b\u8c03\u7528pthread_exit(); \u7ec8\u6b62\u81ea\u5df1. \u7ebf\u7a0b\u8c03\u7528pthread_cancel()\u7ec8\u6b62\u540c\u4e00\u8fdb\u7a0b\u7684\u4e00\u4e2a\u7ebf\u7a0b. \u7ebf\u7a0b\u7b49\u5f85 \u7ebf\u7a0b\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u540e, \u5176\u7a7a\u95f4\u5e76\u6ca1\u6709\u5b8c\u5168\u91ca\u653e, \u800c\u662f\u4f9d\u65e7\u7559\u5728\u5730\u5740\u7a7a\u95f4\u4e2d. \u8c03\u7528\u7ebf\u7a0b\u7b49\u5f85\u53ef\u4ee5\u8ba9\u7ebf\u7a0b\u6302\u8d77\u76f4\u5230\u7ebf\u7a0b\u9000\u51fa int pthread_join(pthread_t&#8230;<\/p>\n","protected":false},"author":1,"featured_media":198,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/posts\/204"}],"collection":[{"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=204"}],"version-history":[{"count":1,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/205"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=\/wp\/v2\/media\/198"}],"wp:attachment":[{"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kishere.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}