{"id":1567,"date":"2012-03-26T00:53:07","date_gmt":"2012-03-25T15:53:07","guid":{"rendered":"http:\/\/peta.okechan.net\/blog\/?p=1567"},"modified":"2012-03-26T00:53:07","modified_gmt":"2012-03-25T15:53:07","slug":"xv6-50-chapter-4-scheduling-code-wait-and-exit","status":"publish","type":"post","link":"https:\/\/peta.okechan.net\/blog\/archives\/1567","title":{"rendered":"[xv6 #50] Chapter 4 &#8211; Scheduling &#8211; Code: Wait and exit"},"content":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e60\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>sleep\u3068wakeup\u306f\u3001\u6761\u4ef6\u304c\u6574\u3046\u307e\u3067\u5f85\u3064\u4e8b\u304c\u5fc5\u8981\u306a\u3001\u69d8\u3005\u306a\u72b6\u6cc1\u3067\u4f7f\u7528\u3055\u308c\u5f97\u308b\u3002<br \/>\n\u7b2c0\u7ae0\u3067\u898b\u305f\u3088\u3046\u306b\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\u305f\u3081\u306bwait\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\nxv6\u3067\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\uff08exit\uff09\u3057\u305f\u3068\u304d\u3001\u3059\u3050\u306b\u306f\u7d42\u4e86\uff08die\uff09\u3057\u306a\u3044\u3002<br \/>\n\u305d\u306e\u4ee3\u308f\u308a\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u51fa\u6765\u308b\u72b6\u614b\u306b\u306a\u308b\u305f\u3081\u306bwait\u3092\u547c\u3076\u307e\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092ZOMBIE\u306b\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u89aa\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3001\u518d\u5229\u7528\u306e\u305f\u3081\u306bproc\u69cb\u9020\u4f53\u3092\u6e96\u5099\u3059\u308b\u8cac\u4efb\u304c\u3042\u308b\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30d7\u30ed\u30bb\u30b9\u306eproc\u69cb\u9020\u4f53\u306f\u3001p-&gt;parent\u3068\u3057\u3066\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u6307\u3059\u30dd\u30a4\u30f3\u30bf\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u5b50\u30d7\u30ed\u30bb\u30b9\u3088\u308a\u5148\u306b\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5834\u5408\u3001\u4e00\u756a\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u3042\u308binit\u304c\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u53d7\u3051\u7d99\u304e\u3001\u305d\u306e\u7d42\u4e86\u3092\u5f85\u3064\u3002<br \/>\n\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5f8c\u306b\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u3059\u308b\u4e8b\u3092\u78ba\u5b9f\u306b\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u306eproc\u69cb\u9020\u4f53\u306f\u3001ptable.lock\u306b\u3088\u3063\u3066\u4fdd\u8b77\u3055\u308c\u308b\u3002<\/p>\n<p>wait\u306f\u3001ptable.lock\u306e\u7372\u5f97\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u635c\u3059\u305f\u3081\u306b\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u8d70\u67fb\u3059\u308b\u3002<br \/>\n\u3082\u3057wait\u304c\u3001\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u307e\u3060\u7d42\u4e86\u3057\u3066\u306a\u3044\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u6301\u3064\u3068\u3044\u3046\u3053\u3068\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\u305f\u3081\u306bsleep\u3092\u547c\u3073\u3001\u305d\u308c\u3092\u7e70\u308a\u8fd4\u3059\u3002<br \/>\n\u305d\u3057\u3066\u3001sleep\u4e2d\u306bptable.lock\u304c\u89e3\u653e\u3055\u308c\u308b\u3002<br \/>\n\u305d\u308c\u306f\u4ee5\u524d\u306e\u7bc0\u3067\u898b\u305f\u7279\u5225\u306a\u5834\u5408\u306b\u76f8\u5f53\u3059\u308b\u3002\uff08\u7279\u5225 = \u540c\u3058\u30ed\u30c3\u30af\u306e\u7372\u5f97\u3068\u89e3\u653e\u3092\u5225\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u884c\u3046\u30d1\u30bf\u30fc\u30f3\u3068\u3044\u3046\u3053\u3068\uff1f\uff09<\/p>\n<p>proc.c\u306ewait\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Wait for a child process to exit and return its pid.\r\n\/\/ Return -1 if this process has no children.\r\nint\r\nwait(void)\r\n{\r\n  struct proc *p;\r\n  int havekids, pid;\r\n\r\n  acquire(&amp;ptable.lock);\r\n  for(;;){\r\n    \/\/ Scan through table looking for zombie children.\r\n    havekids = 0;\r\n    for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++){\r\n      if(p-&gt;parent != proc)\r\n        continue;\r\n      havekids = 1;\r\n      if(p-&gt;state == ZOMBIE){\r\n        \/\/ Found one.\r\n        pid = p-&gt;pid;\r\n        kfree(p-&gt;kstack);\r\n        p-&gt;kstack = 0;\r\n        freevm(p-&gt;pgdir);\r\n        p-&gt;state = UNUSED;\r\n        p-&gt;pid = 0;\r\n        p-&gt;parent = 0;\r\n        p-&gt;name&#x5B;0] = 0;\r\n        p-&gt;killed = 0;\r\n        release(&amp;ptable.lock);\r\n        return pid;\r\n      }\r\n    }\r\n\r\n    \/\/ No point waiting if we don't have any children.\r\n    if(!havekids || proc-&gt;killed){\r\n      release(&amp;ptable.lock);\r\n      return -1;\r\n    }\r\n\r\n    \/\/ Wait for children to exit.  (See wakeup1 call in proc_exit.)\r\n    sleep(proc, &amp;ptable.lock);  \/\/DOC: wait-sleep\r\n  }\r\n}<\/pre>\n<p>exit\u306f\u3001ptable.lock\u3092\u7372\u5f97\u3057\u3001\u305d\u308c\u304b\u3089\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u8d77\u3053\u3059\u3002<br \/>\nexit\u95a2\u6570\u304c\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u3092ZOMBIE\u3068\u3057\u3066\u30de\u30fc\u30af\u3059\u308b\u524d\u306e\u6bb5\u968e\u3067\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u8d77\u3053\u3059\u306e\u306f\u6642\u671f\u5c1a\u65e9\u306b\u898b\u3048\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3057\u304b\u3057\u3053\u308c\u306f\u5b89\u5168\u3067\u3042\u308b\u3002<br \/>\n\u89aa\u30d7\u30ed\u30bb\u30b9\u306f\u305d\u306e\u6642\u70b9\u3067RUNNABLE\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u3066\u308b\u3068\u306f\u8a00\u3048\u3001exit\u304c\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306b\u51e6\u7406\u3092\u79fb\u3059\u305f\u3081\u306bsched\u3092\u547c\u3076\u4e8b\u306b\u3088\u3063\u3066ptable.lock\u3092\u89e3\u653e\u3059\u308b\u307e\u3067\u3001wait\u306e\u4e2d\u306e\u30eb\u30fc\u30d7\u306f\u5b9f\u884c\u3067\u304d\u306a\u3044\u306e\u3067\u3001ZOMBIE\u72b6\u614b\u306b\u306a\u308b\u524d\u306b\u7d42\u4e86\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u3092wait\u3067\u64cd\u4f5c\u3057\u3066\u3057\u307e\u3046\u3088\u3046\u306a\u4e8b\u614b\u306f\u8d77\u304d\u306a\u3044\u3002<br \/>\nexit\u306f\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u3092\u547c\u3076\u524d\u306b\u3001\u7d42\u4e86\u4e2d\u306e\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u3059\u3079\u3066\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\u3092initproc\u3078\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u6700\u5f8c\u306bexit\u306fCPU\u3092\u624b\u653e\u3059\u305f\u3081\u306bsched\u3092\u547c\u3076\u3002<\/p>\n<p>proc.c\u306eexit\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Exit the current process.  Does not return.\r\n\/\/ An exited process remains in the zombie state\r\n\/\/ until its parent calls wait() to find out it exited.\r\nvoid\r\nexit(void)\r\n{\r\n  struct proc *p;\r\n  int fd;\r\n\r\n  if(proc == initproc)\r\n    panic(&quot;init exiting&quot;);\r\n\r\n  \/\/ Close all open files.\r\n  for(fd = 0; fd &lt; NOFILE; fd++){\r\n    if(proc-&gt;ofile&#x5B;fd]){\r\n      fileclose(proc-&gt;ofile&#x5B;fd]);\r\n      proc-&gt;ofile&#x5B;fd] = 0;\r\n    }\r\n  }\r\n\r\n  iput(proc-&gt;cwd);\r\n  proc-&gt;cwd = 0;\r\n\r\n  acquire(&amp;ptable.lock);\r\n\r\n  \/\/ Parent might be sleeping in wait().\r\n  wakeup1(proc-&gt;parent);\r\n\r\n  \/\/ Pass abandoned children to init.\r\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++){\r\n    if(p-&gt;parent == proc){\r\n      p-&gt;parent = initproc;\r\n      if(p-&gt;state == ZOMBIE)\r\n        wakeup1(initproc);\r\n    }\r\n  }\r\n\r\n  \/\/ Jump into the scheduler, never to return.\r\n  proc-&gt;state = ZOMBIE;\r\n  sched();\r\n  panic(&quot;zombie exit&quot;);\r\n}<\/pre>\n<p>\u305d\u3057\u305f\u3089\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306f\u3001\u7d42\u4e86\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\uff08wait\u306esleep\u306e\u547c\u3073\u51fa\u3057\u306b\u3088\u3063\u3066\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u3044\u308b\uff09\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\nsleep\u306e\u547c\u3073\u51fa\u3057\u304b\u3089\u623b\u308b\u3068\u304d\u3001ptable.lock\u3092\u4fdd\u6301\u3057\u305f\u72b6\u614b\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001wait\u306f\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u518d\u635c\u67fb\u3057\u3001state == ZOMBIE\u3068\u306a\u3063\u3066\u3044\u308b\u7d42\u4e86\u3057\u305f\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u635c\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306epid\u3092\u8a18\u9332\u3057\u3001\u305d\u3057\u3066\u305d\u306eproc\u69cb\u9020\u4f53\u3092\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u3057\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u3002\uff08\u3053\u3053\u306fwait\u95a2\u6570\u306eif(p\u2212&gt;state == ZOMBIE){\u306e\u4e2d\u306e\u8a71\uff09<\/p>\n<p>\u3053\u308c\u3067\u5b50\u30d7\u30ed\u30bb\u30b9\u306fexit\u306b\u304a\u3051\u308b\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u51e6\u7406\u306e\u5927\u90e8\u5206\u3092\u5b8c\u4e86\u3057\u305f\u3053\u3068\u306b\u306a\u308b\u304c\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;kstack\u3084p-&gt;pgdir\u306f\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u89e3\u653e\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u306e\u304c\u91cd\u8981\u306a\u70b9\u3067\u3042\u308b\u3002<br \/>\n\u5b50\u30d7\u30ed\u30bb\u30b9\u304cexit\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u30b9\u30bf\u30c3\u30af\u306fp-&gt;kstack\u3068\u3057\u3066\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u306b\u7f6e\u304b\u308c\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u3066\u4f7f\u3046\u3002<br \/>\n\u305d\u308c\u3089\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5f8c\uff08exit\u5185\u3067sched\u7d4c\u7531\u3067swtch\u304c\u547c\u3070\u308c\u305f\u5f8c\uff09\u3067\u306a\u3044\u3068\u89e3\u653e\u3067\u304d\u306a\u3044\u3002<br \/>\n\u3053\u308c\u306f\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306e\u624b\u7d9a\u304d\u304c\u3001sched\u3068\u547c\u3070\u308c\u308b\u30b9\u30ec\u30c3\u30c9\u306e\u30b9\u30bf\u30c3\u30af\u4e0a\u3067\u306f\u306a\u304f\u3001\u305d\u308c\u81ea\u8eab\u306e\u30b9\u30bf\u30c3\u30af\u4e0a\u3067\u52d5\u4f5c\u3059\u308b\u3072\u3068\u3064\u306e\u7406\u7531\u3067\u3042\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>wait\u3068exit\u306e\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u3067\u3059\u304c\u3001\u3061\u3083\u3093\u3068\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u306f\u30d7\u30ed\u30bb\u30b9\u306e\u4ed5\u7d44\u307f\u306b\u3064\u3044\u3066\u306e\u7dcf\u5408\u7684\u306a\u77e5\u8b58\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u81ea\u5206\u306e\u77e5\u8b58\u306e\u4e00\u3064\u4e0a\u3050\u3089\u3044\u306e\u4e8b\u306a\u3089\u307e\u3060\u3042\u30fc\u3060\u3053\u30fc\u3060\u8a00\u3048\u308b\u4f59\u5730\u304c\u3042\u308b\u3093\u3067\u3059\u304c\u3001\u3053\u306e\u7bc0\u306f\u4e8c\u30fb\u4e09\u6bb5\u3050\u3089\u3044\u4e0a\u306a\u611f\u3058\u3067\u3001\u306a\u3093\u3068\u304b\u7406\u89e3\u306f\u51fa\u6765\u308b\u3051\u3069\u53d7\u3051\u8eab\u3067\u300c\u3078\u3047\u305d\u3046\u306a\u3093\u3060\u300d\u3068\u601d\u3046\u3053\u3068\u3057\u304b\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u89aa\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;parent\u306f\u3069\u3046\u306a\u3063\u3066\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u3001\u305d\u308c\u306f\u30b7\u30a7\u30eb\u304b\u3089\u8d77\u52d5\u3055\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5834\u5408\u306f\u3001\u89aa\u306f\u30b7\u30a7\u30eb\u306b\u306a\u3063\u3066\u308b\u306f\u305a\u3067\u3059\u3002\uff08\u30b7\u30a7\u30eb\u306ffork\u3092\u4f7f\u3063\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u306e\u3067\uff09<br \/>\n\u3067\u306f\u30b7\u30a7\u30eb\u306f\u3069\u3046\u306a\u306e\u304b\u3068\u3044\u3046\u3068\u3001\u30bd\u30fc\u30b9\u3092\u5168\u90e8\u8aad\u3093\u3060\u308f\u3051\u3067\u306f\u306a\u3044\u306e\u3067\u3001\u9593\u306b\u4f55\u304b\u306f\u3055\u307e\u3063\u3066\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001initproc\u306e\u5b50\u306b\u306a\u3063\u3066\u308b\u306f\u305a\u3067\u3059\u3002<br \/>\ninitproc\u304c\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u6700\u7d42\u7684\u306a\u89aa\u306b\u306a\u308b\u306e\u3067\u3001exit\u95a2\u6570\u306finitproc\u3067\u5b9f\u884c\u3055\u308c\u308b\u3068panic\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u307e\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30c6\u30ad\u30b9\u30c8\u306e60\u30da\u30fc\u30b8<\/p>\n<h3>\u672c\u6587<\/h3>\n<p>sleep\u3068wakeup\u306f\u3001\u6761\u4ef6\u304c\u6574\u3046\u307e\u3067\u5f85\u3064\u4e8b\u304c\u5fc5\u8981\u306a\u3001\u69d8\u3005\u306a\u72b6\u6cc1\u3067\u4f7f\u7528\u3055\u308c\u5f97\u308b\u3002<br \/>\n\u7b2c0\u7ae0\u3067\u898b\u305f\u3088\u3046\u306b\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\u305f\u3081\u306bwait\u95a2\u6570\u3092\u547c\u3076\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\nxv6\u3067\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\uff08exit\uff09\u3057\u305f\u3068\u304d\u3001\u3059\u3050\u306b\u306f\u7d42\u4e86\uff08die\uff09\u3057\u306a\u3044\u3002<br \/>\n\u305d\u306e\u4ee3\u308f\u308a\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u51fa\u6765\u308b\u72b6\u614b\u306b\u306a\u308b\u305f\u3081\u306bwait\u3092\u547c\u3076\u307e\u3067\u3001\u30d7\u30ed\u30bb\u30b9\u306e\u72b6\u614b\u3092ZOMBIE\u306b\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u89aa\u30d7\u30ed\u30bb\u30b9\u306b\u306f\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3057\u3001\u518d\u5229\u7528\u306e\u305f\u3081\u306bproc\u69cb\u9020\u4f53\u3092\u6e96\u5099\u3059\u308b\u8cac\u4efb\u304c\u3042\u308b\u3002<br \/>\n\u305d\u308c\u305e\u308c\u306e\u30d7\u30ed\u30bb\u30b9\u306eproc\u69cb\u9020\u4f53\u306f\u3001p-&gt;parent\u3068\u3057\u3066\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u6307\u3059\u30dd\u30a4\u30f3\u30bf\u3092\u4fdd\u6301\u3059\u308b\u3002<br \/>\n\u5b50\u30d7\u30ed\u30bb\u30b9\u3088\u308a\u5148\u306b\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5834\u5408\u3001\u4e00\u756a\u6700\u521d\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u3042\u308binit\u304c\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u53d7\u3051\u7d99\u304e\u3001\u305d\u306e\u7d42\u4e86\u3092\u5f85\u3064\u3002<br \/>\n\u3053\u306e\u30b9\u30c6\u30c3\u30d7\u306f\u3001\u3044\u304f\u3064\u304b\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5f8c\u306b\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u3059\u308b\u4e8b\u3092\u78ba\u5b9f\u306b\u3059\u308b\u305f\u3081\u306b\u5fc5\u8981\u3067\u3042\u308b\u3002<br \/>\n\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u306eproc\u69cb\u9020\u4f53\u306f\u3001ptable.lock\u306b\u3088\u3063\u3066\u4fdd\u8b77\u3055\u308c\u308b\u3002<\/p>\n<p>wait\u306f\u3001ptable.lock\u306e\u7372\u5f97\u304b\u3089\u59cb\u3081\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u635c\u3059\u305f\u3081\u306b\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u8d70\u67fb\u3059\u308b\u3002<br \/>\n\u3082\u3057wait\u304c\u3001\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u304c\u307e\u3060\u7d42\u4e86\u3057\u3066\u306a\u3044\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u6301\u3064\u3068\u3044\u3046\u3053\u3068\u3092\u898b\u3064\u3051\u305f\u5834\u5408\u3001\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3059\u308b\u306e\u3092\u5f85\u3064\u305f\u3081\u306bsleep\u3092\u547c\u3073\u3001\u305d\u308c\u3092\u7e70\u308a\u8fd4\u3059\u3002<br \/>\n\u305d\u3057\u3066\u3001sleep\u4e2d\u306bptable.lock\u304c\u89e3\u653e\u3055\u308c\u308b\u3002<br \/>\n\u305d\u308c\u306f\u4ee5\u524d\u306e\u7bc0\u3067\u898b\u305f\u7279\u5225\u306a\u5834\u5408\u306b\u76f8\u5f53\u3059\u308b\u3002\uff08\u7279\u5225 = \u540c\u3058\u30ed\u30c3\u30af\u306e\u7372\u5f97\u3068\u89e3\u653e\u3092\u5225\u306e\u30d7\u30ed\u30bb\u30b9\u3067\u884c\u3046\u30d1\u30bf\u30fc\u30f3\u3068\u3044\u3046\u3053\u3068\uff1f\uff09<\/p>\n<p>proc.c\u306ewait\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Wait for a child process to exit and return its pid.\r\n\/\/ Return -1 if this process has no children.\r\nint\r\nwait(void)\r\n{\r\n  struct proc *p;\r\n  int havekids, pid;\r\n\r\n  acquire(&amp;ptable.lock);\r\n  for(;;){\r\n    \/\/ Scan through table looking for zombie children.\r\n    havekids = 0;\r\n    for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++){\r\n      if(p-&gt;parent != proc)\r\n        continue;\r\n      havekids = 1;\r\n      if(p-&gt;state == ZOMBIE){\r\n        \/\/ Found one.\r\n        pid = p-&gt;pid;\r\n        kfree(p-&gt;kstack);\r\n        p-&gt;kstack = 0;\r\n        freevm(p-&gt;pgdir);\r\n        p-&gt;state = UNUSED;\r\n        p-&gt;pid = 0;\r\n        p-&gt;parent = 0;\r\n        p-&gt;name&#x5B;0] = 0;\r\n        p-&gt;killed = 0;\r\n        release(&amp;ptable.lock);\r\n        return pid;\r\n      }\r\n    }\r\n\r\n    \/\/ No point waiting if we don't have any children.\r\n    if(!havekids || proc-&gt;killed){\r\n      release(&amp;ptable.lock);\r\n      return -1;\r\n    }\r\n\r\n    \/\/ Wait for children to exit.  (See wakeup1 call in proc_exit.)\r\n    sleep(proc, &amp;ptable.lock);  \/\/DOC: wait-sleep\r\n  }\r\n}<\/pre>\n<p>exit\u306f\u3001ptable.lock\u3092\u7372\u5f97\u3057\u3001\u305d\u308c\u304b\u3089\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u8d77\u3053\u3059\u3002<br \/>\nexit\u95a2\u6570\u304c\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u3092ZOMBIE\u3068\u3057\u3066\u30de\u30fc\u30af\u3059\u308b\u524d\u306e\u6bb5\u968e\u3067\u3001\u89aa\u30d7\u30ed\u30bb\u30b9\u3092\u8d77\u3053\u3059\u306e\u306f\u6642\u671f\u5c1a\u65e9\u306b\u898b\u3048\u308b\u3060\u308d\u3046\u3002<br \/>\n\u3057\u304b\u3057\u3053\u308c\u306f\u5b89\u5168\u3067\u3042\u308b\u3002<br \/>\n\u89aa\u30d7\u30ed\u30bb\u30b9\u306f\u305d\u306e\u6642\u70b9\u3067RUNNABLE\u3068\u3057\u3066\u30de\u30fc\u30af\u3055\u308c\u3066\u308b\u3068\u306f\u8a00\u3048\u3001exit\u304c\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306b\u51e6\u7406\u3092\u79fb\u3059\u305f\u3081\u306bsched\u3092\u547c\u3076\u4e8b\u306b\u3088\u3063\u3066ptable.lock\u3092\u89e3\u653e\u3059\u308b\u307e\u3067\u3001wait\u306e\u4e2d\u306e\u30eb\u30fc\u30d7\u306f\u5b9f\u884c\u3067\u304d\u306a\u3044\u306e\u3067\u3001ZOMBIE\u72b6\u614b\u306b\u306a\u308b\u524d\u306b\u7d42\u4e86\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u3092wait\u3067\u64cd\u4f5c\u3057\u3066\u3057\u307e\u3046\u3088\u3046\u306a\u4e8b\u614b\u306f\u8d77\u304d\u306a\u3044\u3002<br \/>\nexit\u306f\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u3092\u547c\u3076\u524d\u306b\u3001\u7d42\u4e86\u4e2d\u306e\u73fe\u5728\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306e\u3059\u3079\u3066\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\u3092initproc\u3078\u5909\u66f4\u3059\u308b\u3002<br \/>\n\u6700\u5f8c\u306bexit\u306fCPU\u3092\u624b\u653e\u3059\u305f\u3081\u306bsched\u3092\u547c\u3076\u3002<\/p>\n<p>proc.c\u306eexit\u95a2\u6570<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ Exit the current process.  Does not return.\r\n\/\/ An exited process remains in the zombie state\r\n\/\/ until its parent calls wait() to find out it exited.\r\nvoid\r\nexit(void)\r\n{\r\n  struct proc *p;\r\n  int fd;\r\n\r\n  if(proc == initproc)\r\n    panic(&quot;init exiting&quot;);\r\n\r\n  \/\/ Close all open files.\r\n  for(fd = 0; fd &lt; NOFILE; fd++){\r\n    if(proc-&gt;ofile&#x5B;fd]){\r\n      fileclose(proc-&gt;ofile&#x5B;fd]);\r\n      proc-&gt;ofile&#x5B;fd] = 0;\r\n    }\r\n  }\r\n\r\n  iput(proc-&gt;cwd);\r\n  proc-&gt;cwd = 0;\r\n\r\n  acquire(&amp;ptable.lock);\r\n\r\n  \/\/ Parent might be sleeping in wait().\r\n  wakeup1(proc-&gt;parent);\r\n\r\n  \/\/ Pass abandoned children to init.\r\n  for(p = ptable.proc; p &lt; &amp;ptable.proc&#x5B;NPROC]; p++){\r\n    if(p-&gt;parent == proc){\r\n      p-&gt;parent = initproc;\r\n      if(p-&gt;state == ZOMBIE)\r\n        wakeup1(initproc);\r\n    }\r\n  }\r\n\r\n  \/\/ Jump into the scheduler, never to return.\r\n  proc-&gt;state = ZOMBIE;\r\n  sched();\r\n  panic(&quot;zombie exit&quot;);\r\n}<\/pre>\n<p>\u305d\u3057\u305f\u3089\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306f\u3001\u7d42\u4e86\u4e2d\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u89aa\u30d7\u30ed\u30bb\u30b9\uff08wait\u306esleep\u306e\u547c\u3073\u51fa\u3057\u306b\u3088\u3063\u3066\u30b9\u30ea\u30fc\u30d7\u3057\u3066\u3044\u308b\uff09\u3092\u5b9f\u884c\u3059\u308b\u305f\u3081\u306b\u9078\u629e\u3059\u308b\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\nsleep\u306e\u547c\u3073\u51fa\u3057\u304b\u3089\u623b\u308b\u3068\u304d\u3001ptable.lock\u3092\u4fdd\u6301\u3057\u305f\u72b6\u614b\u306b\u306a\u3063\u3066\u308b\u306e\u3067\u3001wait\u306f\u30d7\u30ed\u30bb\u30b9\u30c6\u30fc\u30d6\u30eb\u3092\u518d\u635c\u67fb\u3057\u3001state == ZOMBIE\u3068\u306a\u3063\u3066\u3044\u308b\u7d42\u4e86\u3057\u305f\u5b50\u30d7\u30ed\u30bb\u30b9\u3092\u635c\u3059\u3053\u3068\u304c\u51fa\u6765\u308b\u3002<br \/>\n\u305d\u3057\u305f\u3089\u305d\u306e\u5b50\u30d7\u30ed\u30bb\u30b9\u306epid\u3092\u8a18\u9332\u3057\u3001\u305d\u3057\u3066\u305d\u306eproc\u69cb\u9020\u4f53\u3092\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u3057\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u3092\u89e3\u653e\u3059\u308b\u3002\uff08\u3053\u3053\u306fwait\u95a2\u6570\u306eif(p\u2212&gt;state == ZOMBIE){\u306e\u4e2d\u306e\u8a71\uff09<\/p>\n<p>\u3053\u308c\u3067\u5b50\u30d7\u30ed\u30bb\u30b9\u306fexit\u306b\u304a\u3051\u308b\u30af\u30ea\u30fc\u30f3\u30a2\u30c3\u30d7\u51e6\u7406\u306e\u5927\u90e8\u5206\u3092\u5b8c\u4e86\u3057\u305f\u3053\u3068\u306b\u306a\u308b\u304c\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;kstack\u3084p-&gt;pgdir\u306f\u89aa\u30d7\u30ed\u30bb\u30b9\u304c\u89e3\u653e\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u3068\u3044\u3046\u306e\u304c\u91cd\u8981\u306a\u70b9\u3067\u3042\u308b\u3002<br \/>\n\u5b50\u30d7\u30ed\u30bb\u30b9\u304cexit\u3092\u5b9f\u884c\u3059\u308b\u3068\u304d\u3001\u305d\u306e\u30b9\u30bf\u30c3\u30af\u306fp-&gt;kstack\u3068\u3057\u3066\u5272\u308a\u5f53\u3066\u3089\u308c\u305f\u30e1\u30e2\u30ea\u306b\u7f6e\u304b\u308c\u3001\u305d\u306e\u30d7\u30ed\u30bb\u30b9\u81ea\u8eab\u306e\u30da\u30fc\u30b8\u30c6\u30fc\u30d6\u30eb\u3068\u3057\u3066\u4f7f\u3046\u3002<br \/>\n\u305d\u308c\u3089\u306f\u3001\u5b50\u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3057\u305f\u5f8c\uff08exit\u5185\u3067sched\u7d4c\u7531\u3067swtch\u304c\u547c\u3070\u308c\u305f\u5f8c\uff09\u3067\u306a\u3044\u3068\u89e3\u653e\u3067\u304d\u306a\u3044\u3002<br \/>\n\u3053\u308c\u306f\u30b9\u30b1\u30b8\u30e5\u30fc\u30e9\u306e\u624b\u7d9a\u304d\u304c\u3001sched\u3068\u547c\u3070\u308c\u308b\u30b9\u30ec\u30c3\u30c9\u306e\u30b9\u30bf\u30c3\u30af\u4e0a\u3067\u306f\u306a\u304f\u3001\u305d\u308c\u81ea\u8eab\u306e\u30b9\u30bf\u30c3\u30af\u4e0a\u3067\u52d5\u4f5c\u3059\u308b\u3072\u3068\u3064\u306e\u7406\u7531\u3067\u3042\u308b\u3002<\/p>\n<h3>\u611f\u60f3<\/h3>\n<p>wait\u3068exit\u306e\u30b3\u30fc\u30c9\u306e\u8aac\u660e\u3067\u3059\u304c\u3001\u3061\u3083\u3093\u3068\u7406\u89e3\u3059\u308b\u305f\u3081\u306b\u306f\u30d7\u30ed\u30bb\u30b9\u306e\u4ed5\u7d44\u307f\u306b\u3064\u3044\u3066\u306e\u7dcf\u5408\u7684\u306a\u77e5\u8b58\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<p>\u81ea\u5206\u306e\u77e5\u8b58\u306e\u4e00\u3064\u4e0a\u3050\u3089\u3044\u306e\u4e8b\u306a\u3089\u307e\u3060\u3042\u30fc\u3060\u3053\u30fc\u3060\u8a00\u3048\u308b\u4f59\u5730\u304c\u3042\u308b\u3093\u3067\u3059\u304c\u3001\u3053\u306e\u7bc0\u306f\u4e8c\u30fb\u4e09\u6bb5\u3050\u3089\u3044\u4e0a\u306a\u611f\u3058\u3067\u3001\u306a\u3093\u3068\u304b\u7406\u89e3\u306f\u51fa\u6765\u308b\u3051\u3069\u53d7\u3051\u8eab\u3067\u300c\u3078\u3047\u305d\u3046\u306a\u3093\u3060\u300d\u3068\u601d\u3046\u3053\u3068\u3057\u304b\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002<\/p>\n<p>\u89aa\u30d7\u30ed\u30bb\u30b9\u306ep-&gt;parent\u306f\u3069\u3046\u306a\u3063\u3066\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u3001\u305d\u308c\u306f\u30b7\u30a7\u30eb\u304b\u3089\u8d77\u52d5\u3055\u308c\u305f\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u5834\u5408\u306f\u3001\u89aa\u306f\u30b7\u30a7\u30eb\u306b\u306a\u3063\u3066\u308b\u306f\u305a\u3067\u3059\u3002\uff08\u30b7\u30a7\u30eb\u306ffork\u3092\u4f7f\u3063\u3066\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u306e\u3067\uff09<br \/>\n\u3067\u306f\u30b7\u30a7\u30eb\u306f\u3069\u3046\u306a\u306e\u304b\u3068\u3044\u3046\u3068\u3001\u30bd\u30fc\u30b9\u3092\u5168\u90e8\u8aad\u3093\u3060\u308f\u3051\u3067\u306f\u306a\u3044\u306e\u3067\u3001\u9593\u306b\u4f55\u304b\u306f\u3055\u307e\u3063\u3066\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001initproc\u306e\u5b50\u306b\u306a\u3063\u3066\u308b\u306f\u305a\u3067\u3059\u3002<br \/>\ninitproc\u304c\u3059\u3079\u3066\u306e\u30d7\u30ed\u30bb\u30b9\u306e\u6700\u7d42\u7684\u306a\u89aa\u306b\u306a\u308b\u306e\u3067\u3001exit\u95a2\u6570\u306finitproc\u3067\u5b9f\u884c\u3055\u308c\u308b\u3068panic\u3059\u308b\u3088\u3046\u306b\u306a\u3063\u3066\u307e\u3059\u306d\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-1567","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\/1567","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=1567"}],"version-history":[{"count":0,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/posts\/1567\/revisions"}],"wp:attachment":[{"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/media?parent=1567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/categories?post=1567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/peta.okechan.net\/blog\/wp-json\/wp\/v2\/tags?post=1567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}