{"id":1554,"date":"2012-03-21T17:24:56","date_gmt":"2012-03-21T08:24:56","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1554"},"modified":"2012-03-21T17:30:14","modified_gmt":"2012-03-21T08:30:14","slug":"xv6-47-chapter-4-scheduling-sleep-and-wakeup","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1554","title":{"rendered":"[xv6 #47] Chapter 4 &#8211; Scheduling &#8211; Sleep and wakeup"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e55\u301c57\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30ed\u30c3\u30af\u306f\u3001CPU\u540c\u58eb\u3084\u30d7\u30ed\u30bb\u30b9\u540c\u58eb\u304c\u304a\u4e92\u3044\u306b\u5e72\u6e09\u3057\u5408\u3046\u306e\u3092\u907f\u3051\u308b\u306e\u306b\u5f79\u7acb\u3061\u3001\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u306f\u3001\u8907\u6570\u306e\u30d7\u30ed\u30bb\u30b9\u304cCPU\u3092\u5171\u6709\u3059\u308b\u306e\u306b\u5f79\u7acb\u3064\u3002<br \/>\n\u3057\u304b\u3057\u3053\u308c\u307e\u3067\u3001\u6211\u3005\u306f\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3092\u697d\u306b\u3059\u308b\u3088\u3046\u306a\u6982\u5ff5\u306f\u6301\u3061\u5408\u308f\u305b\u3066\u3044\u306a\u304b\u3063\u305f\u3002<br \/>\n&#8220;\u30b9\u30ea\u30fc\u30d7&#8221;\uff08sleep\uff09\u3068&#8221;\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7&#8221;\uff08wakeup\uff09\u306f\u305d\u306e\u9593\u9699\u3092\u57cb\u3081\u3001\u3042\u308b\u30d7\u30ed\u30bb\u30b9\u304c\u3042\u308b\u30a4\u30d9\u30f3\u30c8\u3092\u5f85\u3064\u305f\u3081\u306b\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u3001\u4e00\u5ea6\u30a4\u30d9\u30f3\u30c8\u304c\u8d77\u304d\u305f\u3089\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7\u3055\u305b\u308b\u4e8b\u3092\u53ef\u80fd\u306b\u3059\u308b\u3002<br \/>\n\u30b9\u30ea\u30fc\u30d7\u3068\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7\u306f\u3001\u3057\u3070\u3057\u3070\u9806\u5e8f\u5354\u8abf\uff08\u30b7\u30fc\u30b1\u30f3\u30b9\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30b7\u30e7\u30f3, sequence coordination, \u30aa\u30ec\u30aa\u30ec\u8a33\u306a\u306e\u3067\u6ce8\u610f\uff09\u3001\u6761\u4ef6\u4ed8\u304d\u540c\u671f\u5316\uff08\u30b3\u30f3\u30c7\u30a3\u30b7\u30e7\u30ca\u30eb\u30b7\u30f3\u30af\u30ed\u30ca\u30a4\u30bc\u30fc\u30b7\u30e7\u30f3, conditional synchronization, \u3053\u3061\u3089\u3082\u30aa\u30ec\u30aa\u30ec\u8a33\u306a\u306e\u3067\u6ce8\u610f\uff09\u306e\u6a5f\u69cb\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\nOS\u5b66\u7684\u306b\u4ed6\u306e\u4f3c\u305f\u3088\u3046\u306a\u6a5f\u69cb\u306f\u305f\u304f\u3055\u3093\u3042\u308b\u3002<\/p>\n<p>\u6211\u3005\u304c\u4f55\u3092\u8a00\u3044\u305f\u3044\u304b\u30cf\u30c3\u30ad\u30ea\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u751f\u7523\u8005\/\u6d88\u8cbb\u8005\uff08producer\/consumer\uff09\u30ad\u30e5\u30fc\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u3088\u3046\u3002<br \/>\n\u3053\u306e\u30ad\u30e5\u30fc\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u3068\u30c7\u30d0\u30a4\u30b9\u30c9\u30e9\u30a4\u30d0\u3092\u540c\u671f\u5316\u3059\u308b\u305f\u3081\u306bIDE\u30c9\u30e9\u30a4\u30d0\u306b\u3088\u3063\u3066\u4f7f\u308f\u308c\u305f\u30ad\u30e5\u30fc\u306b\u4f3c\u3066\u3044\u308b\u3002\uff08\u7b2c2\u7ae0\u53c2\u7167\uff09<br \/>\n\u3057\u304b\u3057\u3001\u62bd\u8c61\u7684\u306a\u3059\u3079\u3066\u306eIDE\u7279\u6709\u306e\u30b3\u30fc\u30c9\u306f\u306a\u3044\u3002<br \/>\n\u3053\u306e\u30ad\u30e5\u30fc\u306f\u3001\u3072\u3068\u3064\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u30bc\u30ed\u3058\u3083\u306a\u3044\u30dd\u30a4\u30f3\u30bf\u3092\u9001\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u9001\u4fe1\u5074\u3068\u53d7\u4fe1\u5074\u306f\u305d\u308c\u305e\u308c\u3072\u3068\u3064\u3057\u304b\u306a\u304f\u3001\u305d\u308c\u305e\u308c\u5225\u306eCPU\u3067\u5b9f\u884c\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u5b9f\u88c5\u306f\u6b63\u3057\u3044\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 100; title: ; notranslate\" title=\"\">struct q {\r\n  void *ptr;\r\n};\r\n\r\nvoid*\r\nsend(struct q *q, void *p)\r\n{\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n}\r\n\r\nvoid*\r\nrecv(struct q *q)\r\n{\r\n  void *p;\r\n\r\n  while((p = q-&gt;ptr) == 0)\r\n    ;\r\n  q-&gt;ptr = 0;\r\n  return p;\r\n}<\/pre>\n<p>send\u306f\u3001\u30ad\u30e5\u30fc\u304c\u7a7a\uff08ptr == 0\uff09\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3001\u305d\u3057\u3066\u30ad\u30e5\u30fc\u306e\u4e2d\u306b\u30dd\u30a4\u30f3\u30bfp\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nrecv\u306f\u3001\u30ad\u30e5\u30fc\u306b\u4f55\u304b\u5165\u3063\u3066\u308b\u72b6\u614b\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3001\u30dd\u30a4\u30f3\u30bf\u3092\u53d6\u308a\u51fa\u3059\u3002<br \/>\n\u5225\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u3001send\u3068recv\u306f\u4e21\u65b9\u3068\u3082q-&gt;ptr\u3092\u7de8\u96c6\u3059\u308b\u304c\u3001send\u306fq-&gt;ptr\u304c\u30bc\u30ed\u306e\u3068\u304d\u3060\u3051\u66f8\u304d\u8fbc\u307f\u3001recv\u306fq-&gt;ptr\u304c\u30bc\u30ed\u3058\u3083\u306a\u3044\u3068\u304d\u3060\u3051\u66f8\u304d\u8fbc\u3080\u306e\u3067\u3001\u304a\u4e92\u3044\u306b\u8e0f\u307f\u3064\u3051\u308b\u3053\u3068\u306f\u306a\u3044\u3002<\/p>\n<p>\u4e0a\u8a18\u306e\u5b9f\u88c5\u306f\u591a\u5206\u6b63\u3057\u3044\u304c\u3001\u52b9\u7387\u304c\u60aa\u3044\u3002<br \/>\n\u9001\u4fe1\u5074\u304c\u305f\u307e\u306b\u3057\u304b\u9001\u4fe1\u3057\u306a\u3044\u5834\u5408\u3001\u53d7\u4fe1\u5074\u306f\u30dd\u30a4\u30f3\u30bf\u304c\u6765\u308b\u306e\u3092\u671f\u5f85\u3057\u3066\u30eb\u30fc\u30d7\u3057\u3066\u308b\u9593\u3001\u305d\u306e\u6642\u9593\u306e\u307b\u3068\u3093\u3069\u3092\u6d88\u8cbb\u3057\u3066\u3057\u307e\u3046\u3060\u308d\u3046\u3002<br \/>\nsend\u304c\u30dd\u30a4\u30f3\u30bf\u3092\u30bb\u30c3\u30c8\u3057\u305f\u3068\u304d\u306b\u53d7\u4fe1\u5074\u306b\u901a\u77e5\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308c\u3070\u3001\u53d7\u4fe1\u5074\u306eCPU\u306f\uff08\u305d\u306e\u901a\u77e5\u3092\u5f85\u3063\u3066\u308b\u9593\u3001\u4ed6\u306e\uff09\u3082\u3063\u3068\u751f\u7523\u7684\u306a\u4ed5\u4e8b\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u3060\u308d\u3046\u3002<\/p>\n<p>\u6b21\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u3001sleep\u3068wakeup\u3068\u3044\u3046\u95a2\u6570\u306e\u7d44\u306b\u3064\u3044\u3066\u60f3\u50cf\u3057\u3066\u307f\u3088\u3046\u3002<br \/>\nsleep(chan)\u306f\u3001\u30a6\u30a7\u30a4\u30c8\u30c1\u30e3\u30f3\u30cd\u30eb\uff08wait channel\uff09\u3068\u547c\u3070\u308b\u4efb\u610f\u306e\u5024\u3067\u3042\u308bchan\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3002<br \/>\nsleep\u306f\u3001\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u3092\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u3001\u4ed6\u306e\u4ed5\u4e8b\u306e\u305f\u3081\u306bCPU\u3092\u89e3\u653e\u3059\u308b\u3002<br \/>\nwakeup(chan)\u306f\u3001chan\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\uff08\u3082\u3057\u3042\u308b\u306a\u3089\uff09\u3092\u8d77\u3053\u3057\u3001\u305d\u308c\u3089\u306esleep\u306e\u547c\u3073\u51fa\u3057\u3092\u623b\u3089\u305b\u308b\u3002<br \/>\nchan\u4e0a\u3067\u5f85\u3063\u3066\u3044\u308b\u30d7\u30ed\u30bb\u30b9\u304c\u306a\u3044\u5834\u5408\u3001wakeup\u306f\u4f55\u3082\u3057\u306a\u3044\u3002<br \/>\nsleep\u3068wakeup\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u6211\u3005\u306f\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3092\u6b21\u306e\u3088\u3046\u306b\u6539\u826f\u51fa\u6765\u308b\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 201; title: ; notranslate\" title=\"\">void*\r\nsend(struct q *q, void *p)\r\n{\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n  wakeup(q);  \/* wake recv *\/\r\n}\r\n\r\nvoid*\r\nrecv(struct q * q)\r\n{\r\n  void* p;\r\n\r\n  while((p = q-&gt;ptr) == 0)\r\n    sleep(q);\r\n  q-&gt;ptr = 0;\r\n  return p;\r\n}<\/pre>\n<p>recv\u306f\u3001\u30b9\u30d4\u30f3\u3059\u308b\u4ee3\u308f\u308a\u306bCPU\u3092\u89e3\u653e\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002\u7d20\u6575\u3060\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u3053\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u3088\u308bsleep\u3068wakeup\u306e\u8a2d\u8a08\u304c\u76f4\u63a5\u7684\u3067\u306f\u306a\u304f\u3001\u8d77\u304d\u640d\u306a\u3044\u554f\u984c\uff08lost wakeup probrem\uff09\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u308b\u3082\u306e\u304b\u3089\u88ab\u5bb3\u3092\u53d7\u3051\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3002\uff08\u56f34-2\u53c2\u7167\uff09<br \/>\nrecv\u304c215\u884c\u76ee\u3067q-&gt;ptr == 0\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u898b\u3064\u3051\u3001sleep\u3092\u547c\u3076\u3053\u3068\u3092\u6c7a\u5b9a\u3059\u308b\u3068\u4eee\u5b9a\u3057\u3088\u3046\u3002<br \/>\nrecv\u304csleep\u51fa\u6765\u308b\u524d\u3001send\u306f\u4ed6\u306eCPU\u3067\u5b9f\u884c\u3055\u308c\u3066\u3044\u3066\u3001\u305d\u308c\u306fq-&gt;ptr\u3092\u975e\u30bc\u30ed\u306b\u5909\u66f4\u3057wakeup\u3092\u547c\u3076\u3002<br \/>\n\u305d\u306e\u3068\u304dwakeup\u306f\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u7121\u3044\u306e\u3067\u7d50\u679c\u7684\u306b\u4f55\u3082\u3057\u306a\u3044\u3002<br \/>\n\u305d\u3057\u3066recv\u306f216\u884c\u76ee\u306e\u5b9f\u884c\u3092\u7d9a\u884c\u3057\u3001sleep\u3092\u547c\u3073\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3002<br \/>\n\u3053\u308c\u306f\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u3002<br \/>\nrecv\u306f\u3059\u3067\u306b\u30dd\u30a4\u30f3\u30bf\u304c\u5c4a\u3044\u3066\u308b\u306e\u306b\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u5f85\u3064\u4e8b\u306b\u306a\u308b\u3002<br \/>\n\u6b21\u306esend\u306f\u3001recv\u304c\u30ad\u30e5\u30fc\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u53d6\u308a\u51fa\u3059\u306e\u3092\u5f85\u3061\u7d9a\u3051\u505c\u6b62\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067\u30b7\u30b9\u30c6\u30e0\u306f\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u3060\u308d\u3046\u3002<\/p>\n<p><a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37-300x147.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-03-21 16.43.37\" width=\"300\" height=\"147\" class=\"aligncenter size-medium wp-image-1555\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37-300x147.png 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37.png 740w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u56f34-2 \u8d77\u304d\u640d\u306a\u3044\u554f\u984c\u306e\u4f8b<\/p>\n<p>\u3053\u306e\u554f\u984c\u306e\u6839\u6e90\u306f\u3001recv\u306fq-&gt;ptr == 0\u306e\u3068\u304d\u3060\u3051\u30b9\u30ea\u30fc\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u30a4\u30f3\u30d0\u30ea\u30a2\u30f3\u30c8\u304c\u3001\u90fd\u5408\u306e\u60aa\u3044\u77ac\u9593\u306b\u5b9f\u884c\u3055\u308c\u3066\u3044\u308bsend\u306b\u3088\u3063\u3066\u4fb5\u5bb3\u3055\u308c\u308b\u3068\u3044\u3046\u3068\u3053\u308d\u306b\u3042\u308b\u3002<br \/>\n\u3053\u306e\u30a4\u30f3\u30d0\u30ea\u30a2\u30f3\u30c8\u3092\u4fdd\u8b77\u3059\u308b\u305f\u3081\u306b\u3001\u6211\u3005\u306f\u3001sleep\u3092\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u304c\u30b9\u30ea\u30fc\u30d7\u3057\u305f\u3042\u3068\u306bsleep\u306b\u3088\u3063\u3066\u89e3\u653e\u3055\u308c\u308b\u3088\u3046\u306a\u30ed\u30c3\u30af\u3092\u5c0e\u5165\u3059\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u4e0a\u8a18\u306e\u4f8b\u306e\u3088\u3046\u306a\u8d77\u304d\u640d\u306a\u3044\u3092\u907f\u3051\u308b\u3002<br \/>\n\u4e00\u5ea6sleep\u3092\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u304c\u518d\u5ea6\u8d77\u304d\u305f\u3089\u3001sleep\u306f\u623b\u308b\u524d\u306b\u305d\u306e\u30ed\u30c3\u30af\u3092\u518d\u7372\u5f97\u3059\u308b\u3002<br \/>\n\u6b21\u306e\u30b3\u30fc\u30c9\u304c\u3042\u308a\u5f97\u308b\u3068\u3057\u305f\u3044\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 300; title: ; notranslate\" title=\"\">struct q {\r\n  struct spinlock lock;\r\n  void *ptr;\r\n}\r\n\r\nvoid*\r\nsend(struct q *q, void *p)\r\n{\r\n  acquire(&amp;q-&gt;lock);\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n  wakeup(q);\r\n  release(&amp;q-&gt;lock);\r\n}\r\n\r\nvoid*\r\nrecv(struct q *q)\r\n{\r\n  void* p;\r\n\r\n  acquire(&amp;q-&gt;lock);\r\n  while((p = q-&gt;ptr) == 0)\r\n    sleep(q, &amp;q-&gt;lock);\r\n  q-&gt;ptr = 0;\r\n  release(&amp;q-&gt;lock);\r\n  return p;\r\n}<\/pre>\n<p>recv\u304cq-&gt;lock\u3092\u4fdd\u6301\u3059\u308b\u3068\u3044\u3046\u4e8b\u5b9f\u306f\u3001recv\u304cq-&gt;ptr\u3092\u30c1\u30a7\u30c3\u30af\u3057sleep\u3092\u547c\u3076\u9593\u306b\u3001send\u304cwakeup\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u9632\u3050\u3002<br \/>\n\u3082\u3061\u308d\u3093\u3001\u3053\u306e\u307e\u307e\u3060\u3068\u9001\u4fe1\u5074\u306e\u30d7\u30ed\u30bb\u30b9\u304cwakeup\u3092\u547c\u3076\u306e\u3092\u59a8\u5bb3\u3057\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u306e\u3067\u3001\u53d7\u4fe1\u5074\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u9593q-&gt;lock\u306f\u4fdd\u6301\u3057\u306a\u3044\u307b\u3046\u304c\u3044\u3044\u3002<br \/>\n\u3060\u304b\u3089\u3001sleep\u304c\u30a2\u30c8\u30df\u30c3\u30af\u306bq-&gt;lock\u3092\u958b\u653e\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u3092\u7720\u308a\u306b\u5c0e\u304f\u3068\u3044\u3046\u3053\u3068\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<\/p>\n<p>\u5b8c\u5168\u306a\u9001\u4fe1\u5074\/\u53d7\u4fe1\u5074\u306e\u5b9f\u88c5\u3067\u306f\u3001\u524d\u56de\u306esend\u3067\u9001\u3089\u308c\u305f\u5024\u3092\u53d7\u4fe1\u5074\u304c\u53d6\u308a\u51fa\u3059\u306e\u3092\u5f85\u3064\u3068\u304d\u306b\u3082\u3001send\u5185\u3067\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u3060\u308d\u3046\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3068\u3001\u305d\u306e\u969b\u306e\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u306b\u306a\u308bsleep\u3068wakeup\u306b\u3064\u3044\u3066\u306e\u8a71\u3067\u3059\u3002<br \/>\n\u91cd\u8981\u306a\u306e\u306f\u3001\u3053\u3053\u3067\u306f\u307e\u3060\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u4e00\u5207\u51fa\u3066\u304d\u3066\u306a\u3044\u3068\u3044\u3046\u4e8b\u3067\u3059\u3002<br \/>\n\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u591a\u5206\u6b21\u306e\u7bc0\u3067\u51fa\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306e\u4f8b\u3067\u306f\u3001\u5909\u306a\u30bf\u30a4\u30df\u30f3\u30b0\u3067sleep\u3084wakeup\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044\u3088\u3046\u306b\u30ed\u30c3\u30af\u304c\u5c0e\u5165\u3055\u308c\u3066\u307e\u3059\u304c\u3001\u305d\u306e\u307e\u307e\u3060\u3068recv\u304c\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u30c7\u30fc\u30bf\u304c\u5c4a\u304f\u306e\u3092\u5f85\u3063\u3066\u3044\u308b\u9593\u3001send\u306f\u4f55\u3082\u51fa\u6765\u306a\u3044\uff08acquire\u3067\u6b62\u307e\u308b\uff09\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001sleep\u5185\u3067\u306f\uff08\u672c\u6587\u3067\u306f\u30b3\u30fc\u30c9\u306f\u793a\u3055\u308c\u3066\u307e\u305b\u3093\u304c\uff09\u901a\u5e38\u306e\u7372\u5f97\u2192\u89e3\u653e\u3068\u3044\u3046\u9806\u5e8f\u3068\u306f\u9006\u306b\u3001q-&gt;lock\u3092\u89e3\u653e\u3057\u3066\u304b\u3089\u7372\u5f97\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001\u53d7\u4fe1\u5074\u304c\u30b9\u30ea\u30fc\u30d7\u4e2d\uff08\u30c7\u30fc\u30bf\u5f85\u3061\u4e2d\uff09\u306fsend\u304c\u5b9f\u884c\u53ef\u80fd\u306b\u306a\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e55\u301c57\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>\u30ed\u30c3\u30af\u306f\u3001CPU\u540c\u58eb\u3084\u30d7\u30ed\u30bb\u30b9\u540c\u58eb\u304c\u304a\u4e92\u3044\u306b\u5e72\u6e09\u3057\u5408\u3046\u306e\u3092\u907f\u3051\u308b\u306e\u306b\u5f79\u7acb\u3061\u3001\u30b9\u30b1\u30b8\u30e5\u30fc\u30ea\u30f3\u30b0\u306f\u3001\u8907\u6570\u306e\u30d7\u30ed\u30bb\u30b9\u304cCPU\u3092\u5171\u6709\u3059\u308b\u306e\u306b\u5f79\u7acb\u3064\u3002<br \/>\n\u3057\u304b\u3057\u3053\u308c\u307e\u3067\u3001\u6211\u3005\u306f\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3092\u697d\u306b\u3059\u308b\u3088\u3046\u306a\u6982\u5ff5\u306f\u6301\u3061\u5408\u308f\u305b\u3066\u3044\u306a\u304b\u3063\u305f\u3002<br \/>\n&#8220;\u30b9\u30ea\u30fc\u30d7&#8221;\uff08sleep\uff09\u3068&#8221;\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7&#8221;\uff08wakeup\uff09\u306f\u305d\u306e\u9593\u9699\u3092\u57cb\u3081\u3001\u3042\u308b\u30d7\u30ed\u30bb\u30b9\u304c\u3042\u308b\u30a4\u30d9\u30f3\u30c8\u3092\u5f85\u3064\u305f\u3081\u306b\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u3001\u4e00\u5ea6\u30a4\u30d9\u30f3\u30c8\u304c\u8d77\u304d\u305f\u3089\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7\u3055\u305b\u308b\u4e8b\u3092\u53ef\u80fd\u306b\u3059\u308b\u3002<br \/>\n\u30b9\u30ea\u30fc\u30d7\u3068\u30a6\u30a7\u30a4\u30af\u30a2\u30c3\u30d7\u306f\u3001\u3057\u3070\u3057\u3070\u9806\u5e8f\u5354\u8abf\uff08\u30b7\u30fc\u30b1\u30f3\u30b9\u30b3\u30fc\u30c7\u30a3\u30cd\u30fc\u30b7\u30e7\u30f3, sequence coordination, \u30aa\u30ec\u30aa\u30ec\u8a33\u306a\u306e\u3067\u6ce8\u610f\uff09\u3001\u6761\u4ef6\u4ed8\u304d\u540c\u671f\u5316\uff08\u30b3\u30f3\u30c7\u30a3\u30b7\u30e7\u30ca\u30eb\u30b7\u30f3\u30af\u30ed\u30ca\u30a4\u30bc\u30fc\u30b7\u30e7\u30f3, conditional synchronization, \u3053\u3061\u3089\u3082\u30aa\u30ec\u30aa\u30ec\u8a33\u306a\u306e\u3067\u6ce8\u610f\uff09\u306e\u6a5f\u69cb\u3068\u547c\u3070\u308c\u308b\u3002<br \/>\nOS\u5b66\u7684\u306b\u4ed6\u306e\u4f3c\u305f\u3088\u3046\u306a\u6a5f\u69cb\u306f\u305f\u304f\u3055\u3093\u3042\u308b\u3002<\/p>\n<p>\u6211\u3005\u304c\u4f55\u3092\u8a00\u3044\u305f\u3044\u304b\u30cf\u30c3\u30ad\u30ea\u3055\u305b\u308b\u305f\u3081\u306b\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u751f\u7523\u8005\/\u6d88\u8cbb\u8005\uff08producer\/consumer\uff09\u30ad\u30e5\u30fc\u306b\u3064\u3044\u3066\u8003\u3048\u3066\u307f\u3088\u3046\u3002<br \/>\n\u3053\u306e\u30ad\u30e5\u30fc\u306f\u3001\u30d7\u30ed\u30bb\u30c3\u30b5\u3068\u30c7\u30d0\u30a4\u30b9\u30c9\u30e9\u30a4\u30d0\u3092\u540c\u671f\u5316\u3059\u308b\u305f\u3081\u306bIDE\u30c9\u30e9\u30a4\u30d0\u306b\u3088\u3063\u3066\u4f7f\u308f\u308c\u305f\u30ad\u30e5\u30fc\u306b\u4f3c\u3066\u3044\u308b\u3002\uff08\u7b2c2\u7ae0\u53c2\u7167\uff09<br \/>\n\u3057\u304b\u3057\u3001\u62bd\u8c61\u7684\u306a\u3059\u3079\u3066\u306eIDE\u7279\u6709\u306e\u30b3\u30fc\u30c9\u306f\u306a\u3044\u3002<br \/>\n\u3053\u306e\u30ad\u30e5\u30fc\u306f\u3001\u3072\u3068\u3064\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u30bc\u30ed\u3058\u3083\u306a\u3044\u30dd\u30a4\u30f3\u30bf\u3092\u9001\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u9001\u4fe1\u5074\u3068\u53d7\u4fe1\u5074\u306f\u305d\u308c\u305e\u308c\u3072\u3068\u3064\u3057\u304b\u306a\u304f\u3001\u305d\u308c\u305e\u308c\u5225\u306eCPU\u3067\u5b9f\u884c\u3055\u308c\u3066\u3044\u308b\u3068\u4eee\u5b9a\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u5b9f\u88c5\u306f\u6b63\u3057\u3044\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 100; title: ; notranslate\" title=\"\">struct q {\r\n  void *ptr;\r\n};\r\n\r\nvoid*\r\nsend(struct q *q, void *p)\r\n{\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n}\r\n\r\nvoid*\r\nrecv(struct q *q)\r\n{\r\n  void *p;\r\n\r\n  while((p = q-&gt;ptr) == 0)\r\n    ;\r\n  q-&gt;ptr = 0;\r\n  return p;\r\n}<\/pre>\n<p>send\u306f\u3001\u30ad\u30e5\u30fc\u304c\u7a7a\uff08ptr == 0\uff09\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3001\u305d\u3057\u3066\u30ad\u30e5\u30fc\u306e\u4e2d\u306b\u30dd\u30a4\u30f3\u30bfp\u3092\u30bb\u30c3\u30c8\u3059\u308b\u3002<br \/>\nrecv\u306f\u3001\u30ad\u30e5\u30fc\u306b\u4f55\u304b\u5165\u3063\u3066\u308b\u72b6\u614b\u306b\u306a\u308b\u307e\u3067\u30eb\u30fc\u30d7\u3057\u3001\u30dd\u30a4\u30f3\u30bf\u3092\u53d6\u308a\u51fa\u3059\u3002<br \/>\n\u5225\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u5b9f\u884c\u3055\u308c\u308b\u3068\u304d\u3001send\u3068recv\u306f\u4e21\u65b9\u3068\u3082q-&gt;ptr\u3092\u7de8\u96c6\u3059\u308b\u304c\u3001send\u306fq-&gt;ptr\u304c\u30bc\u30ed\u306e\u3068\u304d\u3060\u3051\u66f8\u304d\u8fbc\u307f\u3001recv\u306fq-&gt;ptr\u304c\u30bc\u30ed\u3058\u3083\u306a\u3044\u3068\u304d\u3060\u3051\u66f8\u304d\u8fbc\u3080\u306e\u3067\u3001\u304a\u4e92\u3044\u306b\u8e0f\u307f\u3064\u3051\u308b\u3053\u3068\u306f\u306a\u3044\u3002<\/p>\n<p>\u4e0a\u8a18\u306e\u5b9f\u88c5\u306f\u591a\u5206\u6b63\u3057\u3044\u304c\u3001\u52b9\u7387\u304c\u60aa\u3044\u3002<br \/>\n\u9001\u4fe1\u5074\u304c\u305f\u307e\u306b\u3057\u304b\u9001\u4fe1\u3057\u306a\u3044\u5834\u5408\u3001\u53d7\u4fe1\u5074\u306f\u30dd\u30a4\u30f3\u30bf\u304c\u6765\u308b\u306e\u3092\u671f\u5f85\u3057\u3066\u30eb\u30fc\u30d7\u3057\u3066\u308b\u9593\u3001\u305d\u306e\u6642\u9593\u306e\u307b\u3068\u3093\u3069\u3092\u6d88\u8cbb\u3057\u3066\u3057\u307e\u3046\u3060\u308d\u3046\u3002<br \/>\nsend\u304c\u30dd\u30a4\u30f3\u30bf\u3092\u30bb\u30c3\u30c8\u3057\u305f\u3068\u304d\u306b\u53d7\u4fe1\u5074\u306b\u901a\u77e5\u3059\u308b\u65b9\u6cd5\u304c\u3042\u308c\u3070\u3001\u53d7\u4fe1\u5074\u306eCPU\u306f\uff08\u305d\u306e\u901a\u77e5\u3092\u5f85\u3063\u3066\u308b\u9593\u3001\u4ed6\u306e\uff09\u3082\u3063\u3068\u751f\u7523\u7684\u306a\u4ed5\u4e8b\u3092\u898b\u3064\u3051\u308b\u3053\u3068\u304c\u3067\u304d\u305f\u3060\u308d\u3046\u3002<\/p>\n<p>\u6b21\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3059\u308b\u3001sleep\u3068wakeup\u3068\u3044\u3046\u95a2\u6570\u306e\u7d44\u306b\u3064\u3044\u3066\u60f3\u50cf\u3057\u3066\u307f\u3088\u3046\u3002<br \/>\nsleep(chan)\u306f\u3001\u30a6\u30a7\u30a4\u30c8\u30c1\u30e3\u30f3\u30cd\u30eb\uff08wait channel\uff09\u3068\u547c\u3070\u308b\u4efb\u610f\u306e\u5024\u3067\u3042\u308bchan\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3002<br \/>\nsleep\u306f\u3001\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u3092\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u3001\u4ed6\u306e\u4ed5\u4e8b\u306e\u305f\u3081\u306bCPU\u3092\u89e3\u653e\u3059\u308b\u3002<br \/>\nwakeup(chan)\u306f\u3001chan\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\uff08\u3082\u3057\u3042\u308b\u306a\u3089\uff09\u3092\u8d77\u3053\u3057\u3001\u305d\u308c\u3089\u306esleep\u306e\u547c\u3073\u51fa\u3057\u3092\u623b\u3089\u305b\u308b\u3002<br \/>\nchan\u4e0a\u3067\u5f85\u3063\u3066\u3044\u308b\u30d7\u30ed\u30bb\u30b9\u304c\u306a\u3044\u5834\u5408\u3001wakeup\u306f\u4f55\u3082\u3057\u306a\u3044\u3002<br \/>\nsleep\u3068wakeup\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u6211\u3005\u306f\u30ad\u30e5\u30fc\u306e\u5b9f\u88c5\u3092\u6b21\u306e\u3088\u3046\u306b\u6539\u826f\u51fa\u6765\u308b\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 201; title: ; notranslate\" title=\"\">void*\r\nsend(struct q *q, void *p)\r\n{\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n  wakeup(q);  \/* wake recv *\/\r\n}\r\n\r\nvoid*\r\nrecv(struct q * q)\r\n{\r\n  void* p;\r\n\r\n  while((p = q-&gt;ptr) == 0)\r\n    sleep(q);\r\n  q-&gt;ptr = 0;\r\n  return p;\r\n}<\/pre>\n<p>recv\u306f\u3001\u30b9\u30d4\u30f3\u3059\u308b\u4ee3\u308f\u308a\u306bCPU\u3092\u89e3\u653e\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u305f\u3002\u7d20\u6575\u3060\u3002<br \/>\n\u3057\u304b\u3057\u306a\u304c\u3089\u3001\u3053\u306e\u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30a4\u30b9\u306b\u3088\u308bsleep\u3068wakeup\u306e\u8a2d\u8a08\u304c\u76f4\u63a5\u7684\u3067\u306f\u306a\u304f\u3001\u8d77\u304d\u640d\u306a\u3044\u554f\u984c\uff08lost wakeup probrem\uff09\u3068\u3057\u3066\u77e5\u3089\u308c\u3066\u3044\u308b\u3082\u306e\u304b\u3089\u88ab\u5bb3\u3092\u53d7\u3051\u308b\u3053\u3068\u304c\u5206\u304b\u308b\u3002\uff08\u56f34-2\u53c2\u7167\uff09<br \/>\nrecv\u304c215\u884c\u76ee\u3067q-&gt;ptr == 0\u3067\u3042\u308b\u3068\u3044\u3046\u3053\u3068\u3092\u898b\u3064\u3051\u3001sleep\u3092\u547c\u3076\u3053\u3068\u3092\u6c7a\u5b9a\u3059\u308b\u3068\u4eee\u5b9a\u3057\u3088\u3046\u3002<br \/>\nrecv\u304csleep\u51fa\u6765\u308b\u524d\u3001send\u306f\u4ed6\u306eCPU\u3067\u5b9f\u884c\u3055\u308c\u3066\u3044\u3066\u3001\u305d\u308c\u306fq-&gt;ptr\u3092\u975e\u30bc\u30ed\u306b\u5909\u66f4\u3057wakeup\u3092\u547c\u3076\u3002<br \/>\n\u305d\u306e\u3068\u304dwakeup\u306f\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u7121\u3044\u306e\u3067\u7d50\u679c\u7684\u306b\u4f55\u3082\u3057\u306a\u3044\u3002<br \/>\n\u305d\u3057\u3066recv\u306f216\u884c\u76ee\u306e\u5b9f\u884c\u3092\u7d9a\u884c\u3057\u3001sleep\u3092\u547c\u3073\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3002<br \/>\n\u3053\u308c\u306f\u554f\u984c\u3092\u5f15\u304d\u8d77\u3053\u3059\u3002<br \/>\nrecv\u306f\u3059\u3067\u306b\u30dd\u30a4\u30f3\u30bf\u304c\u5c4a\u3044\u3066\u308b\u306e\u306b\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u5f85\u3064\u4e8b\u306b\u306a\u308b\u3002<br \/>\n\u6b21\u306esend\u306f\u3001recv\u304c\u30ad\u30e5\u30fc\u306e\u30dd\u30a4\u30f3\u30bf\u3092\u53d6\u308a\u51fa\u3059\u306e\u3092\u5f85\u3061\u7d9a\u3051\u505c\u6b62\u3059\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067\u30b7\u30b9\u30c6\u30e0\u306f\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u3060\u308d\u3046\u3002<\/p>\n<p><a href=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37-300x147.png\" alt=\"\" title=\"\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8 2012-03-21 16.43.37\" width=\"300\" height=\"147\" class=\"aligncenter size-medium wp-image-1555\" srcset=\"https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37-300x147.png 300w, https:\/\/peta.okechan.net\/blog\/wp-content\/uploads\/2012\/03\/\u30b9\u30af\u30ea\u30fc\u30f3\u30b7\u30e7\u30c3\u30c8-2012-03-21-16.43.37.png 740w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\n\u56f34-2 \u8d77\u304d\u640d\u306a\u3044\u554f\u984c\u306e\u4f8b<\/p>\n<p>\u3053\u306e\u554f\u984c\u306e\u6839\u6e90\u306f\u3001recv\u306fq-&gt;ptr == 0\u306e\u3068\u304d\u3060\u3051\u30b9\u30ea\u30fc\u30d7\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u30a4\u30f3\u30d0\u30ea\u30a2\u30f3\u30c8\u304c\u3001\u90fd\u5408\u306e\u60aa\u3044\u77ac\u9593\u306b\u5b9f\u884c\u3055\u308c\u3066\u3044\u308bsend\u306b\u3088\u3063\u3066\u4fb5\u5bb3\u3055\u308c\u308b\u3068\u3044\u3046\u3068\u3053\u308d\u306b\u3042\u308b\u3002<br \/>\n\u3053\u306e\u30a4\u30f3\u30d0\u30ea\u30a2\u30f3\u30c8\u3092\u4fdd\u8b77\u3059\u308b\u305f\u3081\u306b\u3001\u6211\u3005\u306f\u3001sleep\u3092\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u304c\u30b9\u30ea\u30fc\u30d7\u3057\u305f\u3042\u3068\u306bsleep\u306b\u3088\u3063\u3066\u89e3\u653e\u3055\u308c\u308b\u3088\u3046\u306a\u30ed\u30c3\u30af\u3092\u5c0e\u5165\u3059\u308b\u3002<br \/>\n\u3053\u308c\u306f\u3001\u4e0a\u8a18\u306e\u4f8b\u306e\u3088\u3046\u306a\u8d77\u304d\u640d\u306a\u3044\u3092\u907f\u3051\u308b\u3002<br \/>\n\u4e00\u5ea6sleep\u3092\u547c\u3073\u51fa\u3057\u305f\u30d7\u30ed\u30bb\u30b9\u304c\u518d\u5ea6\u8d77\u304d\u305f\u3089\u3001sleep\u306f\u623b\u308b\u524d\u306b\u305d\u306e\u30ed\u30c3\u30af\u3092\u518d\u7372\u5f97\u3059\u308b\u3002<br \/>\n\u6b21\u306e\u30b3\u30fc\u30c9\u304c\u3042\u308a\u5f97\u308b\u3068\u3057\u305f\u3044\u3002<\/p>\n<pre class=\"brush: cpp; first-line: 300; title: ; notranslate\" title=\"\">struct q {\r\n  struct spinlock lock;\r\n  void *ptr;\r\n}\r\n\r\nvoid*\r\nsend(struct q *q, void *p)\r\n{\r\n  acquire(&amp;q-&gt;lock);\r\n  while(q-&gt;ptr != 0)\r\n    ;\r\n  q-&gt;ptr = p;\r\n  wakeup(q);\r\n  release(&amp;q-&gt;lock);\r\n}\r\n\r\nvoid*\r\nrecv(struct q *q)\r\n{\r\n  void* p;\r\n\r\n  acquire(&amp;q-&gt;lock);\r\n  while((p = q-&gt;ptr) == 0)\r\n    sleep(q, &amp;q-&gt;lock);\r\n  q-&gt;ptr = 0;\r\n  release(&amp;q-&gt;lock);\r\n  return p;\r\n}<\/pre>\n<p>recv\u304cq-&gt;lock\u3092\u4fdd\u6301\u3059\u308b\u3068\u3044\u3046\u4e8b\u5b9f\u306f\u3001recv\u304cq-&gt;ptr\u3092\u30c1\u30a7\u30c3\u30af\u3057sleep\u3092\u547c\u3076\u9593\u306b\u3001send\u304cwakeup\u3092\u5b9f\u884c\u3059\u308b\u3053\u3068\u3092\u9632\u3050\u3002<br \/>\n\u3082\u3061\u308d\u3093\u3001\u3053\u306e\u307e\u307e\u3060\u3068\u9001\u4fe1\u5074\u306e\u30d7\u30ed\u30bb\u30b9\u304cwakeup\u3092\u547c\u3076\u306e\u3092\u59a8\u5bb3\u3057\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u306e\u3067\u3001\u53d7\u4fe1\u5074\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u9593q-&gt;lock\u306f\u4fdd\u6301\u3057\u306a\u3044\u307b\u3046\u304c\u3044\u3044\u3002<br \/>\n\u3060\u304b\u3089\u3001sleep\u304c\u30a2\u30c8\u30df\u30c3\u30af\u306bq-&gt;lock\u3092\u958b\u653e\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u3092\u7720\u308a\u306b\u5c0e\u304f\u3068\u3044\u3046\u3053\u3068\u304c\u5fc5\u8981\u3067\u3042\u308b\u3002<\/p>\n<p>\u5b8c\u5168\u306a\u9001\u4fe1\u5074\/\u53d7\u4fe1\u5074\u306e\u5b9f\u88c5\u3067\u306f\u3001\u524d\u56de\u306esend\u3067\u9001\u3089\u308c\u305f\u5024\u3092\u53d7\u4fe1\u5074\u304c\u53d6\u308a\u51fa\u3059\u306e\u3092\u5f85\u3064\u3068\u304d\u306b\u3082\u3001send\u5185\u3067\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u3060\u308d\u3046\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>\u30d7\u30ed\u30bb\u30b9\u9593\u901a\u4fe1\u3068\u3001\u305d\u306e\u969b\u306e\u5f37\u529b\u306a\u30c4\u30fc\u30eb\u306b\u306a\u308bsleep\u3068wakeup\u306b\u3064\u3044\u3066\u306e\u8a71\u3067\u3059\u3002<br \/>\n\u91cd\u8981\u306a\u306e\u306f\u3001\u3053\u3053\u3067\u306f\u307e\u3060\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u4e00\u5207\u51fa\u3066\u304d\u3066\u306a\u3044\u3068\u3044\u3046\u4e8b\u3067\u3059\u3002<br \/>\n\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f\u591a\u5206\u6b21\u306e\u7bc0\u3067\u51fa\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u6700\u5f8c\u306e\u4f8b\u3067\u306f\u3001\u5909\u306a\u30bf\u30a4\u30df\u30f3\u30b0\u3067sleep\u3084wakeup\u304c\u5b9f\u884c\u3055\u308c\u306a\u3044\u3088\u3046\u306b\u30ed\u30c3\u30af\u304c\u5c0e\u5165\u3055\u308c\u3066\u307e\u3059\u304c\u3001\u305d\u306e\u307e\u307e\u3060\u3068recv\u304c\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u30c7\u30fc\u30bf\u304c\u5c4a\u304f\u306e\u3092\u5f85\u3063\u3066\u3044\u308b\u9593\u3001send\u306f\u4f55\u3082\u51fa\u6765\u306a\u3044\uff08acquire\u3067\u6b62\u307e\u308b\uff09\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n\u3053\u308c\u3092\u907f\u3051\u308b\u305f\u3081\u306b\u3001sleep\u5185\u3067\u306f\uff08\u672c\u6587\u3067\u306f\u30b3\u30fc\u30c9\u306f\u793a\u3055\u308c\u3066\u307e\u305b\u3093\u304c\uff09\u901a\u5e38\u306e\u7372\u5f97\u2192\u89e3\u653e\u3068\u3044\u3046\u9806\u5e8f\u3068\u306f\u9006\u306b\u3001q-&gt;lock\u3092\u89e3\u653e\u3057\u3066\u304b\u3089\u7372\u5f97\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001\u53d7\u4fe1\u5074\u304c\u30b9\u30ea\u30fc\u30d7\u4e2d\uff08\u30c7\u30fc\u30bf\u5f85\u3061\u4e2d\uff09\u306fsend\u304c\u5b9f\u884c\u53ef\u80fd\u306b\u306a\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[405],"class_list":["post-1554","post","type-post","status-publish","format-standard","hentry","category-tech","tag-xv6"],"_links":{"self":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/comments?post=1554"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1554\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}