{"id":63725,"date":"2025-04-29T10:30:46","date_gmt":"2025-04-29T02:30:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63725\/"},"modified":"2025-04-29T10:30:46","modified_gmt":"2025-04-29T02:30:46","slug":"linux%e7%ba%bf%e7%a8%8b%e6%80%8e%e4%b9%88%e7%94%a8-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63725\/","title":{"rendered":"linux\u7ebf\u7a0b\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  linux \u7ebf\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff0c\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\uff0c\u53ef\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u591a\u4efb\u52a1\u5e76\u53d1\u6267\u884c\u3002\u4f7f\u7528 linux \u7ebf\u7a0b\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u7ebf\u7a0b\u3001\u7f16\u5199\u7ebf\u7a0b\u51fd\u6570\u3001\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u5e76\u91ca\u653e\u8d44\u6e90\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/04\/2024060414122370054.jpg\" class=\"aligncenter\" title=\"linux\u7ebf\u7a0b\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"linux\u7ebf\u7a0b\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>Linux \u7ebf\u7a0b\u4f7f\u7528\u6307\u5357<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f Linux \u7ebf\u7a0b\uff1f<\/strong><\/p>\n<p>Linux \u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff0c\u5b83\u4e0e\u5176\u4ed6\u7ebf\u7a0b\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\u3002\u7ebf\u7a0b\u4f7f\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002<\/p>\n<p><strong>Linux \u7ebf\u7a0b\u7684\u4f7f\u7528<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5728 Linux \u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff1a<\/p>\n<p><strong>1. \u521b\u5efa\u7ebf\u7a0b<\/strong><\/p>\n<pre>pthread_t tid;\nint ret = pthread_create(&amp;tid, NULL, thread_function, (void *)arg);\nif (ret != 0) {\n    perror(\"pthread_create\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li>pthread_create \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u7ebf\u7a0b\u3002<\/li>\n<li>tid \u662f\u7ebf\u7a0b ID\uff0c\u7528\u4e8e\u8bc6\u522b\u7ebf\u7a0b\u3002<\/li>\n<li>thread_function \u662f\u7ebf\u7a0b\u8981\u6267\u884c\u7684\u51fd\u6570\u3002<\/li>\n<li>arg \u662f\u4f20\u9012\u7ed9\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\uff08\u53ef\u9009\uff09\u3002<\/li>\n<\/ul>\n<p><strong>2. \u7ebf\u7a0b\u51fd\u6570<\/strong><\/p>\n<p>\u7ebf\u7a0b\u51fd\u6570\u662f\u7ebf\u7a0b\u6267\u884c\u4ee3\u7801\u7684\u51fd\u6570\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff08\u5982\u679c\u6ca1\u6709\u53c2\u6570\uff0c\u5219\u4e3a NULL\uff09\u3002<\/p>\n<pre>void *thread_function(void *arg) {\n    \/\/ \u7ebf\u7a0b\u4ee3\u7801\n    return NULL;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>3. \u7b49\u5f85\u7ebf\u7a0b<\/strong><\/p>\n<p>\u4e3b\u7ebf\u7a0b\u53ef\u4ee5\u4f7f\u7528 pthread_join \u51fd\u6570\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u3002<\/p>\n<pre>int ret = pthread_join(tid, NULL);\nif (ret != 0) {\n    perror(\"pthread_join\");\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>4. \u91ca\u653e\u8d44\u6e90<\/strong><\/p>\n<p>\u7ebf\u7a0b\u5b8c\u6210\u6267\u884c\u540e\uff0c\u5e94\u91ca\u653e\u4e0e\u8be5\u7ebf\u7a0b\u5173\u8054\u7684\u4efb\u4f55\u8d44\u6e90\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801<\/strong><\/p>\n<p>\u4ee5\u4e0b\u793a\u4f8b\u4ee3\u7801\u521b\u5efa\u4e86\u4e24\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6253\u5370\u4e00\u4e2a\u4e0d\u540c\u7684\u6d88\u606f\uff1a<\/p>\n<pre>#include &lt;pthread.h&gt;\n#include &lt;stdio.h&gt;\n\nvoid *thread1_function(void *arg) {\n    printf(\"Hello from thread 1!\n\");\n    return NULL;\n}\n\nvoid *thread2_function(void *arg) {\n    printf(\"Hello from thread 2!\n\");\n    return NULL;\n}\n\nint main() {\n    pthread_t tid1, tid2;\n\n    \/\/ \u521b\u5efa\u7ebf\u7a0b 1\n    int ret = pthread_create(&amp;tid1, NULL, thread1_function, NULL);\n    if (ret != 0) {\n        perror(\"pthread_create\");\n        return 1;\n    }\n\n    \/\/ \u521b\u5efa\u7ebf\u7a0b 2\n    ret = pthread_create(&amp;tid2, NULL, thread2_function, NULL);\n    if (ret != 0) {\n        perror(\"pthread_create\");\n        return 1;\n    }\n\n    \/\/ \u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\n    pthread_join(tid1, NULL);\n    pthread_join(tid2, NULL);\n\n    return 0;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7ebf\u7a0b\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8FDCServers\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>linux \u7ebf\u7a0b\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff0c\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\uff0c\u53ef\u5b9e\u73b0\u5e94\u7528\u7a0b\u5e8f\u7684\u591a\u4efb\u52a1\u5e76\u53d1\u6267\u884c\u3002\u4f7f\u7528 linux \u7ebf\u7a0b\u7684\u6b65\u9aa4\u5305\u62ec\uff1a\u521b\u5efa\u7ebf\u7a0b\u3001\u7f16\u5199\u7ebf\u7a0b\u51fd\u6570\u3001\u7b49\u5f85\u7ebf\u7a0b\u5b8c\u6210\u5e76\u91ca\u653e\u8d44\u6e90\u3002 Linux \u7ebf\u7a0b\u4f7f\u7528\u6307\u5357 \u4ec0\u4e48\u662f Linux \u7ebf\u7a0b\uff1f Linux \u7ebf\u7a0b\u662f\u64cd\u4f5c\u7cfb\u7edf\u7684\u8f7b\u91cf\u7ea7\u8fdb\u7a0b\uff0c\u5b83\u4e0e\u5176\u4ed6\u7ebf\u7a0b\u5171\u4eab\u76f8\u540c\u7684\u5185\u5b58\u7a7a\u95f4\u548c\u8d44\u6e90\u3002\u7ebf\u7a0b\u4f7f\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u5e76\u53d1\u6267\u884c\u591a\u4e2a\u4efb\u52a1\uff0c\u4ece\u800c\u63d0\u9ad8\u6027\u80fd\u548c\u54cd\u5e94\u80fd\u529b\u3002 Linux \u7ebf\u7a0b\u7684\u4f7f\u7528 \u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u5728 Linux \u4e2d\u521b\u5efa\u548c\u7ba1\u7406\u7ebf\u7a0b\uff1a 1. \u521b\u5efa\u7ebf\u7a0b pthread_t tid; int ret = pthread_create(&amp;tid, NULL, thread_function, (void *)arg); if (ret != 0) { perror(&#8220;pthread_create&#8221;); } \u767b\u5f55\u540e\u590d\u5236 pthread_create \u51fd\u6570\u7528\u4e8e\u521b\u5efa\u7ebf\u7a0b\u3002 tid \u662f\u7ebf\u7a0b ID\uff0c\u7528\u4e8e\u8bc6\u522b\u7ebf\u7a0b\u3002 thread_function \u662f\u7ebf\u7a0b\u8981\u6267\u884c\u7684\u51fd\u6570\u3002 arg \u662f\u4f20\u9012\u7ed9\u7ebf\u7a0b\u51fd\u6570\u7684\u53c2\u6570\uff08\u53ef\u9009\uff09\u3002 2. \u7ebf\u7a0b\u51fd\u6570 \u7ebf\u7a0b\u51fd\u6570\u662f\u7ebf\u7a0b\u6267\u884c\u4ee3\u7801\u7684\u51fd\u6570\u3002\u5b83\u63a5\u6536\u4e00\u4e2a\u53c2\u6570\uff08\u5982\u679c\u6ca1\u6709\u53c2\u6570\uff0c\u5219\u4e3a NULL\uff09\u3002 void *thread_function(void *arg) { \/\/ \u7ebf\u7a0b\u4ee3\u7801 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-63725","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=63725"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63725\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}