{"id":1603,"date":"2025-12-18T21:21:11","date_gmt":"2025-12-18T13:21:11","guid":{"rendered":"https:\/\/heqiwei.cn\/?p=1603"},"modified":"2025-12-18T22:16:25","modified_gmt":"2025-12-18T14:16:25","slug":"%e3%80%8c%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e3%80%8dpv%e5%a4%a7%e9%a2%98%e7%bb%8f%e5%85%b8%e7%ae%97%e6%b3%95%e6%a8%a1%e7%89%88","status":"publish","type":"post","link":"https:\/\/heqiwei.cn\/index.php\/2025\/12\/18\/%e3%80%8c%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9f%e3%80%8dpv%e5%a4%a7%e9%a2%98%e7%bb%8f%e5%85%b8%e7%ae%97%e6%b3%95%e6%a8%a1%e7%89%88\/","title":{"rendered":"\u300c\u64cd\u4f5c\u7cfb\u7edf\u300dPV\u5927\u9898\u7ecf\u5178\u7b97\u6cd5\u6a21\u7248"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u7bc7\u9488\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u8003\u7814\u4e2d&nbsp;<strong>PV\u64cd\u4f5c\uff08\u4fe1\u53f7\u91cf\u673a\u5236\uff09<\/strong>&nbsp;\u7ecf\u5178\u5927\u9898\u7684\u89e3\u9898\u6a21\u677f\u603b\u7ed3\u3002\u5728\u8003\u8bd5\u4e2d\uff0c\u89e3\u7b54\u6b64\u7c7b\u9898\u76ee\u901a\u5e38\u9700\u8981\u9075\u5faa&nbsp;<strong>\u201c\u5b9a\u4e49\u4fe1\u53f7\u91cf -&gt; \u7f16\u5199\u8fdb\u7a0b\u4f2a\u4ee3\u7801<\/strong>\u201d&nbsp;\u7684\u6b65\u9aa4\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u56db\u5927\u7ecf\u5178\u6a21\u578b\u7684\u6807\u51c6\u89e3\u9898\u6a21\u677f\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u578b<\/h3>\n\n\n\n<p>\u8fd9\u662f\u6700\u57fa\u7840\u4e5f\u662f\u6700\u91cd\u8981\u7684\u6a21\u578b\uff0c\u6838\u5fc3\u5728\u4e8e\u5904\u7406<strong>\u7f13\u51b2\u533a<\/strong>\u7684\u4e92\u65a5\u8bbf\u95ee\u4ee5\u53ca<strong>\u7f13\u51b2\u533a\u6ee1\/\u7a7a<\/strong>\u7684\u540c\u6b65\u95ee\u9898\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">&nbsp;\u89e3\u9898\u5173\u952e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4e92\u65a5\u4fe1\u53f7\u91cf (<code>mutex<\/code>)<\/strong>\uff1a\u4fdd\u62a4\u7f13\u51b2\u533a\uff0c\u9632\u6b62\u540c\u65f6\u5b58\u53d6\u3002<\/li>\n\n\n\n<li><strong>\u540c\u6b65\u4fe1\u53f7\u91cf (<code>full<\/code>\/<code>empty<\/code>)<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>full<\/code>\uff1a\u8868\u793a\u7f13\u51b2\u533a\u4e2d\u201c\u4ea7\u54c1\u201d\u7684\u6570\u91cf\uff08\u521d\u503c\u4e3a0\uff09\u3002<\/li>\n\n\n\n<li><code>empty<\/code>\uff1a\u8868\u793a\u7f13\u51b2\u533a\u4e2d\u201c\u7a7a\u4f4d\u201d\u7684\u6570\u91cf\uff08\u521d\u503c\u4e3aN\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6807\u51c6\u4ee3\u7801\u6a21\u677f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>semaphore mutex = 1;  \/\/ \u4e34\u754c\u533a\u4e92\u65a5\u4fe1\u53f7\u91cf\nsemaphore empty = N;  \/\/ \u7a7a\u7f13\u51b2\u533a\u6570\u91cf\nsemaphore full = 0;   \/\/ \u6ee1\u7f13\u51b2\u533a\u6570\u91cf\uff08\u4ea7\u54c1\u6570\uff09\n\n\/\/ \u751f\u4ea7\u8005\u8fdb\u7a0b\nvoid Producer() {\n    while(true) {\n        produce_item();      \/\/ \u751f\u4ea7\u4e00\u4e2a\u4ea7\u54c1\n\n        P(empty);            \/\/ \u7533\u8bf7\u4e00\u4e2a\u7a7a\u4f4d (\u82e5\u65e0\u7a7a\u4f4d\u5219\u963b\u585e)\n        P(mutex);            \/\/ \u7533\u8bf7\u8fdb\u5165\u7f13\u51b2\u533a (\u4e92\u65a5)\n        \n        put_item();          \/\/ \u5c06\u4ea7\u54c1\u653e\u5165\u7f13\u51b2\u533a\n        \n        V(mutex);            \/\/ \u9000\u51fa\u7f13\u51b2\u533a\n        V(full);             \/\/ \u91ca\u653e\u4e00\u4e2a\u4ea7\u54c1\u4fe1\u53f7 (\u901a\u77e5\u6d88\u8d39\u8005)\n    }\n}\n\n\/\/ \u6d88\u8d39\u8005\u8fdb\u7a0b\nvoid Consumer() {\n    while(true) {\n        P(full);             \/\/ \u7533\u8bf7\u4e00\u4e2a\u4ea7\u54c1 (\u82e5\u65e0\u4ea7\u54c1\u5219\u963b\u585e)\n        P(mutex);            \/\/ \u7533\u8bf7\u8fdb\u5165\u7f13\u51b2\u533a\n        \n        take_item();         \/\/ \u4ece\u7f13\u51b2\u533a\u53d6\u51fa\u4e00\u4e2a\u4ea7\u54c1\n        \n        V(mutex);            \/\/ \u9000\u51fa\u7f13\u51b2\u533a\n        V(empty);            \/\/ \u91ca\u653e\u4e00\u4e2a\u7a7a\u4f4d\u4fe1\u53f7 (\u901a\u77e5\u751f\u4ea7\u8005)\n        \n        consume_item();      \/\/ \u6d88\u8d39\u4ea7\u54c1\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u54f2\u5b66\u5bb6\u8fdb\u9910\u95ee\u9898<\/h3>\n\n\n\n<p>\u6838\u5fc3\u5728\u4e8e\u6b7b\u9501\uff08Deadlock\uff09<strong>\u7684\u9884\u9632\u3002\u5982\u679c\u662f\u666e\u901a\u7684\u6559\u79d1\u4e66\u5f0f\u89e3\u6cd5\uff08\u6bcf\u4eba\u5148\u62ff\u5de6\u518d\u62ff\u53f3\uff09\uff0c\u4f1a\u5bfc\u81f4\u6b7b\u9501\u3002\u8003\u7814\u4e2d\u901a\u5e38\u5efa\u8bae\u4f7f\u7528<\/strong>\u201c\u9650\u5236\u7b56\u7565\u201d<strong>\u6216<\/strong>\u201c\u5947\u5076\u5206\u5c42\u7b56\u7565\u201d\u6765\u4f5c\u4e3a\u6807\u51c6\u7b54\u6848\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u9898\u5173\u952e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8d44\u6e90\u4fe1\u53f7\u91cf<\/strong>\uff1a5\u6839\u7b77\u5b50\uff0c\u6bcf\u6839\u662f\u4e00\u4e2a\u4fe1\u53f7\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u6b7b\u9501\u89e3\u51b3<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u65b9\u6848A\uff1a\u4ec5\u5f53\u5de6\u53f3\u4e24\u6839\u7b77\u5b50\u90fd\u53ef\u7528\u65f6\u624d\u62ff\uff08\u9700\u8981\u5728\u4e00\u4e2a\u4e34\u754c\u533a\u5185\u5224\u65ad\uff09\u3002<\/li>\n\n\n\n<li>\u65b9\u6848B\uff1a\u9650\u5236\u6700\u591a4\u4eba\u540c\u65f6\u8fdb\u9910\u3002<\/li>\n\n\n\n<li><strong>\u65b9\u6848C\uff08\u63a8\u8350\u6a21\u677f\uff09<\/strong>\uff1a\u5947\u6570\u53f7\u54f2\u5b66\u5bb6\u5148\u62ff\u5de6\u8fb9\uff0c\u5076\u6570\u53f7\u54f2\u5b66\u5bb6\u5148\u62ff\u53f3\u8fb9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6807\u51c6\u4ee3\u7801\u6a21\u677f (\u5947\u5076\u7b56\u7565\u9632\u6b7b\u9501\u7248)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>semaphore chopstick&#91;5] = {1, 1, 1, 1, 1}; \/\/ 5\u6839\u7b77\u5b50\n\nvoid Philosopher(int i) { \/\/ i \u4e3a\u54f2\u5b66\u5bb6\u7f16\u53f7 0-4\n    while(true) {\n        think(); \/\/ \u601d\u8003\n        \n        if (i % 2 == 0) {\n            \/\/ \u5076\u6570\u53f7\u54f2\u5b66\u5bb6\uff1a\u5148\u62ff\u5de6(i)\uff0c\u540e\u62ff\u53f3((i+1)%5)\n            P(chopstick&#91;i]);\n            P(chopstick&#91;(i + 1) % 5]);\n        } else {\n            \/\/ \u5947\u6570\u53f7\u54f2\u5b66\u5bb6\uff1a\u5148\u62ff\u53f3((i+1)%5)\uff0c\u540e\u62ff\u5de6(i)\n            \/\/ \u8fd9\u6837\u6253\u7834\u4e86\u73af\u8def\u7b49\u5f85\u6761\u4ef6\n            P(chopstick&#91;(i + 1) % 5]);\n            P(chopstick&#91;i]);\n        }\n\n        eat(); \/\/ \u8fdb\u9910\n\n        \/\/ \u653e\u4e0b\u7b77\u5b50\uff08\u987a\u5e8f\u65e0\u6240\u8c13\uff0c\u901a\u5e38\u600e\u4e48\u62ff\u600e\u4e48\u653e\uff09\n        V(chopstick&#91;i]);\n        V(chopstick&#91;(i + 1) % 5]);\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u8bfb\u8005-\u5199\u8005\u6a21\u578b<\/h3>\n\n\n\n<p>\u6838\u5fc3\u5728\u4e8e\u5904\u7406<strong>\u201c\u8bfb-\u8bfb\u5141\u8bb8\uff0c\u8bfb-\u5199\u4e92\u65a5\uff0c\u5199-\u5199\u4e92\u65a5\u201d\u3002<\/strong>\u6700\u5e38\u8003\u7684\u662f<strong>\u201c\u8bfb\u8005\u4f18\u5148\u201d<\/strong>\uff08\u53ea\u8981\u6709\u8bfb\u8005\u5728\u8bfb\uff0c\u540e\u6765\u7684\u8bfb\u8005\u53ef\u4ee5\u76f4\u63a5\u8fdb\uff0c\u5199\u8005\u5fc5\u987b\u7b49\u6240\u6709\u8bfb\u8005\u8d70\u5b8c\uff09\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u9898\u5173\u952e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8bfb\u5199\u4e92\u65a5 (<code>rw<\/code>)<\/strong>\uff1a\u63a7\u5236\u5199\u8005\u4e0e\u8bfb\u8005\u7684\u4e92\u65a5\u3002<\/li>\n\n\n\n<li><strong>\u8ba1\u6570\u5668 (<code>count<\/code>)<\/strong>\uff1a\u8bb0\u5f55\u5f53\u524d\u8bfb\u8005\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u8ba1\u6570\u5668\u4e92\u65a5 (<code>mutex<\/code>)<\/strong>\uff1a\u4fdd\u62a4&nbsp;<code>count<\/code>&nbsp;\u53d8\u91cf\u7684\u4fee\u6539\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6807\u51c6\u4ee3\u7801\u6a21\u677f (\u8bfb\u8005\u4f18\u5148)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int count = 0;       \/\/ \u8bb0\u5f55\u5f53\u524d\u8bfb\u8005\u6570\u91cf\nsemaphore mutex = 1; \/\/ \u4fdd\u62a4 count \u53d8\u91cf\nsemaphore rw = 1;    \/\/ \u4fdd\u8bc1\u8bfb\u8005\u548c\u5199\u8005\u4e92\u65a5\u8bbf\u95ee\u6587\u4ef6\n\n\/\/ \u5199\u8005\u8fdb\u7a0b\nvoid Writer() {\n    while(true) {\n        P(rw);       \/\/ \u7533\u8bf7\u5199\u6743\u9650 (\u82e5\u6709\u8bfb\u8005\u6216\u5199\u8005\u5728\u64cd\u4f5c\u5219\u963b\u585e)\n        \n        writing();   \/\/ \u5199\u64cd\u4f5c\n        \n        V(rw);       \/\/ \u91ca\u653e\u5199\u6743\u9650\n    }\n}\n\n\/\/ \u8bfb\u8005\u8fdb\u7a0b\nvoid Reader() {\n    while(true) {\n        P(mutex);        \/\/ \u7533\u8bf7\u4fee\u6539 count\n        if (count == 0) {\n            P(rw);       \/\/ \u5982\u679c\u662f\u7b2c\u4e00\u4e2a\u8bfb\u8005\uff0c\u8d1f\u8d23\u9501\u4f4f\u5199\u8005\n        }\n        count++;\n        V(mutex);        \/\/ \u91ca\u653e count \u9501\n        \n        reading();       \/\/ \u8bfb\u64cd\u4f5c (\u591a\u4e2a\u8bfb\u8005\u53ef\u540c\u65f6\u5728\u6b64\u5904)\n        \n        P(mutex);        \/\/ \u7533\u8bf7\u4fee\u6539 count\n        count--;\n        if (count == 0) {\n            V(rw);       \/\/ \u5982\u679c\u662f\u6700\u540e\u4e00\u4e2a\u8bfb\u8005\uff0c\u8d1f\u8d23\u91ca\u653e\u5199\u8005\n        }\n        V(mutex);        \/\/ \u91ca\u653e count \u9501\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u7406\u53d1\u5e08\u6a21\u578b<\/h3>\n\n\n\n<p>\u8fd9\u662f\u4e00\u4e2a\u5178\u578b\u7684\u670d\u52a1\u6392\u961f\u6a21\u578b\u3002\u6838\u5fc3\u5728\u4e8e\u7406\u53d1\u5e08\u5728\u65e0\u987e\u5ba2\u65f6\u7761\u7720\uff0c\u987e\u5ba2\u6765\u65f6\u5524\u9192\u7406\u53d1\u5e08\uff1b\u82e5\u7406\u53d1\u5e08\u5fd9\uff0c\u987e\u5ba2\u5224\u65ad\u662f\u5426\u6709\u7a7a\u4f4d\u7b49\u5f85\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"> \u89e3\u9898\u5173\u952e<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53d8\u91cf&nbsp;<code>waiting<\/code><\/strong>\uff1a\u8bb0\u5f55\u7b49\u5f85\u7406\u53d1\u7684\u987e\u5ba2\u6570\u3002<\/li>\n\n\n\n<li><strong>\u4fe1\u53f7\u91cf&nbsp;<code>customers<\/code><\/strong>\uff1a\u8868\u793a\u7b49\u5f85\u7684\u987e\u5ba2\u6570\u91cf\uff08\u7528\u6765\u5524\u9192\u7406\u53d1\u5e08\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4fe1\u53f7\u91cf&nbsp;<code>barbers<\/code><\/strong>\uff1a\u8868\u793a\u7406\u53d1\u5e08\u7684\u72b6\u6001\uff080\u8868\u793a\u5fd9\u6216\u7761\uff0c1\u8868\u793a\u51c6\u5907\u597d\/\u7a7a\u95f2\uff0c\u6216\u8005\u7528\u6765\u963b\u585e\u987e\u5ba2\uff09\u3002<\/li>\n\n\n\n<li><strong>\u4e92\u65a5&nbsp;<code>mutex<\/code><\/strong>\uff1a\u4fdd\u62a4&nbsp;<code>waiting<\/code>&nbsp;\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p><strong>\u6807\u51c6\u4ee3\u7801\u6a21\u677f<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>semaphore customers = 0; \/\/ \u7b49\u5f85\u7406\u53d1\u7684\u987e\u5ba2\u6570 (\u540c\u6b65)\nsemaphore barbers = 0;   \/\/ \u7406\u53d1\u5e08\u7a7a\u95f2\u4fe1\u53f7 (\u540c\u6b65)\nsemaphore mutex = 1;     \/\/ \u4e92\u65a5\u8bbf\u95ee waiting \u53d8\u91cf\nint waiting = 0;         \/\/ \u7b49\u5f85\u6905\u4e0a\u7684\u4eba\u6570\nint CHAIRS = N;          \/\/ \u6905\u5b50\u603b\u6570\n\n\/\/ \u7406\u53d1\u5e08\u8fdb\u7a0b\nvoid Barber() {\n    while(true) {\n        P(customers);    \/\/ \u7b49\u5f85\u987e\u5ba2 (\u82e5\u65e0\u987e\u5ba2\u5219\u7761\u89c9\/\u963b\u585e)\n        P(mutex);        \/\/ \u7533\u8bf7\u8bbf\u95ee waiting\n        \n        waiting--;       \/\/ \u7b49\u5f85\u4eba\u6570 -1\n        V(barbers);      \/\/ \u53d1\u4fe1\u53f7\uff1a\u7406\u53d1\u5e08\u51c6\u5907\u597d\u4e86\/\u5f00\u59cb\u7406\u53d1\n        \n        V(mutex);        \/\/ \u91ca\u653e waiting \u8bbf\u95ee\u6743\n        \n        cut_hair();      \/\/ \u7406\u53d1 (\u975e\u4e34\u754c\u533a)\n    }\n}\n\n\/\/ \u987e\u5ba2\u8fdb\u7a0b\nvoid Customer() {\n    P(mutex);            \/\/ \u7533\u8bf7\u8bbf\u95ee waiting\n    \n    if (waiting &lt; CHAIRS) {\n        waiting++;       \/\/ \u8fd8\u6709\u7a7a\u4f4d\uff0c\u5750\u4e0b\u7b49\u5f85\n        V(customers);    \/\/ \u5524\u9192\u7406\u53d1\u5e08 (\u6216\u662f\u589e\u52a0\u4e00\u4e2a\u987e\u5ba2\u4fe1\u53f7)\n        V(mutex);        \/\/ \u91ca\u653e waiting \u8bbf\u95ee\u6743\n        \n        P(barbers);      \/\/ \u7b49\u5f85\u7406\u53d1\u5e08\u53eb\u53f7 (\u963b\u585e\u76f4\u5230\u7406\u53d1\u5e08\u7a7a\u95f2)\n        get_haircut();   \/\/ \u63a5\u53d7\u7406\u53d1\n    } else {\n        V(mutex);        \/\/ \u6ca1\u7a7a\u4f4d\u4e86\uff0c\u76f4\u63a5\u79bb\u5f00\n        leave();         \n    }\n}\n<\/code><\/pre>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\"><strong>\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684customer\u8fdb\u7a0b\u4e2d\u6ca1\u6709while(true)\u7684\u5faa\u73af\uff0c\u53ea\u6709\u4e00\u4e2aif-else\u7684\u5206\u652f\u5224\u65ad\u3002<\/strong><\/mark><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u7bc7\u9488\u5bf9\u64cd\u4f5c\u7cfb\u7edf\u8003\u7814\u4e2d&nbsp;PV\u64cd\u4f5c\uff08\u4fe1\u53f7\u91cf\u673a\u5236\uff09&nbsp;\u7ecf\u5178\u5927\u9898\u7684\u89e3\u9898\u6a21\u677f\u603b\u7ed3\u3002\u5728\u8003\u8bd5\u4e2d\uff0c\u89e3\u7b54 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1603","post","type-post","status-publish","format-standard","hentry","category-23"],"_links":{"self":[{"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/posts\/1603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1603"}],"version-history":[{"count":7,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/posts\/1603\/revisions"}],"predecessor-version":[{"id":1611,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/posts\/1603\/revisions\/1611"}],"wp:attachment":[{"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heqiwei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}