{"id":51701,"date":"2024-12-03T17:53:56","date_gmt":"2024-12-03T09:53:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/51701\/"},"modified":"2024-12-03T17:53:56","modified_gmt":"2024-12-03T09:53:56","slug":"linux%e8%bf%9b%e7%a8%8b%e4%b8%ba%e4%bd%95%e4%bc%9a%e9%99%b7%e5%85%a5%e7%9d%a1%e7%9c%a0%e7%8a%b6%e6%80%81%e7%9a%84%e8%a7%a3%e9%87%8a","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/51701\/","title":{"rendered":"Linux\u8fdb\u7a0b\u4e3a\u4f55\u4f1a\u9677\u5165\u7761\u7720\u72b6\u6001\u7684\u89e3\u91ca"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Linux\u8fdb\u7a0b\u4e3a\u4f55\u4f1a\u9677\u5165\u7761\u7720\u72b6\u6001\u7684\u89e3\u91ca<\/h1>\n<p>\u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u7684\u539f\u56e0\u591a\u6837\uff0c\u5305\u62ec\u7b49\u5f85\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u3001\u5185\u5b58\uff09\u3001\u4fe1\u53f7\uff08\u5982 SIGUSR1\uff09\u548c\u4e8b\u4ef6\uff08\u5982\u5b9a\u65f6\u5668\u8d85\u65f6\u3001IO \u4e8b\u4ef6\u5c31\u7eea\uff09\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u6790\u8fd9\u4e09\u4e2a\u65b9\u9762\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u8fdb\u7a0b\u5982\u4f55\u8fdb\u5165\u548c\u9000\u51fa\u7761\u7720\u72b6\u6001\u3002\u901a\u8fc7\u7406\u89e3\u8fdb\u7a0b\u7684\u7761\u7720\u884c\u4e3a\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u4f18\u5316\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002<\/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;mutex);\n    shared_resource++;\n    pthread_mutex_unlock(&amp;mutex);\n    return NULL;\n}\n\nint main() {\n    pthread_t thread;\n    pthread_create(&amp;thread, NULL, thread_function, NULL);\n    \n    pthread_mutex_lock(&amp;mutex);\n    while (shared_resource == 0) {\n        pthread_mutex_unlock(&amp;mutex);\n        sched_yield(); \/\/ \u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u907f\u514d\u5fd9\u7b49\u5f85\n        pthread_mutex_lock(&amp;mutex);\n    }\n    pthread_mutex_unlock(&amp;mutex);\n\n    pthread_join(thread, NULL);\n    \n    return 0;\n}<\/pre>\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\u8fc7<code>sigwait()<\/code>\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\u7528<code>sigwait()<\/code>\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;set);\n    sigaddset(&amp;set, SIGUSR1);\n    sigprocmask(SIG_BLOCK, &amp;set, NULL);\n    sigwait(&amp;set, &amp;sig_no);\n    printf(\"Received signal SIGUSR1\n\");\n    return 0;\n}<\/pre>\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\/select.h&gt;\n\nint main() {\n    fd_set rfds;\n    struct timeval tv;\n    int retval;\n\n    FD_ZERO(&amp;rfds);\n    FD_SET(0, &amp;rfds);\n\n    tv.tv_sec = 5;\n    tv.tv_usec = 0;\n\n    retval = select(1, &amp;rfds, NULL, NULL, &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}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u8fdb\u7a0b\u4f7f\u7528<code>select()<\/code>\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>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aLinux\u8fdb\u7a0b\u4e3a\u4f55\u4f1a\u9677\u5165\u7761\u7720\u72b6\u6001\u7684\u89e3\u91ca\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8eLinux,\u7761\u7720,\u8fdb\u7a0b\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   Gorilla \u652f\u6301\u7684\u5185\u5bb9\u7c7b\u578b\u8fdc\u8fdc\u4e0d\u6b62 application\/json\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Linux\u8fdb\u7a0b\u4e3a\u4f55\u4f1a\u9677\u5165\u7761\u7720\u72b6\u6001\u7684\u89e3\u91ca \u5728 Linux \u7cfb\u7edf\u4e2d\uff0c\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u6a21\u5f0f\u7684\u539f\u56e0\u591a\u6837\uff0c\u5305\u62ec\u7b49\u5f85\u8d44\u6e90\uff08\u5982\u6587\u4ef6\u3001\u5185\u5b58\uff09\u3001\u4fe1\u53f7\uff08\u5982 SIGUSR1\uff09\u548c\u4e8b\u4ef6\uff08\u5982\u5b9a\u65f6\u5668\u8d85\u65f6\u3001IO \u4e8b\u4ef6\u5c31\u7eea\uff09\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u89e3\u6790\u8fd9\u4e09\u4e2a\u65b9\u9762\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u5c55\u793a\u8fdb\u7a0b\u5982\u4f55\u8fdb\u5165\u548c\u9000\u51fa\u7761\u7720\u72b6\u6001\u3002\u901a\u8fc7\u7406\u89e3\u8fdb\u7a0b\u7684\u7761\u7720\u884c\u4e3a\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u548c\u4f18\u5316\u7cfb\u7edf\u8d44\u6e90\uff0c\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u7a33\u5b9a\u6027\u3002 \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;mutex); shared_resource++; pthread_mutex_unlock(&amp;mutex); return NULL; } int main() { pthread_t thread; pthread_create(&amp;thread, NULL, thread_function, NULL); pthread_mutex_lock(&amp;mutex); while (shared_resource == 0) { pthread_mutex_unlock(&amp;mutex); sched_yield(); \/\/ \u4e3b\u52a8\u8ba9\u51faCPU\uff0c\u907f\u514d\u5fd9\u7b49\u5f85 pthread_mutex_lock(&amp;mutex); } pthread_mutex_unlock(&amp;mutex); [&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-51701","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51701","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=51701"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/51701\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=51701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=51701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=51701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}