{"id":1561,"date":"2012-03-22T21:43:09","date_gmt":"2012-03-22T12:43:09","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1561"},"modified":"2012-03-22T21:43:09","modified_gmt":"2012-03-22T12:43:09","slug":"xv6-48-chapter-4-scheduling-code-sleep-and-wakeup","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1561","title":{"rendered":"[xv6 #48] Chapter 4 &#8211; Scheduling &#8211; Code: Sleep and wakeup"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e58\u301c59\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306b\u304a\u3051\u308bsleep\u3068wakeup\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u898b\u3066\u307f\u3088\u3046\u3002<br \/>\nsleep\u306b\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u3092SLEEPING\u3068\u3057\u3066\u5370\u3092\u3064\u3051\u3055\u305b\u3001\u305d\u3057\u3066sched\u3092\u547c\u3073\u305d\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u89e3\u653e\u3057\u3001wakeup\u306f\u4e0e\u3048\u3089\u308c\u305f\u30dd\u30a4\u30f3\u30bf\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u63a2\u3057\u3001\u305d\u308c\u3092RUNNABLE\u3068\u3057\u3066\u5370\u3092\u3064\u3051\u308b\u3001\u3068\u3044\u3046\u306e\u304c\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u3067\u3042\u308b\u3002<\/p>\n<p>sleep\u306f\u3044\u304f\u3064\u304b\u306e\u6761\u4ef6\u30c1\u30a7\u30c3\u30af\u304b\u3089\u59cb\u307e\u308b\u3002<br \/>\n\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u3042\u308a\uff08proc == 0 \u3060\u3068panic\uff09\u3001\u30ed\u30c3\u30af\u3092\u6e21\u3055\u308c\u3066\u306a\u3051\u308c\u3070\uff08lk == 0 \u3067\u3082panic\uff09\u306a\u3089\u306a\u3044\u3002<br \/>\n\u30c1\u30a7\u30c3\u30af\u304c\u7d42\u308f\u308b\u3068\u307e\u305a\u3001ptable.lock\u3092\u7372\u5f97\u3059\u308b\u3002<br \/>\n\u305d\u306e\u307e\u307e\u3067\u306f\u3001ptable.lock\u3068lk\u306e\u4e21\u65b9\u3092\u4fdd\u6301\u3057\u305f\u307e\u307e\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u3057\u307e\u3046\u3002<br \/>\nlk\u306e\u4fdd\u6301\u306f\u3001\uff08\u4f8b\u3048\u3070recv\u306e\u3088\u3046\u306a\uff09\u547c\u3073\u51fa\u3057\u5074\u3067\u5fc5\u8981\u3068\u3055\u308c\u308b\u3002<br \/>\nlk\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001wakeup(chan)\u3092\u547c\u3073\u306f\u3058\u3081\u308c\u308b\u30d7\u30ed\u30bb\u30b9\uff08\u4f8b\u3048\u3070send\u3092\u5b9f\u884c\u3057\u3066\u308b\u30d7\u30ed\u30bb\u30b9\uff09\u304c\u4ed6\u306b\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067sleep\u306fptable.lock\u3092\u4fdd\u6301\u3057\u3001lk\u3092\u5b89\u5168\u306b\u89e3\u653e\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002<br \/>\n\u4ed6\u306e\u3044\u304f\u3064\u304b\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u3001wakeup(chan)\u3092\u547c\u3073\u51fa\u3059\u304b\u3082\u3057\u308c\u306a\u3044\u304c\u3001wakeup\u306fptable.lock\u304c\u7372\u5f97\u53ef\u80fd\u306b\u306a\u308b\u307e\u3067\u5b9f\u884c\u3055\u308c\u306a\u3044\u306e\u3067\u3001sleep\u304c\u30d7\u30ed\u30bb\u30b9\u3092\u78ba\u5b9f\u306b\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u307e\u3067\u5f85\u305f\u306a\u3051\u308c\u3070\u306a\u3089\u305a\u3001\u8d77\u304d\u640d\u306a\u3044\u306e\u554f\u984c\uff08\u524d\u7bc0\u53c2\u7167\uff09\u306f\u8d77\u304d\u306a\u3044\u3002<\/p>\n<p>proc.c\u306esleep\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Atomically release lock and sleep on chan.\r\n\/\/ Reacquires lock when awakened.\r\nvoid\r\nsleep(void *chan, struct spinlock *lk)\r\n{\r\n  if(proc == 0)\r\n    panic(&quot;sleep&quot;);\r\n\r\n  if(lk == 0)\r\n    panic(&quot;sleep without lk&quot;);\r\n\r\n  \/\/ Must acquire ptable.lock in order to\r\n  \/\/ change p-&gt;state and then call sched.\r\n  \/\/ Once we hold ptable.lock, we can be\r\n  \/\/ guaranteed that we won't miss any wakeup\r\n  \/\/ (wakeup runs with ptable.lock locked),\r\n  \/\/ so it's okay to release lk.\r\n  if(lk != &amp;ptable.lock){  \/\/DOC: sleeplock0\r\n    acquire(&amp;ptable.lock);  \/\/DOC: sleeplock1\r\n    release(lk);\r\n  }\r\n\r\n  \/\/ Go to sleep.\r\n  proc-&gt;chan = chan;\r\n  proc-&gt;state = SLEEPING;\r\n  sched();\r\n\r\n  \/\/ Tidy up.\r\n  proc-&gt;chan = 0;\r\n\r\n  \/\/ Reacquire original lock.\r\n  if(lk != &amp;ptable.lock){  \/\/DOC: sleeplock2\r\n    release(&amp;ptable.lock);\r\n    acquire(lk);\r\n  }\r\n}<\/pre>\n<p>\u5fae\u5999\u306b\u8907\u96d1\u306a\u5834\u5408\u304c\u3042\u308b\u3002<br \/>\n\u3082\u3057lk\u304c&amp;ptable.lock\u3068\u7b49\u3057\u3044\u5834\u5408\u3001sleep\u306f\u305d\u308c\u3092&amp;ptable.lock\u3068\u3057\u3066\u7372\u5f97\u3057\u3001lk\u3068\u3057\u3066\u89e3\u653e\u3057\u3088\u3046\u3068\u3057\u3066\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u5834\u5408\u3001sleep\u306facquire\u3068relese\u304c\u304a\u4e92\u3044\u306b\u76f8\u6bba\u3055\u308c\u308b\u3053\u3068\u3092\u8003\u616e\u3057\u3001\u305d\u308c\u3089\u3092\u5b8c\u5168\u306b\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002<\/p>\n<p>\u3053\u306e\u6642\u70b9\u3067\u3001sleep\u306fptable.lock\u306e\u307f\u3092\u4fdd\u6301\u3057\u3001\u30b9\u30ea\u30fc\u30d7\u30c1\u30e3\u30f3\u30cd\u30eb\u8a18\u9332\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092\u5909\u66f4\u3057\u3001sched\u3092\u547c\u3076\u3053\u3068\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u3092\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u4e8b\u304c\u3067\u304d\u308b\u3002<\/p>\n<p>\u5f8c\u306e\u3069\u3053\u304b\u306e\u6bb5\u968e\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u306fwakeup(chan)\u3092\u547c\u3076\u3060\u308d\u3046\u3002<br \/>\nwakeup\u306fptable.lock\u3092\u7372\u5f97\u3057\u3001\u5b9f\u969b\u306e\u51e6\u7406\u3092\u884c\u3046wakeup1\u3092\u547c\u3076\u3002<br \/>\nwakeup1\u306f\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092\u64cd\u4f5c\u3059\u308b\u3057\u3001\u524d\u7bc0\u3067\u898b\u305f\u3088\u3046\u306b\u3001ptable.lock\u306fsleep\u3068wakeup\u304c\u304a\u4e92\u3044\u306b\u6a5f\u4f1a\u3092\u9003\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u306e\u3067\u3001wakeup\u304cptable.lock\u3092\u4fdd\u6301\u3059\u308b\u4e8b\u306f\u3068\u3066\u3082\u91cd\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3068\u304d\u304a\u308a\u3001\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306fptable.lock\u3092\u65e2\u306b\u4fdd\u6301\u3057\u3066\u3044\u308b\u3068\u304d\u306bwakeup\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001wakeup1\u3068\u3057\u3066\u5206\u96e2\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u76f4\u63a5wakeup1\u3092\u547c\u3076\u4f8b\u306f\u5f8c\u3067\u8aac\u660e\u3059\u308b\u3002<br \/>\nwakeup1\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u8d70\u67fb\u3059\u308b\u3002<br \/>\nchan\u304c\u5408\u81f4\u3057\u72b6\u614b\u304cSLEEPING\u306a\u30d7\u30ed\u30bb\u30b9\u3092\u898b\u3064\u3051\u305f\u3089\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092RUNNABLE\u306b\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u6b21\u306b\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u304c\u5b9f\u884c\u3055\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u5b9f\u884c\u6e96\u5099\u304c\u6574\u3063\u3066\u308b\u3068\u3057\u3066\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306b\u6271\u308f\u308c\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>proc.c\u306ewakeup1, wakeup\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Wake up all processes sleeping on chan.\r\n\/\/ The ptable lock must be held.\r\nstatic void\r\nwakeup1(void *chan)\r\n{\r\n  struct proc *p;\r\n\r\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++)\r\n    if(p-&gt;state == SLEEPING &amp;&amp; p-&gt;chan == chan)\r\n      p-&gt;state = RUNNABLE;\r\n}\r\n\r\n\/\/ Wake up all processes sleeping on chan.\r\nvoid\r\nwakeup(void *chan)\r\n{\r\n  acquire(&amp;ptable.lock);\r\n  wakeup1(chan);\r\n  release(&amp;ptable.lock);\r\n}<\/pre>\n<p>wakeup\u306f\u5e38\u306b\u3001wakeup\u306e\u6761\u4ef6\u304c\u306a\u3093\u3067\u3042\u308c\u305d\u306e\u76e3\u8996\u3092\u59a8\u3052\u308b\u3088\u3046\u306a\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u308b\u9593\u3001\u547c\u3070\u308c\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002\uff08\u8b0e\uff09<br \/>\n\u524d\u7bc0\u306e\u4f8b\u3067\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u30ed\u30c3\u30af\u306b\u3042\u305f\u308b\u306e\u306fq-&gt;lock\u3067\u3042\u308b\u3002<br \/>\n\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u306a\u305c\u8d77\u304d\u640d\u306d\u305f\u304f\u306a\u3044\u304b\u3001\u305d\u306e\u305f\u3081\u306e\u5b8c\u5168\u306a\u6839\u62e0\u306f\u3001\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u307e\u3067\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u524d\u304b\u3089\u5e38\u306b\u3001\u6761\u4ef6\u4e0a\u3082\u3057\u304f\u306fptable.lock\u3082\u3057\u304f\u306f\u4e21\u65b9\u306e\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3059\u308b\u3002\uff08\u8b0e\uff09<br \/>\n\u305d\u308c\u3089\u306e\u30ed\u30c3\u30af\u306e\u4e21\u65b9\u3092\u4fdd\u6301\u3057\u3066\u308b\u9593\u3001wakeup\u304c\u5b9f\u884c\u3057\u3066\u304b\u3089\u3001wakeup\u306f\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u304c\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u524d\u3001\u3082\u3057\u304f\u306f\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u305d\u308c\u81ea\u8eab\u304c\u30b9\u30ea\u30fc\u30d7\u3057\u305f\u5f8c\u306b\u5b9f\u884c\u3059\u3079\u304d\u3067\u3042\u308b\u3002\uff08\u8b0e\uff09<\/p>\n<p>\u3068\u304d\u304a\u308a\u3001\u8907\u6570\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u540c\u3058\u30c1\u30e3\u30f3\u30cd\u30eb\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u5834\u5408\u304c\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u30012\u3064\u4ee5\u4e0a\u306e\u30d7\u30ed\u30bb\u30b9\u304c1\u3064\u306e\u30d1\u30a4\u30d7\u304b\u3089\u8aad\u307f\u8fbc\u3080\u5834\u5408\u3067\u3042\u308b\u3002<br \/>\n1\u56de\u306ewakeup\u306e\u547c\u3073\u51fa\u3057\u306f\u3001\u305d\u308c\u3089\u3059\u3079\u3066\u3092\u8d77\u3053\u3059\u3060\u308d\u3046\u3002<br \/>\n\u305d\u308c\u3089\u306e\u3072\u3068\u3064\u306f\u3001\u6700\u521d\u306b\u5b9f\u884c\u3055\u308csleep\u3092\u547c\u3073\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3057\u3001\u305d\u3057\u3066\uff08\u30d1\u30a4\u30d7\u306e\u4f8b\u3060\u3068\uff09\u30d1\u30a4\u30d7\u306b\u66f8\u304d\u3053\u307e\u308c\u305f\u4f55\u3089\u304b\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u3060\u308d\u3046\u3002<br \/>\n\u305d\u306e\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u8d77\u3053\u3055\u308c\u305f\u306b\u3082\u95a2\u308f\u3089\u305a\u3001\u8aad\u307f\u8fbc\u3080\u3079\u304d\u30c7\u30fc\u30bf\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u6c17\u3065\u304f\u3060\u308d\u3046\u3002<br \/>\n\u305d\u3046\u3044\u3063\u305f\u30d7\u30ed\u30bb\u30b9\u306e\u8996\u70b9\u304b\u3089\u898b\u308b\u3068\u3001\u305d\u306ewakeup\u306f&#8221;\u898b\u305b\u304b\u3051&#8221;\u3067\u3042\u308a\u3001\u305d\u3046\u3044\u3063\u305f\u30d7\u30ed\u30bb\u30b9\u306f\u307e\u305f\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u3053\u3093\u306a\u7406\u7531\u3067\u3001sleep\u306f\u5e38\u306b\u3001\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u547c\u3070\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<p>sleep\u3068wakeup\u306e\u547c\u3073\u51fa\u3057\u5074\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u3068\u3057\u3066\u4f55\u3089\u304b\u306e\u5171\u901a\u306e\u4fbf\u5229\u306a\u756a\u53f7\u3092\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u7fd2\u6163\u4e0a\u3001xv6\u306f\u3001\u30c7\u30a3\u30b9\u30af\u30d0\u30c3\u30d5\u30a1\u306e\u3088\u3046\u306a\u3001\u5f85\u6a5f\u306b\u95a2\u9023\u3057\u305f\u30ab\u30fc\u30cd\u30eb\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u3057\u3070\u3057\u3070\u4f7f\u3046\u3002<br \/>\n\u3082\u3057sleep\/wakeup\u30922\u7b87\u6240\u3067\u4f7f\u3063\u3066\u3066\u3001\u5076\u7136\u306b\u3082\u540c\u3058\u30c1\u30e3\u30f3\u30cd\u30eb\u3092\u9078\u629e\u3057\u3066\u3057\u307e\u3063\u3066\u3082\u3001\u5bb3\u306f\u306a\u3044\u3002<br \/>\n\u898b\u305b\u304b\u3051\u306ewakeup\u306b\u906d\u9047\u3059\u308b\u3060\u308d\u3046\u304c\u3001\u4e0a\u8a18\u3067\u8aac\u660e\u3057\u305f\u3088\u3046\u306a\u30eb\u30fc\u30d7\u306f\u3001\u3053\u306e\u554f\u984c\u306b\u8010\u6027\u304c\u3042\u308b\u3002<br \/>\nsleep\/wakeup\u306e\u9b45\u529b\u306e\u591a\u304f\u306f\u3001\u4e21\u65b9\u5171\u8efd\u304f\uff08\u30b9\u30ea\u30fc\u30d7\u30c1\u30e3\u30f3\u30cd\u30eb\u3068\u3057\u3066\u50cd\u304f\u3088\u3046\u306a\u7279\u5225\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u4f5c\u308b\u5fc5\u8981\u304c\u306a\u3044\uff09\u3001\u9593\u63a5\u7684\u306a\u30ec\u30a4\u30e4\u30fc\uff08\u547c\u3073\u51fa\u3057\u5074\u306fsleep\/wakeup\u304c\u76f8\u4e92\u306b\u5f71\u97ff\u3057\u3042\u3046\u8a73\u7d30\u306a\u65b9\u6cd5\u3092\u77e5\u308b\u5fc5\u8981\u304c\u306a\u3044\uff09\u3092\u63d0\u4f9b\u3059\u308b\u3068\u3053\u308d\u306b\u3042\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>sleep\u3068wakeup\u306exv6\u306b\u304a\u3051\u308b\u5b9f\u88c5\u306e\u8aac\u660e\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3061\u3087\u3063\u3068\u539f\u6587\u306e\u8a00\u3044\u56de\u3057\u304c\u96e3\u3057\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u9014\u4e2d\u304b\u306a\u308a\u8b0e\u306a\u6bb5\u843d\u3082\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3076\u3063\u3061\u3083\u3051\u4eca\u56de\u306f\u6587\u7ae0\u8aad\u307e\u306a\u304f\u3066\u3082\u3001\u30bd\u30fc\u30b9\u3068\u524d\u7bc0\u306e\u5185\u5bb9\u3092\u7406\u89e3\u3067\u304d\u3066\u308c\u3070\u554f\u984c\u306a\u3044\u3088\u3046\u306a\u6c17\u304c\u3057\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e58\u301c59\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>xv6\u306b\u304a\u3051\u308bsleep\u3068wakeup\u306e\u5b9f\u88c5\u306b\u3064\u3044\u3066\u898b\u3066\u307f\u3088\u3046\u3002<br \/>\nsleep\u306b\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u3092SLEEPING\u3068\u3057\u3066\u5370\u3092\u3064\u3051\u3055\u305b\u3001\u305d\u3057\u3066sched\u3092\u547c\u3073\u305d\u306e\u30d7\u30ed\u30bb\u30c3\u30b5\u3092\u89e3\u653e\u3057\u3001wakeup\u306f\u4e0e\u3048\u3089\u308c\u305f\u30dd\u30a4\u30f3\u30bf\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u3092\u63a2\u3057\u3001\u305d\u308c\u3092RUNNABLE\u3068\u3057\u3066\u5370\u3092\u3064\u3051\u308b\u3001\u3068\u3044\u3046\u306e\u304c\u57fa\u672c\u7684\u306a\u30a2\u30a4\u30c7\u30a2\u3067\u3042\u308b\u3002<\/p>\n<p>sleep\u306f\u3044\u304f\u3064\u304b\u306e\u6761\u4ef6\u30c1\u30a7\u30c3\u30af\u304b\u3089\u59cb\u307e\u308b\u3002<br \/>\n\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u3042\u308a\uff08proc == 0 \u3060\u3068panic\uff09\u3001\u30ed\u30c3\u30af\u3092\u6e21\u3055\u308c\u3066\u306a\u3051\u308c\u3070\uff08lk == 0 \u3067\u3082panic\uff09\u306a\u3089\u306a\u3044\u3002<br \/>\n\u30c1\u30a7\u30c3\u30af\u304c\u7d42\u308f\u308b\u3068\u307e\u305a\u3001ptable.lock\u3092\u7372\u5f97\u3059\u308b\u3002<br \/>\n\u305d\u306e\u307e\u307e\u3067\u306f\u3001ptable.lock\u3068lk\u306e\u4e21\u65b9\u3092\u4fdd\u6301\u3057\u305f\u307e\u307e\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u3057\u307e\u3046\u3002<br \/>\nlk\u306e\u4fdd\u6301\u306f\u3001\uff08\u4f8b\u3048\u3070recv\u306e\u3088\u3046\u306a\uff09\u547c\u3073\u51fa\u3057\u5074\u3067\u5fc5\u8981\u3068\u3055\u308c\u308b\u3002<br \/>\nlk\u3092\u4fdd\u6301\u3057\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u306f\u3001wakeup(chan)\u3092\u547c\u3073\u306f\u3058\u3081\u308c\u308b\u30d7\u30ed\u30bb\u30b9\uff08\u4f8b\u3048\u3070send\u3092\u5b9f\u884c\u3057\u3066\u308b\u30d7\u30ed\u30bb\u30b9\uff09\u304c\u4ed6\u306b\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u3002<br \/>\n\u3053\u306e\u6642\u70b9\u3067sleep\u306fptable.lock\u3092\u4fdd\u6301\u3057\u3001lk\u3092\u5b89\u5168\u306b\u89e3\u653e\u51fa\u6765\u308b\u3088\u3046\u306b\u306a\u308b\u3002<br \/>\n\u4ed6\u306e\u3044\u304f\u3064\u304b\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u3001wakeup(chan)\u3092\u547c\u3073\u51fa\u3059\u304b\u3082\u3057\u308c\u306a\u3044\u304c\u3001wakeup\u306fptable.lock\u304c\u7372\u5f97\u53ef\u80fd\u306b\u306a\u308b\u307e\u3067\u5b9f\u884c\u3055\u308c\u306a\u3044\u306e\u3067\u3001sleep\u304c\u30d7\u30ed\u30bb\u30b9\u3092\u78ba\u5b9f\u306b\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u307e\u3067\u5f85\u305f\u306a\u3051\u308c\u3070\u306a\u3089\u305a\u3001\u8d77\u304d\u640d\u306a\u3044\u306e\u554f\u984c\uff08\u524d\u7bc0\u53c2\u7167\uff09\u306f\u8d77\u304d\u306a\u3044\u3002<\/p>\n<p>proc.c\u306esleep\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Atomically release lock and sleep on chan.\r\n\/\/ Reacquires lock when awakened.\r\nvoid\r\nsleep(void *chan, struct spinlock *lk)\r\n{\r\n  if(proc == 0)\r\n    panic(&quot;sleep&quot;);\r\n\r\n  if(lk == 0)\r\n    panic(&quot;sleep without lk&quot;);\r\n\r\n  \/\/ Must acquire ptable.lock in order to\r\n  \/\/ change p-&gt;state and then call sched.\r\n  \/\/ Once we hold ptable.lock, we can be\r\n  \/\/ guaranteed that we won't miss any wakeup\r\n  \/\/ (wakeup runs with ptable.lock locked),\r\n  \/\/ so it's okay to release lk.\r\n  if(lk != &amp;ptable.lock){  \/\/DOC: sleeplock0\r\n    acquire(&amp;ptable.lock);  \/\/DOC: sleeplock1\r\n    release(lk);\r\n  }\r\n\r\n  \/\/ Go to sleep.\r\n  proc-&gt;chan = chan;\r\n  proc-&gt;state = SLEEPING;\r\n  sched();\r\n\r\n  \/\/ Tidy up.\r\n  proc-&gt;chan = 0;\r\n\r\n  \/\/ Reacquire original lock.\r\n  if(lk != &amp;ptable.lock){  \/\/DOC: sleeplock2\r\n    release(&amp;ptable.lock);\r\n    acquire(lk);\r\n  }\r\n}<\/pre>\n<p>\u5fae\u5999\u306b\u8907\u96d1\u306a\u5834\u5408\u304c\u3042\u308b\u3002<br \/>\n\u3082\u3057lk\u304c&amp;ptable.lock\u3068\u7b49\u3057\u3044\u5834\u5408\u3001sleep\u306f\u305d\u308c\u3092&amp;ptable.lock\u3068\u3057\u3066\u7372\u5f97\u3057\u3001lk\u3068\u3057\u3066\u89e3\u653e\u3057\u3088\u3046\u3068\u3057\u3066\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u9665\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3053\u306e\u5834\u5408\u3001sleep\u306facquire\u3068relese\u304c\u304a\u4e92\u3044\u306b\u76f8\u6bba\u3055\u308c\u308b\u3053\u3068\u3092\u8003\u616e\u3057\u3001\u305d\u308c\u3089\u3092\u5b8c\u5168\u306b\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u3002<\/p>\n<p>\u3053\u306e\u6642\u70b9\u3067\u3001sleep\u306fptable.lock\u306e\u307f\u3092\u4fdd\u6301\u3057\u3001\u30b9\u30ea\u30fc\u30d7\u30c1\u30e3\u30f3\u30cd\u30eb\u8a18\u9332\u3057\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092\u5909\u66f4\u3057\u3001sched\u3092\u547c\u3076\u3053\u3068\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u3092\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u4e8b\u304c\u3067\u304d\u308b\u3002<\/p>\n<p>\u5f8c\u306e\u3069\u3053\u304b\u306e\u6bb5\u968e\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u306fwakeup(chan)\u3092\u547c\u3076\u3060\u308d\u3046\u3002<br \/>\nwakeup\u306fptable.lock\u3092\u7372\u5f97\u3057\u3001\u5b9f\u969b\u306e\u51e6\u7406\u3092\u884c\u3046wakeup1\u3092\u547c\u3076\u3002<br \/>\nwakeup1\u306f\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092\u64cd\u4f5c\u3059\u308b\u3057\u3001\u524d\u7bc0\u3067\u898b\u305f\u3088\u3046\u306b\u3001ptable.lock\u306fsleep\u3068wakeup\u304c\u304a\u4e92\u3044\u306b\u6a5f\u4f1a\u3092\u9003\u3055\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3092\u78ba\u5b9f\u306b\u3059\u308b\u306e\u3067\u3001wakeup\u304cptable.lock\u3092\u4fdd\u6301\u3059\u308b\u4e8b\u306f\u3068\u3066\u3082\u91cd\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3068\u304d\u304a\u308a\u3001\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306fptable.lock\u3092\u65e2\u306b\u4fdd\u6301\u3057\u3066\u3044\u308b\u3068\u304d\u306bwakeup\u3092\u5b9f\u884c\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u306e\u3067\u3001wakeup1\u3068\u3057\u3066\u5206\u96e2\u3055\u308c\u3066\u3044\u308b\u3002<br \/>\n\u76f4\u63a5wakeup1\u3092\u547c\u3076\u4f8b\u306f\u5f8c\u3067\u8aac\u660e\u3059\u308b\u3002<br \/>\nwakeup1\u306f\u3001\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u8d70\u67fb\u3059\u308b\u3002<br \/>\nchan\u304c\u5408\u81f4\u3057\u72b6\u614b\u304cSLEEPING\u306a\u30d7\u30ed\u30bb\u30b9\u3092\u898b\u3064\u3051\u305f\u3089\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092RUNNABLE\u306b\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u6b21\u306b\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u304c\u5b9f\u884c\u3055\u308c\u305f\u3068\u304d\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u5b9f\u884c\u6e96\u5099\u304c\u6574\u3063\u3066\u308b\u3068\u3057\u3066\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306b\u6271\u308f\u308c\u308b\u3060\u308d\u3046\u3002<\/p>\n<p>proc.c\u306ewakeup1, wakeup\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Wake up all processes sleeping on chan.\r\n\/\/ The ptable lock must be held.\r\nstatic void\r\nwakeup1(void *chan)\r\n{\r\n  struct proc *p;\r\n\r\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++)\r\n    if(p-&gt;state == SLEEPING &amp;&amp; p-&gt;chan == chan)\r\n      p-&gt;state = RUNNABLE;\r\n}\r\n\r\n\/\/ Wake up all processes sleeping on chan.\r\nvoid\r\nwakeup(void *chan)\r\n{\r\n  acquire(&amp;ptable.lock);\r\n  wakeup1(chan);\r\n  release(&amp;ptable.lock);\r\n}<\/pre>\n<p>wakeup\u306f\u5e38\u306b\u3001wakeup\u306e\u6761\u4ef6\u304c\u306a\u3093\u3067\u3042\u308c\u305d\u306e\u76e3\u8996\u3092\u59a8\u3052\u308b\u3088\u3046\u306a\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3057\u3066\u308b\u9593\u3001\u547c\u3070\u308c\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3002\uff08\u8b0e\uff09<br \/>\n\u524d\u7bc0\u306e\u4f8b\u3067\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u30ed\u30c3\u30af\u306b\u3042\u305f\u308b\u306e\u306fq-&gt;lock\u3067\u3042\u308b\u3002<br \/>\n\u30b9\u30ea\u30fc\u30d7\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u306a\u305c\u8d77\u304d\u640d\u306d\u305f\u304f\u306a\u3044\u304b\u3001\u305d\u306e\u305f\u3081\u306e\u5b8c\u5168\u306a\u6839\u62e0\u306f\u3001\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u307e\u3067\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u524d\u304b\u3089\u5e38\u306b\u3001\u6761\u4ef6\u4e0a\u3082\u3057\u304f\u306fptable.lock\u3082\u3057\u304f\u306f\u4e21\u65b9\u306e\u30ed\u30c3\u30af\u3092\u4fdd\u6301\u3059\u308b\u3002\uff08\u8b0e\uff09<br \/>\n\u305d\u308c\u3089\u306e\u30ed\u30c3\u30af\u306e\u4e21\u65b9\u3092\u4fdd\u6301\u3057\u3066\u308b\u9593\u3001wakeup\u304c\u5b9f\u884c\u3057\u3066\u304b\u3089\u3001wakeup\u306f\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u304c\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u524d\u3001\u3082\u3057\u304f\u306f\u30b9\u30ea\u30fc\u30d7\u3055\u305b\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3082\u306e\u305d\u308c\u81ea\u8eab\u304c\u30b9\u30ea\u30fc\u30d7\u3057\u305f\u5f8c\u306b\u5b9f\u884c\u3059\u3079\u304d\u3067\u3042\u308b\u3002\uff08\u8b0e\uff09<\/p>\n<p>\u3068\u304d\u304a\u308a\u3001\u8907\u6570\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u540c\u3058\u30c1\u30e3\u30f3\u30cd\u30eb\u4e0a\u3067\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u308b\u5834\u5408\u304c\u3042\u308b\u3002<br \/>\n\u4f8b\u3048\u3070\u30012\u3064\u4ee5\u4e0a\u306e\u30d7\u30ed\u30bb\u30b9\u304c1\u3064\u306e\u30d1\u30a4\u30d7\u304b\u3089\u8aad\u307f\u8fbc\u3080\u5834\u5408\u3067\u3042\u308b\u3002<br \/>\n1\u56de\u306ewakeup\u306e\u547c\u3073\u51fa\u3057\u306f\u3001\u305d\u308c\u3089\u3059\u3079\u3066\u3092\u8d77\u3053\u3059\u3060\u308d\u3046\u3002<br \/>\n\u305d\u308c\u3089\u306e\u3072\u3068\u3064\u306f\u3001\u6700\u521d\u306b\u5b9f\u884c\u3055\u308csleep\u3092\u547c\u3073\u30ed\u30c3\u30af\u3092\u7372\u5f97\u3057\u3001\u305d\u3057\u3066\uff08\u30d1\u30a4\u30d7\u306e\u4f8b\u3060\u3068\uff09\u30d1\u30a4\u30d7\u306b\u66f8\u304d\u3053\u307e\u308c\u305f\u4f55\u3089\u304b\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u3060\u308d\u3046\u3002<br \/>\n\u305d\u306e\u4ed6\u306e\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u8d77\u3053\u3055\u308c\u305f\u306b\u3082\u95a2\u308f\u3089\u305a\u3001\u8aad\u307f\u8fbc\u3080\u3079\u304d\u30c7\u30fc\u30bf\u304c\u306a\u3044\u3068\u3044\u3046\u3053\u3068\u306b\u6c17\u3065\u304f\u3060\u308d\u3046\u3002<br \/>\n\u305d\u3046\u3044\u3063\u305f\u30d7\u30ed\u30bb\u30b9\u306e\u8996\u70b9\u304b\u3089\u898b\u308b\u3068\u3001\u305d\u306ewakeup\u306f&#8221;\u898b\u305b\u304b\u3051&#8221;\u3067\u3042\u308a\u3001\u305d\u3046\u3044\u3063\u305f\u30d7\u30ed\u30bb\u30b9\u306f\u307e\u305f\u30b9\u30ea\u30fc\u30d7\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002<br \/>\n\u3053\u3093\u306a\u7406\u7531\u3067\u3001sleep\u306f\u5e38\u306b\u3001\u6761\u4ef6\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u30eb\u30fc\u30d7\u306e\u4e2d\u3067\u547c\u3070\u308c\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u3044\u308b\u3002<\/p>\n<p>sleep\u3068wakeup\u306e\u547c\u3073\u51fa\u3057\u5074\u306f\u3001\u30c1\u30e3\u30f3\u30cd\u30eb\u3068\u3057\u3066\u4f55\u3089\u304b\u306e\u5171\u901a\u306e\u4fbf\u5229\u306a\u756a\u53f7\u3092\u4f7f\u3046\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u7fd2\u6163\u4e0a\u3001xv6\u306f\u3001\u30c7\u30a3\u30b9\u30af\u30d0\u30c3\u30d5\u30a1\u306e\u3088\u3046\u306a\u3001\u5f85\u6a5f\u306b\u95a2\u9023\u3057\u305f\u30ab\u30fc\u30cd\u30eb\u306e\u30c7\u30fc\u30bf\u69cb\u9020\u306e\u30a2\u30c9\u30ec\u30b9\u3092\u3057\u3070\u3057\u3070\u4f7f\u3046\u3002<br \/>\n\u3082\u3057sleep\/wakeup\u30922\u7b87\u6240\u3067\u4f7f\u3063\u3066\u3066\u3001\u5076\u7136\u306b\u3082\u540c\u3058\u30c1\u30e3\u30f3\u30cd\u30eb\u3092\u9078\u629e\u3057\u3066\u3057\u307e\u3063\u3066\u3082\u3001\u5bb3\u306f\u306a\u3044\u3002<br \/>\n\u898b\u305b\u304b\u3051\u306ewakeup\u306b\u906d\u9047\u3059\u308b\u3060\u308d\u3046\u304c\u3001\u4e0a\u8a18\u3067\u8aac\u660e\u3057\u305f\u3088\u3046\u306a\u30eb\u30fc\u30d7\u306f\u3001\u3053\u306e\u554f\u984c\u306b\u8010\u6027\u304c\u3042\u308b\u3002<br \/>\nsleep\/wakeup\u306e\u9b45\u529b\u306e\u591a\u304f\u306f\u3001\u4e21\u65b9\u5171\u8efd\u304f\uff08\u30b9\u30ea\u30fc\u30d7\u30c1\u30e3\u30f3\u30cd\u30eb\u3068\u3057\u3066\u50cd\u304f\u3088\u3046\u306a\u7279\u5225\u306a\u30c7\u30fc\u30bf\u69cb\u9020\u3092\u4f5c\u308b\u5fc5\u8981\u304c\u306a\u3044\uff09\u3001\u9593\u63a5\u7684\u306a\u30ec\u30a4\u30e4\u30fc\uff08\u547c\u3073\u51fa\u3057\u5074\u306fsleep\/wakeup\u304c\u76f8\u4e92\u306b\u5f71\u97ff\u3057\u3042\u3046\u8a73\u7d30\u306a\u65b9\u6cd5\u3092\u77e5\u308b\u5fc5\u8981\u304c\u306a\u3044\uff09\u3092\u63d0\u4f9b\u3059\u308b\u3068\u3053\u308d\u306b\u3042\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>sleep\u3068wakeup\u306exv6\u306b\u304a\u3051\u308b\u5b9f\u88c5\u306e\u8aac\u660e\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3061\u3087\u3063\u3068\u539f\u6587\u306e\u8a00\u3044\u56de\u3057\u304c\u96e3\u3057\u304b\u3063\u305f\u3067\u3059\u3002<br \/>\n\u9014\u4e2d\u304b\u306a\u308a\u8b0e\u306a\u6bb5\u843d\u3082\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3076\u3063\u3061\u3083\u3051\u4eca\u56de\u306f\u6587\u7ae0\u8aad\u307e\u306a\u304f\u3066\u3082\u3001\u30bd\u30fc\u30b9\u3068\u524d\u7bc0\u306e\u5185\u5bb9\u3092\u7406\u89e3\u3067\u304d\u3066\u308c\u3070\u554f\u984c\u306a\u3044\u3088\u3046\u306a\u6c17\u304c\u3057\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-1561","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\/1561","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=1561"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1561\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}