{"id":63333,"date":"2025-04-29T13:51:29","date_gmt":"2025-04-29T05:51:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/63333\/"},"modified":"2025-04-29T13:51:29","modified_gmt":"2025-04-29T05:51:29","slug":"%e8%a7%a3%e6%9e%90linux%e8%bf%9b%e7%a8%8b%e4%b8%ba%e4%bb%80%e4%b9%88%e4%bc%9a%e8%bf%9b%e5%85%a5%e7%9d%a1%e7%9c%a0%e6%a8%a1%e5%bc%8f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/63333\/","title":{"rendered":"\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/171099036561067.jpg\" class=\"aligncenter\" title=\"\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u63d2\u56fe\" alt=\"\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u4f1a\u56e0\u4e3a\u591a\u79cd\u539f\u56e0\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u3002\u7761\u7720\u6a21\u5f0f\u5305\u62ec\u7b49\u5f85\u8d44\u6e90\u3001\u7b49\u5f85\u4fe1\u53f7\u548c\u7b49\u5f85\u4e8b\u4ef6\u7b49\u60c5\u51b5\u3002\u672c\u6587\u5c06\u4ece\u8fd9\u51e0\u4e2a\u65b9\u9762\u8be6\u7ec6\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u3002<\/p>\n<p><strong>\u7b49\u5f85\u8d44\u6e90<\/strong><\/p>\n<p>\u8fdb\u7a0b\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8bbf\u95ee\u4e00\u4e9b\u5171\u4eab\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u5185\u5b58\u7b49\u3002\u5f53\u67d0\u4e00\u8d44\u6e90\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u6216\u8005\u88ab\u9501\u5b9a\u65f6\uff0c\u5f53\u524d\u8fdb\u7a0b\u5c31\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u7b49\u5f85\u8d44\u6e90\u7684\u91ca\u653e\u6216\u89e3\u9501\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u83b7\u53d6\u4e00\u4e2a\u9501\u8d44\u6e90\u7684\u8fc7\u7a0b\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;pthread.h&gt;\n\npthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;\nint shared_resource = 0;\n\nvoid* thread_function(void* arg) {\n    pthread_mutex_lock(&amp;amp;mutex);\n    shared_resource++;\n    pthread_mutex_unlock(&amp;amp;mutex);\n    return NULL;\n}\n\nint main() {\n    pthread_t thread;\n    pthread_create(&amp;amp;thread, NULL, thread_function, NULL);\n    \n    pthread_mutex_lock(&amp;amp;mutex);\n    while (shared_resource == 0) {\n        pthread_mutex_unlock(&amp;amp;mutex);\n        sched_yield(); \/\/ \u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u907f\u514d\u5fd9\u7b49\u5f85\n        pthread_mutex_lock(&amp;amp;mutex);\n    }\n    pthread_mutex_unlock(&amp;amp;mutex);\n\n    pthread_join(thread, NULL);\n    \n    return 0;\n}&lt;\/pthread.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u4e3b\u7ebf\u7a0b\u7b49\u5f85\u5b50\u7ebf\u7a0b\u83b7\u53d6\u9501\u8d44\u6e90\u540e\u624d\u80fd\u7ee7\u7eed\u6267\u884c\uff0c\u5f53\u5b50\u7ebf\u7a0b\u83b7\u53d6\u9501\u8d44\u6e90\u540e\uff0c\u4e3b\u7ebf\u7a0b\u5c31\u4f1a\u9000\u51fa\u7761\u7720\u72b6\u6001\u3002<\/p>\n<p><strong>\u7b49\u5f85\u4fe1\u53f7<\/strong><\/p>\n<p>\u8fdb\u7a0b\u53ef\u4ee5\u901a\u8fc7\u4fe1\u53f7\u4e0e\u5185\u6838\u3001\u5176\u4ed6\u8fdb\u7a0b\u8fdb\u884c\u901a\u4fe1\u3002\u5f53\u8fdb\u7a0b\u7b49\u5f85\u4fe1\u53f7\u5230\u8fbe\u65f6\uff0c\u4f1a\u8fdb\u5165\u7761\u7720\u72b6\u6001\u3002\u53ef\u4ee5\u901a\u8fc7sigwait()\u6216\u8005\u4fe1\u53f7\u5904\u7406\u51fd\u6570\u7b49\u65b9\u5f0f\u6765\u5904\u7406\u4fe1\u53f7\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528sigwait()\u51fd\u6570\u7b49\u5f85\u4fe1\u53f7\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;signal.h&gt;\n\nint main() {\n    sigset_t set;\n    int sig_no;\n    sigemptyset(&amp;amp;set);\n    sigaddset(&amp;amp;set, SIGUSR1);\n    sigprocmask(SIG_BLOCK, &amp;amp;set, NULL);\n    sigwait(&amp;amp;set, &amp;amp;sig_no);\n    printf(\"Received signal SIGUSR1\n\");\n    return 0;\n}&lt;\/signal.h&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u8fdb\u7a0b\u963b\u585eSIGUSR1\u4fe1\u53f7\uff0c\u5f53\u63a5\u6536\u5230\u8be5\u4fe1\u53f7\u65f6\uff0c\u5c31\u4f1a\u9000\u51fa\u7761\u7720\u72b6\u6001\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u7b49\u5f85\u4e8b\u4ef6<\/strong><\/p>\n<p>\u8fdb\u7a0b\u6709\u65f6\u5019\u9700\u8981\u7b49\u5f85\u67d0\u4e9b\u4e8b\u4ef6\u7684\u53d1\u751f\uff0c\u6bd4\u5982\u5b9a\u65f6\u5668\u8d85\u65f6\u3001IO\u4e8b\u4ef6\u5c31\u7eea\u7b49\u3002\u8fdb\u7a0b\u4f1a\u56e0\u4e3a\u7b49\u5f85\u4e8b\u4ef6\u800c\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff0c\u76f4\u5230\u4e8b\u4ef6\u53d1\u751f\u5e76\u4e14\u5524\u9192\u8fdb\u7a0b\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528IO\u591a\u8def\u590d\u7528\u7b49\u5f85\u4e8b\u4ef6\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>#include &lt;stdio.h&gt;\n#include &lt;sys&gt;\n\nint main() {\n    fd_set rfds;\n    struct timeval tv;\n    int retval;\n\n    FD_ZERO(&amp;amp;rfds);\n    FD_SET(0, &amp;amp;rfds);\n\n    tv.tv_sec = 5;\n    tv.tv_usec = 0;\n\n    retval = select(1, &amp;amp;rfds, NULL, NULL, &amp;amp;tv);\n    if (retval == -1) {\n        perror(\"select()\");\n    } else if (retval) {\n        printf(\"Data is available now.\n\");\n    } else {\n        printf(\"No data within five seconds.\n\");\n    }\n\n    return 0;\n}&lt;\/sys&gt;&lt;\/stdio.h&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u8fdb\u7a0b\u4f7f\u7528select()\u51fd\u6570\u7b49\u5f85\u6807\u51c6\u8f93\u5165\u662f\u5426\u6709\u6570\u636e\u53ef\u8bfb\uff0c\u5f53\u6570\u636e\u53ef\u8bfb\u6216\u8005\u7b49\u5f85\u8d85\u65f6\uff0c\u8fdb\u7a0b\u5c31\u4f1a\u88ab\u5524\u9192\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cLinux\u8fdb\u7a0b\u4f1a\u56e0\u4e3a\u7b49\u5f85\u8d44\u6e90\u3001\u7b49\u5f85\u4fe1\u53f7\u548c\u7b49\u5f85\u4e8b\u4ef6\u7b49\u539f\u56e0\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u3002\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u8fdb\u7a0b\u7684\u7761\u7720\u884c\u4e3a\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\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>\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u9700\u8981\u5177\u4f53\u4ee3\u7801\u793a\u4f8b \u5728Linux\u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u4f1a\u56e0\u4e3a\u591a\u79cd\u539f\u56e0\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u3002\u7761\u7720\u6a21\u5f0f\u5305\u62ec\u7b49\u5f85\u8d44\u6e90\u3001\u7b49\u5f85\u4fe1\u53f7\u548c\u7b49\u5f85\u4e8b\u4ef6\u7b49\u60c5\u51b5\u3002\u672c\u6587\u5c06\u4ece\u8fd9\u51e0\u4e2a\u65b9\u9762\u8be6\u7ec6\u89e3\u6790Linux\u8fdb\u7a0b\u4e3a\u4ec0\u4e48\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u5e76\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8bf4\u660e\u3002 \u7b49\u5f85\u8d44\u6e90 \u8fdb\u7a0b\u5728\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u9700\u8981\u8bbf\u95ee\u4e00\u4e9b\u5171\u4eab\u8d44\u6e90\uff0c\u5982\u6587\u4ef6\u3001\u7f51\u7edc\u8fde\u63a5\u3001\u5185\u5b58\u7b49\u3002\u5f53\u67d0\u4e00\u8d44\u6e90\u88ab\u5176\u4ed6\u8fdb\u7a0b\u5360\u7528\u6216\u8005\u88ab\u9501\u5b9a\u65f6\uff0c\u5f53\u524d\u8fdb\u7a0b\u5c31\u4f1a\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\uff0c\u7b49\u5f85\u8d44\u6e90\u7684\u91ca\u653e\u6216\u89e3\u9501\u3002 \u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u4e00\u4e2a\u7ebf\u7a0b\u7b49\u5f85\u83b7\u53d6\u4e00\u4e2a\u9501\u8d44\u6e90\u7684\u8fc7\u7a0b\uff1a #include &lt;stdio.h&gt; #include &lt;pthread.h&gt; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int shared_resource = 0; void* thread_function(void* arg) { pthread_mutex_lock(&amp;amp;mutex); shared_resource++; pthread_mutex_unlock(&amp;amp;mutex); return NULL; } int main() { pthread_t thread; pthread_create(&amp;amp;thread, NULL, thread_function, NULL); pthread_mutex_lock(&amp;amp;mutex); while (shared_resource == 0) { pthread_mutex_unlock(&amp;amp;mutex); sched_yield(); \/\/ \u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u907f\u514d\u5fd9\u7b49\u5f85 pthread_mutex_lock(&amp;amp;mutex); } pthread_mutex_unlock(&amp;amp;mutex); pthread_join(thread, NULL); return 0; }&lt;\/pthread.h&gt;&lt;\/stdio.h&gt; \u767b\u5f55\u540e\u590d\u5236 [&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-63333","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63333","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=63333"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/63333\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=63333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=63333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=63333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}