{"id":877,"date":"2019-10-23T12:40:05","date_gmt":"2019-10-23T04:40:05","guid":{"rendered":"http:\/\/149.129.74.185\/?p=877"},"modified":"2019-10-23T14:23:22","modified_gmt":"2019-10-23T06:23:22","slug":"%e6%93%8d%e4%bd%9c%e7%b3%bb%e7%bb%9fweek-6-note","status":"publish","type":"post","link":"http:\/\/www.magicmipamipa.com\/?p=877","title":{"rendered":"[\u64cd\u4f5c\u7cfb\u7edf]Week 6 Note"},"content":{"rendered":"<p>6.5 Semaphores \u4fe1\u53f7\u91cf<\/p>\n<p>Dijkstra\u63d0\u51fa\u2014\u2014\u4fe1\u53f7\u91cf\u673a\u5236<\/p>\n<ul>\n<li>\u4fe1\u53f7\u91cf\uff1a\n<ol>\n<li>\u6574\u5f62\u4fe1\u53f7\u91cf<\/li>\n<li>\u8bb0\u5f55\u578b\u4fe1\u53f7\u91cf<\/li>\n<li>AND\u578b\u4fe1\u53f7\u91cf\u3001\u4fe1\u53f7\u91cf\u96c6<\/li>\n<li>\u4e8c\u503c\u4fe1\u53f7\u91cf<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>1\u3001\u7528\u6cd5<\/p>\n<p>S\u4fe1\u53f7\u91cf\u53ea\u80fd\u8fdb\u884c\u4e24\u79cd<strong>\u4e0d\u53ef\u5206\u5272<\/strong>\u7684<strong>\u539f\u5b50\u64cd\u4f5c<\/strong>\uff1a<strong><span style=\"color: #ff0000;\">wait(S)<\/span><\/strong>\u548c<strong><span style=\"color: #ff0000;\">signal(S)<\/span><\/strong>\uff08\u6700\u5f00\u59cb\u53eb\u4f5cP()\u548cV()\uff09<\/p>\n<p>Counting semaphore \u8ba1\u6570\u4fe1\u53f7\u91cf \uff08\u6574\u6570\u4fe1\u53f7\u91cf\uff09<\/p>\n<p>Binary semaphore \u4e8c\u503c\u4fe1\u53f7\u91cf \uff080\u62161\uff09 &#8211; \u5373\u4e92\u65a5\u9501\uff08mutex locks\uff09<\/p>\n<p>&nbsp;<\/p>\n<p>2\u3001\u5b9e\u73b0<\/p>\n<p>Define a semaphore as a record \uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">typedef struct {\r\nint value;\r\nstruct process *list;\r\n} semaphore;<\/pre>\n<p>Two operations:<\/p>\n<p>block() \u2013 \u5c06\u8c03\u7528\u64cd\u4f5c\u7684\u8fc7\u7a0b\u653e\u5728\u9002\u5f53\u7684\u7b49\u5f85\u961f\u5217\u4e0a\u3002<\/p>\n<ul>\n<li>running \u2192 waiting<\/li>\n<\/ul>\n<p>wakeup() \u2013\u5220\u9664\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u8fdb\u7a0b\u4e4b\u4e00\u5e76\u5c06\u5176\u653e\u5165\u5c31\u7eea\u961f\u5217\u3002<\/p>\n<ul>\n<li>waiting\u2192ready<\/li>\n<\/ul>\n<table>\n<tbody>\n<tr>\n<td>Implementation of wait<\/td>\n<td>Implementation of signal:<\/td>\n<\/tr>\n<tr>\n<td>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">wait(semaphore *S) {\r\nS-&gt;value--;\r\nif (S-&gt;value &lt; 0) {\r\nadd this process to S-&gt;list;\r\nblock();\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/td>\n<td>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">signal(semaphore *S) {\r\nS-&gt;value++;\r\nif (S-&gt;value &lt;= 0) {\r\nremove a process P from S-&gt;list;\r\nwakeup(P);\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>3\u3001wait\u548csignal\u7684\u64cd\u4f5c\u8ba8\u8bba<\/p>\n<ul style=\"list-style-type: square;\">\n<li>\u901a\u5e38\u7528\u4fe1\u53f7\u91cf\u8868\u793a\u8d44\u6e90\u6216\u4e34\u754c\u533a<\/li>\n<li>\u4fe1\u53f7\u91cf\u7684\u7269\u7406\u542b\u4e49\n<ul style=\"list-style-type: circle;\">\n<li>S.value &gt;0 \u8868\u793a\u6709S.value\u4e2a\u8d44\u6e90\u53ef\u7528\uff1b<\/li>\n<li>S.value=0 \u8868\u793a\u65e0\u8d44\u6e90\u53ef\u7528\u6216\u8868\u793a\u4e0d\u5141\u8bb8\u8fdb\u7a0b\u518d\u8fdb\u5165\u4e34\u754c\u533a\uff1b<\/li>\n<li>S.value&lt;0 \u5219|S.value|\u8868\u793a\u5728\u7b49\u5f85\u961f\u5217\u4e2d\u8fdb\u7a0b\u7684\u4e2a\u6570\u6216\u8868\u793a\u7b49\u5f85\u8fdb\u5165\u4e34\u754c\u533a\u7684\u8fdb\u7a0b\u4e2a\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<li>wait(S)\u2261P(S) \u2261 down(S) \uff1a \u8868\u793a\u7533\u8bf7\u4e00\u4e2a\u8d44\u6e90<\/li>\n<li>signal(S)\u2261V(S) \u2261 up(S) \uff1a \u8868\u793a\u91ca\u653e\u4e00\u4e2a\u8d44\u6e90<\/li>\n<li>wait\u3001signal\u64cd\u4f5c\u5fc5\u987b\u6210\u5bf9\u51fa\u73b0\uff0c\u6709\u4e00\u4e2await\u64cd\u4f5c\u5c31\u4e00\u5b9a\u6709\u4e00\u4e2asignal\u64cd\u4f5c\u3002\u4e00\u822c\u60c5\u51b5\u4e0b\uff1a\u5f53\u4e3a\u4e92\u65a5\u64cd\u4f5c\u65f6\uff0c\u5b83\u4eec\u540c\u5904\u4e8e\u540c\u4e00\u8fdb\u7a0b\uff1b\u5f53\u4e3a\u540c\u6b65\u64cd\u4f5c\u65f6\uff0c\u5219\u4e0d\u5728\u540c\u4e00\u8fdb\u7a0b\u4e2d\u51fa\u73b0\u3002<\/li>\n<li>\u5982\u679c\u4e24\u4e2await\u64cd\u4f5c\u76f8\u90bb\uff0c\u90a3\u4e48\u5b83\u4eec\u7684\u987a\u5e8f\u81f3\u5173\u91cd\u8981\uff0c\u800c\u4e24\u4e2a\u76f8\u90bb\u7684signal\u64cd\u4f5c\u7684\u987a\u5e8f\u65e0\u5173\u7d27\u8981\u3002\u4e00\u4e2a\u540c\u6b65wait\u64cd\u4f5c\u4e0e\u4e00\u4e2a\u4e92\u65a5wait\u64cd\u4f5c\u5728\u4e00\u8d77\u65f6\uff0c\u540c\u6b65wait \u64cd\u4f5c\u5728\u4e92\u65a5wait\u64cd\u4f5c\u524d\u3002<\/li>\n<li>wait\u3001signal\u64cd\u4f5c\u7684\u4f18\u7f3a\u70b9\n<ul style=\"list-style-type: circle;\">\n<li>\u4f18\u70b9\uff1a\u7b80\u5355\uff0c\u800c\u4e14\u8868\u8fbe\u80fd\u529b\u5f3a\u3002<\/li>\n<li>\u7f3a\u70b9\uff1a\u4e0d\u591f\u5b89\u5168\uff1bwait\u3001signal\u64cd\u4f5c\u4f7f\u7528\u4e0d\u5f53\u4f1a\u51fa\u73b0\u6b7b\u9501\uff1b\u5b9e\u73b0\u590d\u6742\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>4\u3001\u6b7b\u9501\u548c\u9965\u997f<\/p>\n<p>\u6b7b\u9501\u2013\u4e24\u4e2a\u6216\u591a\u4e2a\u8fdb\u7a0b\u65e0\u9650\u671f\u5730\u7b49\u5f85\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u8be5\u4e8b\u4ef6\u4ec5\u7531\u4e00\u4e2a\u7b49\u5f85\u7684\u8fdb\u7a0b\u5f15\u8d77\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>6.6 \u7ecf\u5178\u540c\u6b65\u95ee\u9898<\/p>\n<p>1\u3001\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898<\/p>\n<ul>\n<li>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u662f\u6700\u8457\u540d\u7684\u540c\u6b65\u95ee\u9898\uff0c\u5b83\u63cf\u8ff0\u4e00\u7ec4\u751f\u4ea7\u8005\uff08P1 \u2026\u2026Pm\uff09\u5411 \u4e00\u7ec4\u6d88\u8d39\u8005\uff08C1\u2026\u2026Cq\uff09\u63d0\u4f9b\u6d88\u606f\u3002\u5b83\u4eec\u5171\u4eab\u4e00\u4e2a\u6709\u9650\u7f13\u51b2\u6c60\uff08bounded buffer pool\uff09,\u751f\u4ea7\u8005\u5411\u5176\u4e2d\u6295\u653e\u6d88\u606f\uff0c\u6d88\u8d39\u8005\u4ece\u4e2d\u53d6\u5f97\u6d88\u606f\u3002<\/li>\n<li>\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u95ee\u9898\u662f\u8bb8\u591a\u76f8\u4e92\u5408\u4f5c\u8fdb\u7a0b\u7684\u4e00\u79cd\u62bd\u8c61\u3002<\/li>\n<\/ul>\n<table style=\"width: 900px;\">\n<tbody>\n<tr>\n<td colspan=\"2\" style=\"width: 900px;\">\n<ul style=\"list-style-type: square;\">\n<li>N buffers, each can hold one item<\/li>\n<li>Semaphore <span style=\"color: #ff0000;\">mutex<\/span> initialized to the value 1<\/li>\n<li>Semaphore <span style=\"color: #ff0000;\">full<\/span> initialized to the value 0 \/\/\u6d88\u8d39\u8005<\/li>\n<li>Semaphore <span style=\"color: #ff0000;\">empty<\/span> initialized to the value N. \/\/\u751f\u4ea7\u8005<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" style=\"width: 900px;\">Shared data semaphore full, empty, mutex;<\/p>\n<p>Initially: full = 0, empty = n, mutex = 1<\/td>\n<\/tr>\n<tr>\n<td style=\"width: 438px;\">\u751f\u4ea7\u8005\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">do {\r\n\u2026\r\nproduce an item in nextp\r\n\u2026\r\nwait(empty);\r\nwait(mutex);\r\n\u2026\r\nadd nextp to buffer\r\n\u2026\r\nsignal(mutex);\r\nsignal(full);\r\n} while (1);<\/pre>\n<\/td>\n<td style=\"width: 462px;\">\u6d88\u8d39\u8005\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\">do {\r\nwait(full);\r\nwait(mutex);\r\n\u2026\r\nremove an item from buffer to nextc\r\n\u2026\r\nsignal(mutex);\r\nsignal(empty);\r\n\u2026\r\nconsume the item in nextc\r\n\u2026\r\n} while (1);<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>6.5 Semaphores \u4fe1\u53f7\u91cf Dijkstra\u63d0\u51fa\u2014\u2014\u4fe1\u53f7\u91cf\u673a\u5236 \u4fe1\u53f7\u91cf\uff1a \u6574\u5f62\u4fe1\u53f7\u91cf \u8bb0\u5f55 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,20],"tags":[],"_links":{"self":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/877"}],"collection":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=877"}],"version-history":[{"count":1,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/877\/revisions"}],"predecessor-version":[{"id":879,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=\/wp\/v2\/posts\/877\/revisions\/879"}],"wp:attachment":[{"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=877"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicmipamipa.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}