{"id":1125,"date":"2023-04-28T16:30:44","date_gmt":"2023-04-28T08:30:44","guid":{"rendered":"https:\/\/white-night.club\/?p=1125"},"modified":"2023-11-09T11:07:55","modified_gmt":"2023-11-09T03:07:55","slug":"study14","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/04\/28\/study14\/","title":{"rendered":"\u8fdb\u7a0b\u540c\u6b65\u7684\u57fa\u672c\u6982\u5ff5"},"content":{"rendered":"\n<p>\u5728\u8ba8\u8bba\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u7b49\u4e09\u79cd\u8fdb\u7a0b\u540c\u6b65\u95ee\u9898\u524d\uff0c\u6211\u4eec\u5148\u6765\u770b\u4e0b\u8fdb\u7a0b\u540c\u6b65\u7684\u76f8\u5173\u6982\u5ff5\uff1a<\/p>\n\n\n\n<ul>\n<li>\u4ec0\u4e48\u662f\u8fdb\u7a0b\u540c\u6b65\uff1f<\/li>\n\n\n\n<li>\u540c\u6b65\u673a\u5236\u7684\u56db\u6761\u89c4\u5219\u3002<\/li>\n\n\n\n<li>\u4ec0\u4e48\u662f\u4fe1\u53f7\u91cf?<\/li>\n<\/ul>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u4ec0\u4e48\u662f\u8fdb\u7a0b\u540c\u6b65\uff1f<\/p>\n<cite><em>\u5e76\u53d1\u8fdb\u7a0b\u6309\u89c4\u5219\u5171\u4eab\u8d44\u6e90<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u5f53\u591a\u4e2a\u7a0b\u5e8f\u5e76\u53d1\u6267\u884c\u65f6\uff0c\u5b83\u4eec\u7684\u5236\u7ea6\u5173\u7cfb\u53ef\u5206\u4e3a\u4ee5\u4e0b\u4e24\u79cd\uff1a<\/p>\n\n\n\n<ul>\n<li>\u95f4\u63a5\u76f8\u4e92\u5236\u7ea6\u5173\u7cfb-<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5171\u4eab\u7cfb\u7edf\u8d44\u6e90<\/mark><\/li>\n<\/ul>\n\n\n\n<p>\u6bd4\u5982A\u8fdb\u7a0b\u9700\u8981\u7528\u7cfb\u7edf\u7684\u6253\u5370\u673a\u8d44\u6e90\u6253\u5370\u4e1c\u897f\uff0cB\u8fdb\u7a0b\u4e5f\u9700\u8981\u4f7f\u7528\u6253\u5370\u673a\u8d44\u6e90\u6253\u5370\u4e1c\u897f\u3002\u800c\u6253\u5370\u673a\u4e00\u6b21\u53ea\u80fd\u6253\u5370\u4e00\u5f20\u7eb8\u3002\u90a3\u4e48A\uff0cB\u8fdb\u7a0b\u5fc5\u987b\u4e92\u65a5\u8bbf\u95ee\u6253\u5370\u673a\uff0c\u540c\u65f6\u8bbf\u95ee\u7684\u8bdd\u5c31\u4f1a\u5bfc\u81f4A\uff0cB\u8fdb\u7a0b\u8981\u6253\u5370\u7684\u5185\u5bb9\u6df7\u5728\u540c\u4e00\u5f20\u7eb8\u4e0a\u3002<\/p>\n\n\n\n<ul>\n<li>\u76f4\u63a5\u76f8\u4e92\u5236\u7ea6\u5173\u7cfb-<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4e3a\u5b8c\u6210\u540c\u4e00\u4efb\u52a1\u800c\u5408\u4f5c<\/mark><\/li>\n<\/ul>\n\n\n\n<p>\u6bd4\u5982A\u8fdb\u7a0b\u65b0\u5efa\u4e00\u4e2a\u6587\u672c\uff0c\u5e76\u5f80\u6587\u672c\u4e2d\u5199\u5165\u5185\u5bb9\u3002B\u8fdb\u7a0b\u7528\u6253\u5370\u673a\u8d44\u6e90\u6253\u5370\u8be5\u6587\u672c\u3002\u90a3\u4e48\u8be5\u6587\u672c\u5c31\u76f8\u5f53\u4e8e\u4e00\u4e2a\u7f13\u51b2\u533a\u3002A\u8fdb\u7a0b\u5f80\u7f13\u51b2\u533a\u91cc\u653e\u8d44\u6e90\uff0cB\u8fdb\u7a0b\u4ece\u7f13\u51b2\u533a\u4e2d\u53d6\u8d44\u6e90\u3002\u5982\u679c\u7f13\u51b2\u533a\u4e3a\u7a7a\u65f6\uff0cB\u8fdb\u7a0b\u65e0\u6cd5\u83b7\u5f97\u6240\u9700\u5185\u5bb9\u6545\u88ab\u963b\u585e\u3002\u5982\u679c\u7f13\u51b2\u533a\u4e3a\u6ee1\u65f6\uff0cA\u8fdb\u7a0b\u65e0\u6cd5\u5411\u5176\u4e2d\u5199\u5165\u5185\u5bb9\u800c\u88ab\u963b\u585e\u3002<\/p>\n\n\n\n<p>\u800c\u8fdb\u7a0b\u540c\u6b65\uff0c\u5219\u662f\u4e3a\u4e86\u8ba9\u4e0a\u8ff0\u7684A\uff0cB\u8fdb\u7a0b\u6309\u67d0\u4e9b\u89c4\u5219\u6216\u65f6\u8bb8\u5171\u4eab\u7cfb\u7edf\u8d44\u6e90\uff0c\u5e76\u80fd\u76f8\u4e92\u5408\u4f5c\uff0c\u4ece\u800c\u4f7f\u7a0b\u5e8f\u7684\u6267\u884c\u5177\u6709\u53ef\u518d\u73b0\u6027\u3002<\/p>\n\n\n\n<p>\u4e0a\u8ff0\u63d0\u5230\u7684\u6253\u5370\u673a\u5219\u662f\u4e00\u79cd<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4e34\u754c\u8d44\u6e90<\/mark>(Critical Resource)\uff0c\u8868\u660e<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5404\u8fdb\u7a0b\u5e94\u8be5\u901a\u8fc7\u4e92\u65a5\u7684\u65b9\u5f0f\u8bbf\u95ee\u6b64\u79cd\u8d44\u6e90<\/mark>\u3002<\/p>\n\n\n\n<p>\u800c\u8fdb\u7a0b\u4e2d<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u7684\u4ee3\u7801\u6bb5\u5219\u88ab\u79f0\u4e3a\u4e34\u754c\u533a<\/mark>(Critical Secion)\u3002\u5728\u8fdb\u7a0b\u6267\u884c\u5230\u4e34\u754c\u533a\u4e4b\u524d\uff0c\u8fd8\u5e94\u6709<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u68c0\u67e5\u8fdb\u7a0b\u80fd\u5426\u8bbf\u95ee\u4e34\u754c\u8d44\u6e90\u7684\u8fdb\u5165\u533a<\/mark>(entry section)\uff0c\u4ee5\u53ca<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5c06\u4e34\u754c\u533a\u8bbf\u95ee\u7684\u4e34\u754c\u8d44\u6e90\u6062\u590d\u4e3a\u672a\u88ab\u8bbf\u95ee\u6807\u5fd7\u7684\u9000\u51fa\u533a(<\/mark>exit section)\u3002<\/p>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u540c\u6b65\u673a\u5236\u7684\u89c4\u5219<\/p>\n<cite><em>\u56db\u6761\u51c6\u5219<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u6240\u6709\u540c\u6b65\u673a\u5236\u90fd\u5e94\u8be5\u9075\u5faa\u4e0b\u8ff0\u56db\u6761\u51c6\u5219\uff1a<\/p>\n\n\n\n<ul>\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u7a7a\u95f2\u8ba9\u8fdb<\/mark>\n<ul>\n<li>\u65e0\u8fdb\u7a0b\u5904\u4e8e\u4e34\u754c\u533a\u65f6\uff0c\u8868\u793a\u4e34\u754c\u8d44\u6e90\u7a7a\u95f2\u3002\u6b64\u65f6\u5e94\u5141\u8bb8\u8bf7\u6c42\u8fdb\u5165\u4e34\u754c\u533a\u7684\u8fdb\u7a0b\u8fdb\u5165\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5fd9\u5219\u7b49\u5f85<\/mark>\n<ul>\n<li>\u5f53\u5df2\u6709\u8fdb\u7a0b\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u8868\u793a\u4e34\u754c\u8d44\u6e90\u6b63\u5728\u88ab\u8bbf\u95ee\u3002\u6b64\u65f6\u5176\u4ed6\u8bd5\u56fe\u8fdb\u5165\u4e34\u754c\u533a\u7684\u8fdb\u7a0b\u5e94\u8be5\u7b49\u5f85\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u6709\u9650\u7b49\u5f85<\/mark>\n<ul>\n<li>\u8fdb\u7a0b\u5e94\u5728\u6709\u9650\u65f6\u95f4\u5185\u80fd\u8fdb\u5165\u81ea\u5df1\u7684\u4e34\u754c\u533a\uff0c\u4ee5\u514d\u8fdb\u5165\u201c\u6b7b\u7b49\u201d\u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u8ba9\u6743\u7b49\u5f85<\/mark>\n<ul>\n<li> \u88ab\u5206\u914d\u4e86\u5904\u7406\u673a\u7684\u8fdb\u7a0b\u4e0d\u80fd\u8fdb\u5165\u4e34\u754c\u533a\u65f6\uff0c\u5e94\u8be5\u91ca\u653e\u5904\u7406\u673a\uff0c\u4ee5\u514d\u8fdb\u7a0b\u9677\u5165\u201c\u5fd9\u7b49\u201d\u72b6\u6001\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u4ec0\u4e48\u662f\u4fe1\u53f7\u91cf\uff1f<\/p>\n<cite><em>\u56db\u79cd\u4fe1\u53f7\u91cf\u673a\u5236<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u4fe1\u53f7\u91cf(Semaphores)\u673a\u5236\u662f\u4e00\u79cd\u975e\u5e38\u6709\u6548\u7684\u8fdb\u7a0b\u540c\u6b65\u5de5\u5177\u3002<\/p>\n\n\n\n<ul>\n<li><strong>\u6574\u578b\u4fe1\u53f7\u91cf<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u987e\u540d\u601d\u4e49\uff0c\u8fd9\u91cc\u7684\u4fe1\u53f7\u91cfS\u53ef\u4ee5\u7b80\u5355\u7684\u7406\u89e3\u4e3aint\u7c7b\u578b\u7684\u6570\u3002\u9664\u4e86\u4fe1\u53f7\u91cf\u7684\u521d\u59cb\u5316\uff0c\u6211\u4eec<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u4ec5\u80fd\u901a\u8fc7\u4e24\u4e2a\u539f\u5b50\u64cd\u4f5c&#8211;wait\u548csignal<\/mark>\u6765\u5b9e\u73b0\u5bf9\u4fe1\u53f7\u91cf\u7684\u8bbf\u95ee\u3002\u539f\u5b50\u64cd\u4f5c\u610f\u5473\u7740\u67d0\u4e2a\u8fdb\u7a0b\u5728\u4fee\u6539\u4fe1\u53f7\u91cf\u65f6\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5176\u4ed6\u8fdb\u7a0b\u90fd\u65e0\u6cd5\u5bf9\u540c\u4e00\u4fe1\u53f7\u91cf\u8fdb\u884c\u4fee\u6539<\/mark>\uff0c\u53ea\u80fd\u7b49\u5f85\u4e0a\u4e00\u8fdb\u7a0b\u4fee\u6539\u5b8c\u4fe1\u53f7\u91cf\u3002<\/p>\n\n\n\n<p>\u4e24\u4e2a\u64cd\u4f5c\u7684\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wait (S)\n{\n    while(S&lt;=0); \/\/\u76f4\u5230\u8fdb\u7a0b\u4f7f\u7528\u5b8c\u8d44\u6e90\n    S--;\n}\nsignal(S)\n{\n    S++;\n}<\/code><\/pre>\n\n\n\n<p>\u5177\u4f53\u5e94\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul>\n<li>\u5f53\u67d0\u4e2a\u8d44\u6e90\u7684\u4fe1\u53f7\u91cf\u4e3a1\u65f6\uff0c\u8868\u793a\u4e24\u4e2a\u8fdb\u7a0b\u90fd\u6ca1\u6709\u8fdb\u5165\u9700\u8981\u4e92\u65a5\u6267\u884c\u7684\u4e34\u754c\u533a\uff1b<\/li>\n\n\n\n<li>\u5f53A\u8fdb\u7a0b\u51c6\u5907\u8fdb\u5165\u4e34\u754c\u533a\u65f6\uff0c\u8c03\u7528wait(S)\u4f7f\u4fe1\u53f7\u91cf\u4e3a0\uff0c\u8868\u793aA\u8fdb\u7a0b\u5373\u5c06\u4f7f\u7528\u8be5\u8d44\u6e90\u3002<\/li>\n\n\n\n<li>\u518d\u8c03\u7528wait(S)\u4f7f\u4fe1\u53f7\u91cf\u4e3a-1\uff0c\u8868\u793aA\u8fdb\u7a0b\u6b63\u5728\u4e34\u754c\u533a\u4e2d\u8fd0\u884c\u3002<\/li>\n\n\n\n<li>\u8c03\u7528signal(S)\u4f7f\u4fe1\u53f7\u91cf\u4e3a0\uff0c\u8868\u793aA\u8fdb\u7a0b\u9000\u51fa\u4e34\u754c\u533a\uff0c\u5176\u4ed6\u8fdb\u7a0b\u5141\u8bb8\u4f7f\u7528\u8be5\u8d44\u6e90\u3002<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote has-vivid-cyan-blue-color has-text-color\">\n<p>\u5199\u8005\u6ce8<\/p>\n<cite>\u539f\u5b50\u64cd\u4f5c\u7531\u4e00\u6bb5\u4ee3\u7801\u6784\u6210\uff0c\u8be5\u64cd\u4f5c\u5185\u7684\u4ee3\u7801\u8981\u4e48\u5168\u6267\u884c\uff0c\u8981\u4e48\u5168\u90fd\u4e0d\u6267\u884c\u3002\u800cwait\u548csignal\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e24\u4e2a\u51fd\u6570\uff0c\u5373wait(S)\u76f8\u5f53\u4e8e\u5bf9\u4fe1\u53f7\u91cf\u6267\u884c\u201cS-1\u201d\u64cd\u4f5c\uff0csignal(S)\u76f8\u5f53\u4e8e\u5bf9\u4fe1\u53f7\u91cf\u6267\u884c&#8221;S+1&#8243;\u64cd\u4f5c\u3002<\/cite><\/blockquote>\n\n\n\n<ul>\n<li><strong>\u8bb0\u5f55\u578b\u4fe1\u53f7\u91cf<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u201c\u6574\u578b\u4fe1\u53f7\u91cf\u201d\u673a\u5236\u65e0\u6cd5\u5b9e\u73b0\u201c\u8ba9\u6743\u7b49\u5f85\u201d\uff0c\u5982\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5982\u679c\u6839\u636e\u7ed9\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u4f1a\u51fa\u73b0\u5982\u4e0b\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul>\n<li>A\uff0cB\u8fdb\u7a0b\u90fd\u9700\u8981\u8bbf\u95ee\u540c\u4e00\u8d44\u6e90<\/li>\n\n\n\n<li>A\u8fdb\u7a0b\u5728\u4e34\u754c\u533a\u4e2d\u6267\u884c<\/li>\n\n\n\n<li>\u6b64\u65f6B\u8fdb\u7a0b<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u5728\u5904\u7406\u673a\u4e2d\u4e0d\u65ad\u5faa\u73af\u68c0\u67e5\u4fe1\u53f7\u91cf\u7684\u91cf\uff0c\u76f4\u5230\u4fe1\u53f7\u91cf\u53ef\u7528<\/mark>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5f88\u660e\u663e\u8fd9\u4e0d\u591f\u9ad8\u6548\u3002\u90a3\u4e48\u4e0d\u5982\u5c06\u4fe1\u53f7\u91cf\u4ece\u6574\u578b\u53d8\u4e3a\u7ed3\u6784\u4f53\uff0c\u7528\u4e00\u4e2a\u94fe\u8868\u5b58\u50a8\u9700\u8981\u7b49\u5f85\u7684\u8fdb\u7a0b\uff08\u7684PCB\uff09\uff0c\u6ca1\u6cd5\u8fdb\u5165\u4e34\u754c\u533a\u65f6\u5c31\u5c06\u8be5\u8fdb\u7a0b\u6dfb\u52a0\u5230\u94fe\u8868\u3002\u518d\u7528\u4e00\u4e2a\u6574\u578b\u503c\u8868\u793a\u53ef\u5206\u914d\u7684\u8d44\u6e90\u6570\u3002<\/p>\n\n\n\n<p>\u8be5\u7ed3\u6784\u4f53\u7684\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>typedef struct\n{\n    int value;\n    struct process_control_block *list; \n}semaphore;<\/code><\/pre>\n\n\n\n<p>\u5bf9\u5e94\u7684\uff0cwait\u548csignal\u64cd\u4f5c\u53ef\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wait(semaphore* S)\n{\n    S-&gt;value--;\n    if(S-&gt;value&lt;0) block(S-&gt;list);\n}\nsignal(semaphore* S)\n{\n    S-&gt;value++;\n    if(S-&gt;value&lt;=0) wakeup(S-&gt;list);\n}<\/code><\/pre>\n\n\n\n<p>\u5177\u4f53\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul>\n<li>\u7ed9\u5b9a\u6253\u5370\u673a\u8d44\u6e90value=1\uff0c\u8fdb\u7a0bA\uff0cB\u3002<\/li>\n\n\n\n<li>\u8fdb\u7a0bA\u8c03\u7528wait\uff0c\u610f\u5473\u7740\u5c06\u6253\u5370\u673a\u8d44\u6e90\u5206\u914d\u7ed9\u8fdb\u7a0bA\u3002\u6b64\u65f6value=0<\/li>\n\n\n\n<li>\u8fdb\u7a0bB\u518d\u8c03\u7528wait\uff0c\u53d1\u73b0value=-1\uff0c\u5c06\u8fdb\u7a0bB\u6302\u8d77\u5e76\u63d2\u5165\u5230\u7b49\u5f85\u94fe\u8868list\u4e2d\u3002<\/li>\n\n\n\n<li>\u8fdb\u7a0bA\u4f7f\u7528\u5b8c\u6253\u5370\u673a\uff0c\u8c03\u7528signal\uff0c\u91ca\u653e\u6253\u5370\u673a\u8d44\u6e90\u3002\u6b64\u65f6value=0\u3002\u6b64\u65f6\u8c03\u7528wakeup\uff0c\u5c06\u8fdb\u7a0bB\u5524\u9192\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5728\u8fd9\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u60f3\u8981\u8fdb\u5165\u4e34\u754c\u533a\uff0c\u4f46\u6ca1\u6709\u53ef\u5206\u914d\u8d44\u6e90\u7684\u8fdb\u7a0b\u4f1a\u88ab\u6302\u8d77\u5230\u7b49\u5f85\u961f\u5217\u4e2d\uff0c\u76f4\u5230\u5176\u4ed6\u8fdb\u7a0b\u91ca\u653e\u53ef\u7528\u8d44\u6e90\u3002<\/p>\n\n\n\n<ul>\n<li><strong>AND\u578b\u4fe1\u53f7\u91cf<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u4e0a\u9762\u4e24\u4e2a\u90fd\u662f\u201c\u591a\u4e2a\u8fdb\u7a0b\u5bf9\u540c\u4e00\u4e34\u754c\u8d44\u6e90\u201c\u7684\u4e92\u65a5\u8bbf\u95ee\u3002\u800c\u6709\u65f6\u4f1a\u51fa\u73b0\u201d\u67d0\u4e2a\u8fdb\u7a0b\u5bf9\u591a\u4e2a\u4e34\u754c\u8d44\u6e90\u201c\u7684\u4e92\u65a5\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u5957\u7528\u4e0a\u9762\u4e24\u79cd\u4fe1\u53f7\u91cf\u7684\u64cd\u4f5c\u3002\u6bd4\u5982\u8fdb\u7a0bA\uff0cB\u90fd\u9700\u8981\u8bbf\u95ee\u6570\u636e\u6bb5C\uff0cD\u3002\u90a3\u4e48\u4e3a\u8fd9\u4e24\u4e2a\u6570\u636e\u6bb5\u5206\u522b\u8bbe\u7f6e\u4fe1\u53f7\u91cfCmutex\u548cDmutex\uff0c\u5e76\u4ee4\u5b83\u4eec\u7684\u521d\u503c\u4e3a1\u3002\u4e24\u4e2a\u8fdb\u7a0b\u4e2d\u5305\u542b\u5bf9\u4e24\u4e2a\u4fe1\u53f7\u91cf\u7684\u64cd\u4f5c\uff0c\u5373<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>process A:\nwait(Cmutex);\nwait(Dmutex);\n\nprocess B:\nwait(Cmutex);\nwait(Dmutex);<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u6709\u4e2a\u95ee\u9898\uff0c\u5982\u679c\u8fdb\u7a0bB\u5728\u8fdb\u7a0bA\u4e4b\u524d\u5c31\u9501\u5b9a\u4e86\u6570\u636e\u6bb5C\uff0c\u800cA\u8fdb\u7a0b\u9501\u5b9a\u4e86\u6570\u636e\u6bb5E\uff0c\u5373\u4e24\u4e2a\u8fdb\u7a0b\u4ea4\u66ff\u6267\u884c\u4e86wait\u64cd\u4f5c\u3002<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u90a3\u4e48\u8fd9\u4e24\u4e2a\u8fdb\u7a0b\u6b64\u65f6\u90fd\u5728\u7b49\u5f85\u53e6\u4e00\u4e2a\u8fdb\u7a0b\u91ca\u653e\u8d44\u6e90\uff0c\u5373\u8fdb\u5165\u4e86\u6b7b\u9501\u72b6\u6001<\/mark>\u3002<\/p>\n\n\n\n<p>\u7531\u6b64\uff0c\u4e0a\u8ff0\u4e24\u79cd\u65b9\u6cd5\u90fd\u884c\u4e0d\u901a\uff0c\u4e8e\u662f\u6211\u4eec\u5f15\u5165\u7684AND\u578b\u4fe1\u53f7\u91cf\u3002<\/p>\n\n\n\n<p>\u7c7b\u4f3c\u4e8e\u539f\u5b50\u64cd\u4f5c\uff0c<mark style=\"background-color:rgba(0, 0, 0, 0);color:#fcb900\" class=\"has-inline-color\">\u6211\u4eec\u8981\u4e48\u5c06\u6240\u6709\u6240\u9700\u8d44\u6e90\u90fd\u5206\u914d\u7ed9\u67d0\u4e2a\u8fdb\u7a0b\uff0c\u8981\u4e48\u5c31\u5168\u90e8\u90fd\u4e0d\u5206\u914d<\/mark>\u3002\u8be5\u64cd\u4f5c\u7684\u4ee3\u7801\u63cf\u8ff0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Swait(S1,S2,...,Sn)\n{    while(TRUE)\n    {\n        if(Si&gt;=1...&amp;&amp;Sn&gt;=1)\n        {\n            for(i=1;i&lt;=n;i++)\n                Si--;\n            break;\n        }else\n        {\n           <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\"> \/*\u5c06\u65e0\u6cd5\u83b7\u53d6\u8d44\u6e90\u7684\u8fdb\u7a0b\u6302\u8d77\u5230\u7b49\u5f85\u961f\u5217\u4e2d\uff0c\n            \u5e76\u5c06\u8be5\u8fdb\u7a0b\u8ba1\u6570\u8bbe\u7f6e\u4e3aSwait\u64cd\u4f5c\u7684\u9996\u5730\u5740*\/<\/mark>\n        }\n    }\n}\nSsignal(S1,S2,...,Sn)\n{\n    while(TRUE)\n    {\n        for(i=1;i&lt;=n;i++)\n        {\n            Si++;\n           <mark style=\"background-color:rgba(0, 0, 0, 0);color:#00ff2f\" class=\"has-inline-color\"> \/\/\u5c06\u6240\u6709\u4e8eSi\u6709\u5173\u7684\u8fdb\u7a0b\u4ece\u7b49\u5f85\u961f\u5217\u79fb\u5165\u5c31\u7eea\u961f\u5217<\/mark>\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<ul>\n<li><strong>\u4fe1\u53f7\u91cf\u96c6<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u5728\u4e0a\u9762\u63d0\u5230\u7684\u8bb0\u5f55\u578b\u4fe1\u53f7\u91cf\u673a\u5236\u4e2d\uff0cwait\u548csignal\u90fd\u662f\u5bf9\u4fe1\u53f7\u91cf\u5b9e\u65bd\u201c\u52a0\u4e00\u51cf\u4e00\u201d\u7684\u64cd\u4f5c\uff0c\u5982\u679c\u67d0\u4e2a\u8fdb\u7a0b\u8981\u7533\u8bf7100\u4e2a\u5355\u4f4d\u7684\u4e34\u754c\u8d44\u6e90\uff0c\u90a3\u4e48\u5c31\u8981\u6267\u884c100\u6b64wait\u64cd\u4f5c\u3002\u8fd9\u663e\u7136\u662f\u4f4e\u6548\u7684\u3002\u800c\u4e14\u7531\u4e8e\u8fdb\u7a0b\u95f4\u7684\u5f02\u6b65\u64cd\u4f5c\uff0c\u8fd8\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u9501\u7684\u53d1\u751f\uff0c<\/p>\n\n\n\n<p>\u540c\u65f6\uff0c\u4e3a\u786e\u4fdd\u7cfb\u7edf\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u8d44\u6e90\u7684\u53ef\u5206\u914d\u6570\u91cf\u901a\u5e38\u6709\u4e2a\u4e0a\u9650\u3002\u5373\u8d85\u8fc7\u8be5\u4e0a\u9650\u65f6\u7cfb\u7edf\u5e94\u4e0d\u518d\u4e3a\u8fdb\u7a0b\u5206\u914d\u8be5\u7c7b\u8d44\u6e90\u3002<\/p>\n\n\n\n<p>\u57fa\u4e8e\u8fd9\u4e24\u70b9\uff0c\u6211\u4eec\u5bf9AND\u578b\u4fe1\u53f7\u91cf\u673a\u5236\u52a0\u4ee5\u6269\u5145\u3002<\/p>\n\n\n\n<p>\u8bbe\u7f6e\u5404\u79cd\u8d44\u6e90\u7684\u5206\u914d\u4e0b\u9650\u503c\u4e3ati\uff0c\u5404\u8fdb\u7a0b\u5bf9\u8be5\u8d44\u6e90\u7684\u9700\u6c42\u503c\u4e3adi\u3002\u5f53\u4fe1\u53f7\u91cfS\u2265ti\u65f6\uff0c\u4e0d\u518d\u5206\u914d\u8be5\u8d44\u6e90\uff1b\u800c\u4fe1\u53f7\u91cf\u7684\u503c\u4e3aS=S-di\u3002<\/p>\n\n\n\n<p>\u4fe1\u53f7\u91cf\u96c6\u8fd8\u6709\u4ee5\u4e0b\u4e09\u79cd\u7279\u6b8a\u60c5\u51b5\uff1a<\/p>\n\n\n\n<ul>\n<li>Swait(S,d,d)\n<ul>\n<li>\u8868\u793a\u4fe1\u53f7\u91cf\u96c6\u4e2d\u53ea\u6709\u4e00\u4e2a\u4fe1\u53f7\u91cf\uff0c\u5141\u8bb8\u8fdb\u7a0b\u7533\u8bf7d\u4e2a\u8d44\u6e90\uff0c\u5f53\u8d44\u6e90\u6570\u5c0f\u4e8ed\u65f6\u4e0d\u4e88\u5206\u914d\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Swait(S,1,1)\n<ul>\n<li>\u4e00\u4e2a\u8d44\u6e90\u5bf9\u5e94\u4e00\u4e2a\u8fdb\u7a0b\uff0c\u8715\u5316\u6210\u4e86\u8bb0\u5f55\u578b\u4fe1\u53f7\u91cf(S&gt;1)\u6216\u4e92\u65a5\u4fe1\u53f7\u91cf(S=1)\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Swait(S,1,0)\n<ul>\n<li>\u5f53S\u22651\u65f6\uff0c\u76f8\u5f53\u4e8e\u5141\u8bb8\u591a\u4e2a\u8fdb\u7a0b\u7533\u8bf7\u4e00\u4e2a\u8be5\u8d44\u6e90\uff1b\u5f53S=0\u65f6\uff0c\u6240\u6709\u8fdb\u7a0b\u90fd\u65e0\u6cd5\u8fdb\u5165\u4e34\u754c\u533a\u3002\u76f8\u5f53\u4e8e\u4e00\u4e2a\u53ef\u63a7\u7684\u5f00\u5173\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8fdb\u7a0b\u540c\u6b65\u4e0e\u4fe1\u53f7\u91cf\u673a\u5236<\/p>\n","protected":false},"author":1,"featured_media":2985,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[26,47],"tags":[31],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1125"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=1125"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/1125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2985"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=1125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=1125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=1125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}