{"id":400,"date":"2023-07-26T13:58:07","date_gmt":"2023-07-26T05:58:07","guid":{"rendered":"https:\/\/blog.taiyang.space\/?p=400"},"modified":"2023-07-26T13:58:08","modified_gmt":"2023-07-26T05:58:08","slug":"l7-%e8%bf%9b%e7%a8%8b%e5%90%8c%e6%ad%a5%e4%b8%8e%e4%bf%a1%e5%8f%b7%e9%87%8f","status":"publish","type":"post","link":"https:\/\/blog.taiyang.space\/?p=400","title":{"rendered":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf"},"content":{"rendered":"<h2 id=\"1\u4fe1\u53f7\u91cf\">1\u3001\u4fe1\u53f7\u91cf<\/h2>\n<h3 id=\"11-\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784\">1.1 \u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784<\/h3>\n<p>\u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u8bbe\u8ba1\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\u3002\u672c\u8282\u4e3b\u8981\u662f\u60f3\u901a\u8fc7\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u6765\u7406\u89e3\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784\u3002\u6211\u4eec\u5148\u770b\u770b\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784\uff1a<br \/>\n\u9996\u5148\u4fe1\u53f7\u91cf\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53<\/p>\n<pre><code class=\"language-c\" lang=\"c\">struct semaphore{\n\tchar *name;\/\/ \u4fe1\u53f7\u91cf\u7684\u540d\u5b57\uff08\u8fd9\u4e2a\u5176\u5b9e\u53ef\u6709\u53ef\u65e0\uff09\n    int value; \/\/ \u8bb0\u5f55\u8d44\u6e90\u4e2a\u6570,\u5c31\u50cf empty \u4e00\u6837\u3002\u8fd9\u4e2a\u503c\u4e0d\u4e00\u5b9a\u975e\u8981\u5927\u4e8e\u7b49\u4e8e0\uff0c\u4e5f\u53ef\u4ee5\u662f\u5c0f\u4e8e0\u7684\u6570\uff0c\u5728\u540e\u6587\u6709\u89e3\u91ca\u3002\n    PCB *queue;\/\/ \u8bb0\u5f55\u7b49\u5f85\u5728\u8be5\u4fe1\u53f7\u91cf\u4e0a\u7684\u8fdb\u7a0b\uff0c\u65b9\u4fbf\u4e4b\u540e\u8fdb\u884c\u5524\u9192\u3002\u53ef\u4ee5\u7406\u89e3\u8fd9\u662f\u4e2a\u961f\u5217\uff0c\u91cc\u9762\u53ef\u4ee5\u8bb0\u5f55\u5f88\u591a\u7ebf\u7a0b\u3002\n};\n<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u662f\u5bf9\u4fe1\u53f7\u91cf\u7684\u64cd\u4f5c\uff08P\/V \u64cd\u4f5c\u3002\u5176\u4e2dP\u662f\u6307\u6d88\u8d39\u8005\uff0c\u4f1a\u6d88\u8d39\u8d44\u6e90\uff1bV\u662f\u6307\u751f\u4ea7\u8005\uff0c\u4f1a\u751f\u4ea7\u8d44\u6e90\uff09<\/p>\n<pre><code class=\"language-c\" lang=\"c\">P(semaphore s){\n    s.value--;\n    if(s.value &lt; 0) \n    \tsleep(s.queue);  \/\/ \u5c06\u5f53\u524d\u7ebf\u7a0b\u7761\u7720\uff0c\u5e76\u5c06\u5f53\u524d\u7ebf\u7a0b\u8bb0\u5f55\u5230 s.queue \u961f\u5217\u4e2d\n}\n\nV(semaphore s){\n    s.value++;\n    if(s.value &lt;= 0)\n    \twakeup(s.queue);\/\/ \u5524\u9192 s.queue \u961f\u5217\u4e2d\u7684\u7ebf\u7a0b\u3002\n}\n<\/code><\/pre>\n<p>\u6709\u4e86\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784\u540e\uff0c\u6211\u4eec\u5728\u901a\u8fc7\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u6765\u7406\u89e3\u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784\u3002\u4e3a\u4e86\u65b9\u4fbf\u5206\u6790\uff0c\u4e0b\u9762\u7684\u4fe1\u53f7\u91cf\u4e0d\u4f7f\u7528\u7ed3\u6784\u4f53\u5b9a\u4e49\uff0c\u4fe1\u53f7\u91cf\u7684\u64cd\u4f5c\u51fd\u6570\uff08P\u3001V\uff09\u4e5f\u662f\u76f4\u63a5\u5185\u8054\u5230\u4ee3\u7801\u4e2d\uff0c\u4e0b\u9762\u4ee3\u7801\u4e2d\u5b9a\u4e49\u7684\u4e92\u65a5\u9501\uff0c\u662f\u4e3a\u4e86\u4fdd\u62a4\u4e34\u754c\u8d44\u6e90\uff0c\u81f3\u4e8e\u4e92\u65a5\u9501\u7684\u5b9e\u73b0\u539f\u7406\u4e2a\u4eba\u8ba4\u4e3a\u5e94\u8be5\u548c 1.2\u8282\uff1a\u4fe1\u53f7\u91cf\u4e34\u754c\u533a\u4fdd\u62a4\u7c7b\u4f3c\u3002<\/p>\n<pre><code class=\"language-c\" lang=\"c\">#define BUFFER_SIZE 10      \nitem buffer[BUFFER_SIZE];  \/\/ \u5faa\u73af\u961f\u5217\uff0c\u961f\u5c3e\u5165\uff0c\u961f\u9996\u51fa\uff08\u4e34\u754c\u8d44\u6e90\uff09\nint in = out = 0;          \/\/ in\u4e3a\u961f\u5c3e\uff0cout\u4e3a\u961f\u9996\n\nint empty = BUFFER_SIZE;   \/\/ empty\u4e3a\u961f\u5217\u4e2d\u7a7a\u4f4d\u7f6e\u7684\u4e2a\u6570\uff08\u4fe1\u53f7\u91cf\uff09\nint full = 0;              \/\/ full\u4e3a\u961f\u5217\u4e2d\u7684\u5143\u7d20\u4e2a\u6570\uff08\u4fe1\u53f7\u91cf\uff09\nmutex_def mutex;           \/\/ \u4e92\u65a5\u9501\n<\/code><\/pre>\n<p>\u751f\u4ea7\u8005\u8fdb\u7a0b\u4e0d\u65ad\u751f\u4ea7 item\uff0c\u5e76\u5c06\u5176\u5b58\u653e\u5230 buffer \u961f\u5217\u4e2d\uff0c\u6d88\u8d39\u8005\u8fdb\u7a0b\u5219\u4e0d\u65ad\u4ece buffer \u961f\u5217\u4e2d\u201c\u6d88\u8d39\u201d item\u3002\u4e3a\u4e86\u4fdd\u8bc1\u751f\u4ea7\u8005\u8fdb\u7a0b\u548c\u6d88\u8d39\u8005\u8fdb\u7a0b\u80fd\u591f\u6709\u5e8f\u63a8\u8fdb\uff0c\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u8ba1\u4e3a\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">\/*******\u751f\u4ea7\u8005\u8fdb\u7a0b*******\/\nwhile (true) {\n\tempty--;      \/\/ P \u64cd\u4f5c\uff0c\u751f\u4ea7\u8005\u6d88\u8d39 buffer \u4e2d\u7684\u7a7a\u4f4d\u7f6e\n    if(empty &lt; 0)\/\/\u82e5\u961f\u5217\u4e2d\u5df2\u7ecf\u6ca1\u6709\u7a7a\u4f4d\u7f6e\u4e86\u5219\u751f\u4ea7\u8005\u505c\u4e0b\n    \tsleep(\u751f\u4ea7\u8005\u8fdb\u7a0b);\n    \t\n    mutex.lock();   \/\/ \u4e0a\u9501\uff0c\u8fd9\u91cc\u4e0a\u9501\u4ec5\u4ec5\u662f\u4e3a\u4e86\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6267\u884c\u4e0b\u9762\u4e24\u6761\u8bed\u53e5\n    buffer[in] = item;\n    in = (in + 1) % BUFFER_SIZE;\n    mutex.unlock();   \/\/ \u89e3\u9501\n    \n    full++;       \/\/ V \u64cd\u4f5c\uff0c\u751f\u4ea7\u8005\u751f\u4ea7\u8d44\u6e90\u5e76\u653e\u5982\u4e86 buffer \u4e2d\n    if(full &lt;= 0)\/\/ \u60f3\u60f3\u8fd9\u91cc full&lt;=0 \u662f\u4ec0\u4e48\u610f\u601d\uff1f\n    \twakeup(\u6d88\u8d39\u8005\u8fdb\u7a0b);\n}\n\/*******\u6d88\u8d39\u8005\u8fdb\u7a0b*******\/\nwhile (true) {\n\tfull--;\n    if(full &lt; 0) \/\/ \u82e5\u961f\u5217\u4e3a\u7a7a\u5219\u6d88\u8d39\u8005\u505c\u4e0b\n    \tsleep(\u6d88\u8d39\u8005\u8fdb\u7a0b);\n    \t\n    mutex.lock();   \/\/ \u4e0a\u9501\n    item = buffer[out];\n    out = (out + 1) % BUFFER_SIZE;\n\tmutex.unlock();   \/\/ \u89e3\u9501\n\t\n    empty++;\n    \/\/ empty&lt;=0 \u8868\u793a\u961f\u5217\u5df2\u7ecf\u6ee1\u4e86\uff0c\u76ee\u524d\u8fd8\u6709\u751f\u4ea7\u8005\u8fdb\u7a0b\u5904\u4e8e\u7761\u7720\u72b6\u6001\uff0c\u4f46\u662f\u73b0\u5728\u5df2\u7ecf\u67091\u4e2a\u7a7a\u4f4d\u7f6e\u4e86\uff08empty++ \u4ea7\u751f\u4e86\u4e00\u4e2a\u7a7a\u4f4d\u7f6e\uff09\uff0c\u56e0\u6b64\u9700\u8981\u5524\u9192\u4e00\u4e2a\u751f\u4ea7\u8005\u8fdb\u7a0b\u3002\n    if(empty &lt;= 0)\n    \twakeup(\u751f\u4ea7\u8005\u8fdb\u7a0b);\n}\n<\/code><\/pre>\n<p>\u7a0b\u5e8f\u4e2d\u7684 empty \u548c full \u5c31\u662f\u4e24\u4e2a\u4fe1\u53f7\u91cf\u3002\u8fd9\u91cc\u4ee5 empty \u4e3a\u4f8b\u6765\u5206\u6790\u4fe1\u53f7\u91cf\u3002\u5728\u4e0a\u9762\u7684\u7a0b\u5e8f\u4e2d empty \u67092\u4e2a\u529f\u80fd\uff1a\uff081\uff09\u8bb0\u5f55\u961f\u5217\u4e2d\u7a7a\u4f4d\u7f6e\u7684\u4e2a\u6570\uff1b\uff082\uff09\u6839\u636e empty \u7684\u503c\u6765\u51b3\u5b9a\u662f\u5426\u7761\u7720\u6216\u5524\u9192\u751f\u4ea7\u8005\u8fdb\u7a0b\u3002\u4ec0\u4e48\u662f\u4fe1\u53f7\u91cf? \u8bb0\u5f55\u4e00\u4e9b\u4fe1\u606f(\u91cf)\uff0c\u5e76\u6839\u636e\u8fd9\u4e2a\u4fe1\u606f\u51b3\u5b9a\u7761\u7720\u8fd8\u662f\u5524\u9192(\u4fe1\u53f7)\u3002\u53ef\u4ee5\u770b\u51fa\u4fe1\u53f7\u91cf\u6709\u4e24\u4e2a\u90e8\u5206\uff0c\u91cf\u7528\u6765\u8bb0\u5f55\uff0c\u4fe1\u53f7\u7528\u6765sleep\u548cwakeup\u3002 \u76f8\u5bf9\u4e8e\u4fe1\u53f7\u53ea\u6709\u201c\u6709\u201c\u6216\u8005\u201d\u65e0\u201c\u4e24\u79cd\u542b\u4e49\u6765\u8bf4\uff0c\u4fe1\u53f7\u91cf\u6709\u7740\u66f4\u591a\u7684\u542b\u4e49\u3002empty \u7684\u503c\u6709\u4e09\u79cd\u60c5\u51b5\uff1a<\/p>\n<ol start=\"\">\n<li>empty &gt; 0\uff0c\u8868\u793a buffer \u4e2d\u8fd8\u6709 empty \u4e2a\u7a7a\u4f4d\u7f6e\uff1b<\/li>\n<li>empty == 0\uff0c\u8868\u793a buffer \u4e2d\u5df2\u7ecf\u6ca1\u6709\u7a7a\u4f4d\u7f6e\uff08\u961f\u5217\u5df2\u6ee1\uff09\uff1b<\/li>\n<li>empty &lt; 0\uff0c\u8868\u793a buffer \u4e2d\u8fd8\u7f3a empty \u4e2a\u7a7a\u4f4d\u7f6e\uff0c\u5373\u6709 -empty \u4e2a\u8fdb\u7a0b\u9700\u8981\u5411 buffer \u5b58\u653e\u751f\u4ea7\u597d\u7684 item\uff0c\u4e0d\u8fc7 buffer \u5df2\u7ecf\u6ca1\u6709\u7a7a\u4f4d\u7f6e\u4e86\uff0c\u6240\u4ee5\u8fd9 -empty \u4e2a\u8fdb\u7a0b\u4f1a\u8fdb\u5165\u7761\u7720\u72b6\u6001\uff1b<\/li>\n<\/ol>\n<h3 id=\"12-\u4fe1\u53f7\u91cf\u4e34\u754c\u533a\u4fdd\u62a4\">1.2 \u4fe1\u53f7\u91cf\u4e34\u754c\u533a\u4fdd\u62a4<\/h3>\n<p>\u5728\u4e0a\u4e00\u8282\u7684 P\\V \u64cd\u4f5c\u4e2d\uff0c s.value&#8211; \u548c s.value++ \u5728CPU\u4e2d\u5e76\u4e0d\u662f\u4e00\u6761\u6307\u4ee4\u5b8c\u6210\u7684\u3002\u5982 s.value&#8211; \u53ef\u80fd\u662f\u7531\u51e0\u6761\u6307\u4ee4\u7ec4\u6210\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">register = s.value;  \/\/ register\u4e3a\u5bc4\u5b58\u5668\nregister = register - 1;\ns.value = register;\n<\/code><\/pre>\n<p>\u82e5\u51e0\u4e2a\u8fdb\u7a0b\u540c\u65f6\u8c03\u7528\u4e86 P \u64cd\u4f5c\uff0c\u90a3\u4e48 s.value \u8fd0\u7b97\u7ed3\u679c\u53ef\u80fd\u5c31\u4f1a\u4ea7\u751f\u9519\u8bef\u3002\u56e0\u6b64\u5bf9 value \u7684\u52a0\u51cf\u8fd0\u7b97\u5e94\u8be5\u52a0\u9501\uff0c\u5373\u4ee5\u4e0a\u76843\u6761\u6307\u4ee4\u5e94\u8be5\u4e00\u6b21\u5b8c\u6210\uff0c\u4e2d\u95f4\u4e0d\u80fd\u88ab\u6253\u65ad\u3002\u5bf9\u4fe1\u53f7\u91cf\u4e34\u754c\u533a\u7684\u4fdd\u62a4\u67093\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ol start=\"\">\n<li>\u9762\u5305\u5e97\u7b97\u6cd5\u3002\u53ef\u4ee5\u6709\u7eaf\u8f6f\u4ef6\u5b9e\u73b0\uff0c\u7f3a\u70b9\u5c31\u662f\u6548\u7387\u4f4e<\/li>\n<li>\u5f00\u5173\u4e2d\u65ad\u3002\u5728\u5bf9 value \u8fdb\u884c\u52a0\u51cf\u8fd0\u7b97\u524d\uff0c\u5173\u95ed\u4e2d\u65ad\uff0c\u9632\u6b62\u5207\u6362\u5230\u5176\u4ed6\u8fdb\u7a0b\uff0c\u5728\u8fd0\u7b97\u7ed3\u675f\u540e\u518d\u6253\u5f00\u4e2d\u65ad\u3002\u8fd9\u4e2a\u529e\u6cd5\u5b9e\u73b0\u8d77\u6765\u7b80\u5355\u4e00\u4e9b\uff0c\u4f46\u662f\u53ea\u80fd\u518d\u5355CPU\u7cfb\u7edf\u4e2d\u4f7f\u7528\uff0c\u5bf9\u4e8e\u591aCPU\u7cfb\u7edf\u4e0d\u9002\u7528\u3002<\/li>\n<li>\u786c\u4ef6\u539f\u5b50\u6307\u4ee4\u6cd5\u3002\u505a\u4e00\u6761\u786c\u4ef6\u6307\u4ee4\uff0c\u7528\u6765\u68c0\u6d4b\u9501\u662f\u5426\u5df2\u7ecf\u88ab\u9501\u4e0a\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u7528\u4e8e\u591aCPU\u7cfb\u7edf\uff0c\u4f46\u662f\u8fd9\u4e2a\u9700\u8981\u786c\u4ef6\u652f\u6301\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0cTestAndSet() \u4e3a\u4e00\u6761\u6307\u4ee4\uff0c\u51fd\u6570\u5185\u7684\u64cd\u4f5c\u90fd\u662f\u4e00\u6761\u6307\u4ee4\u5b8c\u6210\uff0c\u8be5\u51fd\u6570\u7528\u4e8e\u68c0\u6d4b\u9501\u662f\u5426\u5df2\u7ecf\u88ab\u9501\u4e0a\u3002<\/li>\n<\/ol>\n<h4 id=\"121-\u8f6e\u6362\u6cd5\">1.2.1 \u8f6e\u6362\u6cd5<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249891.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<h4 id=\"122-peterson\u7b97\u6cd5\">1.2.2 Peterson\u7b97\u6cd5<\/h4>\n<p>\u7ed3\u5408\u4e86\u6807\u8bb0\u548c\u8f6e\u8f6c\u4e24\u79cd\u601d\u60f3<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249886.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<p>\u6807\u8bb0\u4e3a1\uff0c\u5e76\u4e14\u8be51\u5c311\u6267\u884c<\/p>\n<h4 id=\"123-\u9762\u5305\u5e97\u7b97\u6cd5\">1.2.3 \u9762\u5305\u5e97\u7b97\u6cd5<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249021.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<p>####1.2.4 \u5173\u4e2d\u65ad<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249014.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<h4 id=\"125-\u786c\u4ef6\u539f\u5b50\u6307\u4ee4\u6cd5\">1.2.5 \u786c\u4ef6\u539f\u5b50\u6307\u4ee4\u6cd5<\/h4>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249904.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<p>\u7528\u4e34\u754c\u533a\u4fdd\u62a4\u4fe1\u53f7\u91cf\uff0c\u7528\u4fe1\u53f7\u91cf\u5b9e\u73b0\u7ebf\u7a0b\u540c\u6b65<\/p>\n<h2 id=\"2linux011\u4e2d\u7684\u8fdb\u7a0b\u540c\u6b65\u6848\u4f8b\">2\u3001Linux0.11\u4e2d\u7684\u8fdb\u7a0b\u540c\u6b65\u6848\u4f8b<\/h2>\n<p>Linux0.11 \u4e2d\u867d\u7136\u6ca1\u6709\u5b9e\u73b0\u4fe1\u53f7\u91cf\uff0c\u4f46\u662f\u6709\u8fdb\u7a0b\u540c\u6b65\u7684\u5730\u65b9\u3002\u672c\u8282\u4ee5 Linux0.11 \u4e2d\u8bfb\u78c1\u76d8\u7684\u7a0b\u5e8f\u4e3a\u4f8b\uff0c\u770b\u770b Linux0.11 \u4e2d\u662f\u5982\u4f55\u505a\u5230\u8fdb\u7a0b\u540c\u6b65\u7684\u3002\u4e0b\u9762\u662f\u5173\u4e8e Linux0.11 \u8bbf\u95ee\u78c1\u76d8\u7684\u4ecb\u7ecd\uff1a<\/p>\n<p>Linux 0.11\u8bbf\u95ee\u78c1\u76d8\u7684\u57fa\u672c\u5904\u7406\u529e\u6cd5\u662f\u5728\u5185\u5b58\u4e2d\u5212\u51fa\u4e00\u6bb5<strong>\u78c1\u76d8\u7f13\u5b58<\/strong>\uff0c\u7528\u6765\u52a0\u5feb\u5bf9\u78c1\u76d8\u7684\u8bbf\u95ee\u3002\u8fdb\u7a0b\u63d0\u51fa\u7684\u78c1\u76d8\u8bbf\u95ee\u8bf7\u6c42\u9996\u5148\u8981\u5230\u78c1\u76d8\u7f13\u5b58\u4e2d\u53bb\u627e\uff0c\u5982\u679c\u627e\u5230\u76f4\u63a5\u8fd4\u56de\uff1b\u5982\u679c\u6ca1\u6709\u627e\u5230\u5219\u7533\u8bf7\u4e00\u6bb5\u7a7a\u95f2\u7684\u78c1\u76d8\u7f13\u5b58\uff0c\u4ee5\u8fd9\u6bb5\u78c1\u76d8\u7f13\u5b58\u4e3a\u53c2\u6570\u53d1\u8d77\u78c1\u76d8\u8bfb\u5199\u8bf7\u6c42\u3002\u8bf7\u6c42\u53d1\u51fa\u540e\uff0c\u8fdb\u7a0b\u8981\u7761\u7720\u7b49\u5f85\uff08\u56e0\u4e3a\u78c1\u76d8\u8bfb\u5199\u5f88\u6162\uff0c\u5e94\u8be5\u8ba9\u51faCPU\u8ba9\u5176\u4ed6\u8fdb\u7a0b\u6267\u884c\uff09\u3002\u8fd9\u79cd\u65b9\u6cd5\u662f\u8bb8\u591a\u64cd\u4f5c\u7cfb\u7edf\uff08\u5305\u62ec\u73b0\u4ee3Linux\u3001UNIX\u7b49\uff09\u91c7\u7528\u7684\u8f83\u901a\u7528\u7684\u65b9\u6cd5\u3002 \u2014\u2014\u6458\u53d6\u81ea\u5b9e\u9a8c\u6307\u5bfc\u4e66<\/p>\n<p>\u53d1\u51fa\u8bfb\u5199\u8bf7\u6c42\u7684\u51fd\u6570\u662f\u7531 make_request() \u5b8c\u6210\u7684\uff0cmake_request() \u4e2d\u4f1a\u8c03\u7528 lock_buffer() \u4f7f\u8fdb\u7a0b\u8fdb\u5165\u7761\u7720\u3002 lock_buffer() \u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">\/\/ bh \u4e3a\u7533\u8bf7\u7684\u5185\u5b58\u7f13\u51b2\u533a\uff0cbu\u4e2d\u8fd8\u6709\u4e00\u4e2a\u9501 b_lock\uff0c\u7528\u4e8e\u6807\u8bb0\u7f13\u51b2\u533a\u662f\u5426\u88ab\u9501\u5b9a\nstatic inline void lock_buffer(struct buffer_head * bh){\n    cli();                 \t\t\/\/ \u5173\u4e2d\u65ad\n    while (bh-&gt;b_lock)  \t\t\/\/ \u82e5 b_lock == 1 \u5219\u8fdb\u5165\u7761\u7720\n    \tsleep_on(&amp;bh-&gt;b_wait);  \/\/ \u5c06\u5f53\u524d\u8fdb\u7a0b\u7761\u7720\u5728bh-&gt;b_wait\n    bh-&gt;b_lock=1;          \t\t\/\/ \u7f13\u51b2\u533a\u4e0a\u9501\n    sti();                \t \t\/\/ \u5f00\u4e2d\u65ad\n}\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u51faLinux0.11\u662f\u5229\u7528\u5f00\u5173\u4e2d\u65ad\u6765\u4fdd\u62a4\u4fe1\u53f7\u91cf\u4e34\u754c\u533a\u7684\u3002sleep_on\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">\/\/ p \u662f\u7b49\u5f85\u961f\u5217\u7684\u961f\u5934\u6307\u9488\nvoid sleep_on(struct task_struct **p){\n\tstruct task_struct *tmp; \/\/ \u6ce8\u610ftmp\u662f\u518d\u5185\u6838\u6808\u4e2d\uff0c\u800c\u6bcf\u4e00\u4e2a\u8fdb\u7a0b\u90fd\u6709\u4e00\u4e2a\u5185\u6838\u6808\u3002\n...\n\ttmp = *p;     \/\/tmp\u6307\u5411\u961f\u5934\n\t*p = current; \/\/p\u6307\u5411\u5f53\u524d\u6307\u9488\n\tcurrent-&gt;state = TASK_UNINTERRUPTIBLE;   \/\/ \u5c06\u5f53\u524d\u8fdb\u7a0b\u8bbe\u4e3a\u963b\u585e\u6001\n\tschedule();   \/\/\u5207\u5230\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\u53bb\u6267\u884c\n\t\/\/ \u5f53\u524d\u8fdb\u7a0b\u88ab\u5524\u9192\u540e\uff0c\u5c31\u4f1a\u4ece\u8fd9\u91cc\u91cd\u65b0\u5f00\u59cb\u6267\u884c\u3002\u82e5 tmp \u4e0d\u4e3a\u7a7a\u5219\u5c06tmp\u8fdb\u7a0b\u6539\u4e3a\u5c31\u7eea\u6001\u3002\n\t\/\/ \u5f53tmp\u53d8\u4e3a\u8fd0\u884c\u6001\u540e\uff0c\u53c8\u4f1a\u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c\uff0c\u4ece\u800c\u53c8\u5524\u9192tmp\u7684\u4e0b\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u5982\u6b64\u5f80\u590d\n\t\/\/ \u76f4\u5230\u5c06\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u5168\u90e8\u8fdb\u7a0b\u5168\u90e8\u5524\u9192\u3002\n\tif (tmp)     \n\t\ttmp-&gt;state=0;\n}\n<\/code><\/pre>\n<p>sleep_on() \u6bd4\u8f83\u96be\u7406\u89e3\uff0c\u5176\u662f\u5b83\u662f\u901a\u8fc7 tmp \u8fd9\u4e2a\u5c40\u90e8\u53d8\u91cf\u6765\u5f62\u6210\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u7684\u3002\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u56fe\u5c31\u5bb9\u6613\u7406\u89e3\u4e86\uff1a<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/picture.chentaiyang.space\/202307261249923.png\" referrerpolicy=\"no-referrer\" alt=\"img\"><\/p>\n<p>\u5f53\u8bf7\u6c42\u6210\u529f\uff0c\u5e76\u83b7\u53d6\u5230\u78c1\u76d8\u5185\u5bb9\u540e\uff0c\u78c1\u76d8\u4f1a\u5f15\u53d1\u7cfb\u7edf\u4e2d\u65ad\u6765\u5524\u9192\u8bf7\u6c42\u7684\u8fdb\u7a0b\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">static inline void unlock_buffer(struct buffer_head * bh){\n...\n    bh-&gt;b_lock = 0;       \/\/ \u89e3\u9501\u7f13\u51b2\u533a\n    wake_up(&amp;bh-&gt;b_wait); \/\/ \u5524\u9192\u8bf7\u6c42\u78c1\u76d8\u7684\u8fdb\u7a0b\n}\n<\/code><\/pre>\n<p>wake_up() \u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-c\" lang=\"c\">void wake_up(struct task_struct **p){\n\tif (p &amp;&amp; *p) {\n\t\t(**p).state=0;  \/\/ \u5c06 p \u7f6e\u4e3a\u5c31\u7eea\u6001\n\t\t*p=NULL;\n\t}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u4fe1\u53f7\u91cf 1.1 \u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784 \u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u8bbe\u8ba1\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[133,131,129,132],"class_list":["post-400","post","type-post","status-publish","format-standard","hentry","category-10","tag-peterson","tag-131","tag-129","tag-132"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0<\/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:\/\/blog.taiyang.space\/?p=400\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0\" \/>\n<meta property=\"og:description\" content=\"1\u3001\u4fe1\u53f7\u91cf 1.1 \u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784 \u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u8bbe\u8ba1\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\u3002 [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.taiyang.space\/?p=400\" \/>\n<meta property=\"og:site_name\" content=\"#\u762b\u7537\u65e5\u8bb0\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-26T05:58:07+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-26T05:58:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/picture.chentaiyang.space\/202307261249891.png\" \/>\n<meta name=\"author\" content=\"taiyang\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"taiyang\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blog.taiyang.space\/?p=400#article\",\"isPartOf\":{\"@id\":\"https:\/\/blog.taiyang.space\/?p=400\"},\"author\":{\"name\":\"taiyang\",\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c\"},\"headline\":\"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf\",\"datePublished\":\"2023-07-26T05:58:07+00:00\",\"dateModified\":\"2023-07-26T05:58:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blog.taiyang.space\/?p=400\"},\"wordCount\":82,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c\"},\"keywords\":[\"Peterson\u7b97\u6cd5\",\"\u4e34\u754c\u533a\",\"\u7ebf\u7a0b\u540c\u6b65\",\"\u9762\u5305\u5e97\u7b97\u6cd5\"],\"articleSection\":[\"\u64cd\u4f5c\u7cfb\u7edf\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blog.taiyang.space\/?p=400#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.taiyang.space\/?p=400\",\"url\":\"https:\/\/blog.taiyang.space\/?p=400\",\"name\":\"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0\",\"isPartOf\":{\"@id\":\"https:\/\/blog.taiyang.space\/#website\"},\"datePublished\":\"2023-07-26T05:58:07+00:00\",\"dateModified\":\"2023-07-26T05:58:08+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blog.taiyang.space\/?p=400#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.taiyang.space\/?p=400\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.taiyang.space\/?p=400#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/blog.taiyang.space\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.taiyang.space\/#website\",\"url\":\"https:\/\/blog.taiyang.space\/\",\"name\":\"#\u762b\u7537\u65e5\u8bb0\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.taiyang.space\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c\",\"name\":\"taiyang\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/blog.taiyang.space\/wp-content\/uploads\/2022\/10\/touxiang.jpg\",\"contentUrl\":\"https:\/\/blog.taiyang.space\/wp-content\/uploads\/2022\/10\/touxiang.jpg\",\"width\":612,\"height\":640,\"caption\":\"taiyang\"},\"logo\":{\"@id\":\"https:\/\/blog.taiyang.space\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/blog.taiyang.space\"],\"url\":\"https:\/\/blog.taiyang.space\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0","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:\/\/blog.taiyang.space\/?p=400","og_locale":"zh_CN","og_type":"article","og_title":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0","og_description":"1\u3001\u4fe1\u53f7\u91cf 1.1 \u4fe1\u53f7\u91cf\u7684\u57fa\u672c\u7ed3\u6784 \u5173\u4e8e\u5982\u4f55\u4f7f\u7528\u4fe1\u53f7\u91cf\u6765\u8bbe\u8ba1\u751f\u4ea7\u8005 &#8211; \u6d88\u8d39\u8005\u6a21\u578b\u8fd9\u91cc\u4e0d\u505a\u4ecb\u7ecd\u3002 [&hellip;]","og_url":"https:\/\/blog.taiyang.space\/?p=400","og_site_name":"#\u762b\u7537\u65e5\u8bb0","article_published_time":"2023-07-26T05:58:07+00:00","article_modified_time":"2023-07-26T05:58:08+00:00","og_image":[{"url":"https:\/\/picture.chentaiyang.space\/202307261249891.png"}],"author":"taiyang","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"taiyang","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.taiyang.space\/?p=400#article","isPartOf":{"@id":"https:\/\/blog.taiyang.space\/?p=400"},"author":{"name":"taiyang","@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c"},"headline":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf","datePublished":"2023-07-26T05:58:07+00:00","dateModified":"2023-07-26T05:58:08+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.taiyang.space\/?p=400"},"wordCount":82,"commentCount":0,"publisher":{"@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c"},"keywords":["Peterson\u7b97\u6cd5","\u4e34\u754c\u533a","\u7ebf\u7a0b\u540c\u6b65","\u9762\u5305\u5e97\u7b97\u6cd5"],"articleSection":["\u64cd\u4f5c\u7cfb\u7edf"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.taiyang.space\/?p=400#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.taiyang.space\/?p=400","url":"https:\/\/blog.taiyang.space\/?p=400","name":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf - #\u762b\u7537\u65e5\u8bb0","isPartOf":{"@id":"https:\/\/blog.taiyang.space\/#website"},"datePublished":"2023-07-26T05:58:07+00:00","dateModified":"2023-07-26T05:58:08+00:00","breadcrumb":{"@id":"https:\/\/blog.taiyang.space\/?p=400#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.taiyang.space\/?p=400"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.taiyang.space\/?p=400#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/blog.taiyang.space\/"},{"@type":"ListItem","position":2,"name":"L7-\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf"}]},{"@type":"WebSite","@id":"https:\/\/blog.taiyang.space\/#website","url":"https:\/\/blog.taiyang.space\/","name":"#\u762b\u7537\u65e5\u8bb0","description":"","publisher":{"@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.taiyang.space\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/5e3b9567310d4c4a328573992325178c","name":"taiyang","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/image\/","url":"https:\/\/blog.taiyang.space\/wp-content\/uploads\/2022\/10\/touxiang.jpg","contentUrl":"https:\/\/blog.taiyang.space\/wp-content\/uploads\/2022\/10\/touxiang.jpg","width":612,"height":640,"caption":"taiyang"},"logo":{"@id":"https:\/\/blog.taiyang.space\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/blog.taiyang.space"],"url":"https:\/\/blog.taiyang.space\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=400"}],"version-history":[{"count":1,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":401,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=\/wp\/v2\/posts\/400\/revisions\/401"}],"wp:attachment":[{"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.taiyang.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}