ã©ã®ãããªãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ããã³ã³ãã¥ãŒã¿ãæã£ãŠããããã»ããµã®æ°ä»¥äžã®ããã»ã¹ãå®è¡ããåŸã£ãŠããã»ã¹éã§æåå²å ±æãå¿ èŠã«ãªããçæ³çã«ã¯ããŠãŒã¶ããã»ã¹ããã¯ãã®å ±æã¯èŠããªãããã«ããã¹ãã§ãããå ±éã®ã¢ãããŒããšããŠã¯ãåããã»ã¹ãåã ã«ä»®æ³ãã·ã³ãä¿æããŠããããã«èŠãããããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ãè€æ°ã®ä»®æ³ãã·ã³ãåäžã®ããã»ããµã§æåå²å ±æããŠå®è¡ããããšã§ãããæ¬æ§ã§ã¯xv6ãã©ã®ããã«ããŠè€æ°ã®ããã»ã¹ãããã»ããµäžã§å®è¡ããŠããã®ãã«ã€ããŠèª¬æããã
xv6ã¯åããã»ããµãããããã»ã¹ããä»ã®ããã»ã¹ã«åãæ¿ããããšã§å€éåãè¡ãããããã«ã¯2ã€ã®åºäº¬ãããããã1ã€ç®ã¯ãxv6ã¯ãããããã»ã¹ãããã€ã¹ããã€ãI/Oã®å®äºãåŸ
ã€ããã«åŸ
ã¡ç¶æ
ã«ãªããšãsleep
ãšwakeup
ã®2ã€ã®ã¡ã«ããºã ã«ããåãæ¿ããè¡ãããåäŸãçµäºããã®ãåŸ
ã€ããsleep
ã·ã¹ãã ã³ãŒã«ã«ãã£ãŠçµäºããã®ãåŸ
ã€ã2çªç®ã¯ãxv6ããŠãŒã¶åœä»€ãå®è¡äžã«ãå®æçã«åŒ·å¶çã«åãæ¿ããè¡ãã
ãã®å€éåã«ãããåããã»ã¹ã¯èªåã®CPUãæã£ãŠããããã«èŠããããxv6ãã¡ã¢ãªã¢ãã±ãŒã¿ãšããŒããŠã§ã¢ããŒãžããŒãã«ã«ããåããã»ã¹ã®åºæã®ã¡ã¢ãªãæã£ãŠããããã«èŠãæããŠããã ãã§ããã
å€éåãå®è£ ããã«ã¯ãããã€ãå°é£ãªç¹ããããæåã«ãããããã»ã¹ããã©ã®ããã«ããŠå¥ã®ããã»ã¹ã«åãæ¿ããã®ãïŒxv6ã¯ã³ã³ããã¹ãã¹ã€ããã³ã°ã®æšæºçãªã¡ã«ããºã ãå©çšããŠãã; ãããã¢ã€ãã¢ã¯ã·ã³ãã«ã§ãå®è£ ã¯ã·ã¹ãã ã«ãããŠæãäžéæã§ããã2çªç®ã«ãã©ã®ããã«ããŠééçãªã³ã³ããã¹ãã¹ã€ããã³ã°ãå®çŸããã®ãïŒxv6ã¯æšæºçãªã¿ã€ãå²ã蟌ã¿ãã³ãã©ã«ããã³ã³ããã¹ãã¹ã€ãããé§åããŠããã3çªç®ã«ãå€ãã®CPUã¯ããã»ã¹ãåæã«åãæ¿ããŠãããåŸã£ãŠã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ãé¿ããããã«ããã¯ã®æ©æ§ãèããå¿ èŠãããã4çªç®ã«ãããã»ã¹ãçµäºãããšãã«ããã®ã¡ã¢ãªãšè³æºãéæŸããªããã°ãªããªãããããããããèªåèªèº«ã§ã¯å®è¡ã§ããªããäœæ ãªãã°ã(äŸãã°)èªåãå©çšããŠããã®ã«èªåã®ã«ãŒãã«ã¹ã¿ãã¯ãéæŸããããšã¯ã§ããªãããã§ãããxv6ã¯ãã®åé¡ããªãã¹ãã·ã³ãã«ãªæ¹æ³ã§è§£æ±ºããããšããŠããããçµæãšããŠåŸãããã³ãŒãã¯ããªãããŒã§ããã
xv6ã¯ããã»ã¹ãèªåèªèº«ã調æŽããããšã®ã§ããæ¹æ³ãæäŸããªããã°ãªããªããäŸãã°ã芪ããã»ã¹ã¯ãã®åããã»ã¹ãçµäºãããŸã§åŸ ã€ããä»ã®ããã»ã¹ããã€ããžã®æžã蟌ã¿ãè¡ãã®ãåŸ ããªããã°ãªããªããããã»ã¹ããææã®ã€ãã³ããçºçããŠããããã§ãã¯ããããã«CPUãç¡é§ã«å©çšããããããxv6ã¯CPUã®å©çšãè«ŠããŠã€ãã³ããçºçãããŸã§ã¯ç ã£ãŠãããä»ã®ããã»ã¹ãæåã®ããã»ã¹ãèµ·åããã»ããè¯ããã€ãã³ãã®éç¥ãèªã¿èœãšãããšãé¿ããããã«ãã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ãé¿ããããã®ã±ã¢ãå¿ èŠã«ãªãããã®åé¡ãšè§£çã®äŸãšããŠãæ¬ç« ã§ã¯ãã€ãã®å®è£ ã«ã€ããŠåãæ±ãã
å³5-1ã«ç€ºãããã«ãããã»ã¹éã§åãæ¿ããè¡ãããã«ã¯ãxv6ã¯äœã¬ã€ã€ã«ãããŠ2çš®é¡ã®ã³ã³ããã¹ãã¹ã€ãããè¡ã£ãŠãã: ããã»ã¹ã®ã«ãŒãã«ã¹ã¬ããããçŸåšã®CPUã®ã¹ã±ãžã¥ãŒã©ã¹ã¬ãããžã®åãæ¿ããšãã¹ã±ãžã¥ãŒã©ã¹ã¬ããããããã»ã¹ã®ã«ãŒãã«ã¹ã¬ãããžã®åãæ¿ãã§ãããxv6ã¯ããããŠãŒã¶ç©ºéã®ããã»ã¹ããä»ã®ããã»ã¹ãžçŽæ¥åãæ¿ããããšããããšã¯æ±ºããŠãªã;ãã®ãããªçŽæ¥ä»ã®ããã»ã¹ã«åãæ¿ããç¶æ³ã¯ããŠãŒã¶ã«ãŒãã«ã®å€æ(ã·ã¹ãã ã³ãŒã«ãããã¯å²ã蟌ã¿)ã«ãã£ãŠçºçããããšã¯ããããã¹ã±ãžã¥ãŒã©ãžã®ã³ã³ããã¹ãã¹ã€ãããæ°ããããã»ã¹ã®ã«ãŒãã«ã¹ã¬ãããžã®ã³ã³ããã¹ãã¹ã€ãããããã³ãã©ãããªã¿ãŒã³ã®å Žåã®ã¿çºçãããæ¬ç« ã§ã¯ãã®ã¡ã«ããºã ã®èª¬æãšããŠãã«ãŒãã«ã¹ã¬ãããšã¹ã±ãžã¥ãŒã©ã¹ã¬ãããåãæ±ãã
第2ç« ã§èŠãŠããããã«ãå
šãŠã®xv6ã®ããã»ã¹ã¯èªåèªèº«ã®ã«ãŒãã«ã¹ã¿ãã¯ãšã¬ãžã¹ã¿ã»ãããæã£ãŠãããåCPUã¯ä»»æã®ããã»ã¹ã®ã«ãŒãã«ã¹ã¬ããåãã§ã¯ãªããã¹ã±ãžã¥ãŒã©ãå®è¡ããã«åé¢ããã¹ã±ãžã¥ãŒã©ã¹ã¬ãããæã£ãŠããããã1ã€ã®ã¹ã¬ããããä»ã®ã¹ã¬ããã«åãæ¿ããããã«ãå€ãã¹ã¬ããã®CPUã¬ãžã¹ã¿ã察æ¯ããæ°ããã¹ã¬ããã®ã¬ãžã¹ã¿ã埩垰ããããšããåŠçãçºçãã;%esp
ãš%eip
ã®ä¿åãšå埩ãå®è¡ãããCPUãã¹ã¿ãã¯ãã¹ã€ããããããšã§ãå®è¡ããŠããã³ãŒããã¹ã€ããããããšã«ãªãã
swtch
ã¯ã¹ã¬ããã®ããšãçŽæ¥ç¥ã£ãŠããããã§ã¯ãªã;contexts
ãšåŒã°ããã¬ãžã¹ã¿ã»ããã®ä¿åãšåŸ©åž°ãè¡ãåŠçãå®è¡ããŠããã ãã§ãããããã»ã¹ãCPUã䜿ãããšãè«Šãããšãããã»ã¹ã®ã«ãŒãã«ã¹ã¬ãããswtch
ãäºåãèªèº«ã®ã³ã³ããã¹ããéé¿ããŠã¹ã±ãžã¥ãŒã©ã³ã³ããã¹ããžãšé£ã¶ãåã³ã³ããã¹ãã¯struct context*
ãšããŠè¡šçŸãããŠãããé¢é£ããã«ãŒãã«ã¹ã¿ãã¯ã®æ§é äœã®ãã€ã³ã¿ãšããŠè¡šçŸãããŠããã
swtch
ã¯2ã€ã®åŒæ°ãåã; struct context **old
ãšstruct context *new
ã§ãããswtch
ã¯çŸåšã®CPUã¬ãžã¹ã¿ãã¹ã¿ãã¯ã«ä¿åããŠãã¹ã¿ãã¯ã®ãã€ã³ã¿ã*old
ã«ä¿åããã次ã«ãswtch
ã¯newã%esp
ã«ã³ããŒããåã®ä¿åããã¬ãžã¹ã¿ããããããŠããé¢æ°ããæ»ãã
swtch
å
ãèŠãŠã¹ã±ãžã¥ãŒã©ãè¿œãããã代ããã«ãç§ãã¡ã®ãŠãŒã¶ããã»ã¹ã埩垰ãããšãããèŠãŠã¿ããã第3ç« ã«ãããŠãåå²ã蟌ã¿ã®æåŸã«trap
ãyield
ãåŒã³åºãå¯èœæ§ãããããšã«ã€ããŠè§Šãããyield
ã¯sched
ãåŒã³åºããsched
ã¯proc->context
ã«å
¥ã£ãŠããçŸåšã®ã³ã³ããã¹ããä¿åããŠcpu->scheduler
ã«ãã£ãŠä¿åããŠããéå»ã®ã¹ã±ãžã¥ãŒã©ã³ã³ããã¹ãã«ã¹ã€ãããã(2766è¡ç®)ã
swtch
(2952è¡ç®)ã¯ãŸãã¹ã¿ãã¯ããåŒæ°ãããŒãããŠãããã%eax
ãš%edx
(2959-2960è¡ç®)ã«æ ŒçŽãã;swtch
ã¯ã¹ã¿ãã¯ãã€ã³ã¿ãå€æŽããŠ%esp
ãéããŠã©ãã«ãã¢ã¯ã»ã¹ã§ããªããªãåã«ãããå®è¡ããå¿
èŠãããã次ã«ãswtch
ã¯ã¬ãžã¹ã¿ã¹ããŒããä¿åããçŸåšã®ã¹ã¿ãã¯äžã«ã³ã³ããã¹ãæ§é äœãäœæãããåŒã³åºãå
ãä¿åããã¬ãžã¹ã¿ã¯ä¿åããå¿
èŠããã; x86ã¯%ebp
,%ebx
,%esi
,%ebp
,%esp
ã察象ã§ããã
swtch
ã¯æåã®4ã€ã®ã¬ãžã¹ã¿ãæ瀺çã«ããã·ã¥ãã(2963-2966è¡ç®); æåŸã®ã¬ãžã¹ã¿ã¯ã*old
ã«struct context*
ãæžã蟌ãããšã«ãã£ãŠæé»çã«ä¿åããããããã«ãããäžã€éèŠãªã¬ãžã¹ã¿ãååšãã: ããã°ã©ã ã«ãŠã³ã¿%eip
ã¯swtch
ãåŒã³åºãcall
ã«ããä¿åããã%ebp
ã®ã¹ã¿ãã¯ã®äžã«æ ŒçŽããããå€ãã³ã³ããã¹ããä¿åããããšã«ãã£ãŠãswtch
ã¯æ°ããã³ã³ããã¹ããããŒãããæºåãæŽããswtch
ã¯ãã€ã³ã¿ãæ°ããã³ã³ããã¹ãã®ã¹ã¿ãã¯ãã€ã³ã¿ã«ç§»ã(2970è¡ç®)ãæ°ããã¹ã¿ãã¯ã¯swtch
ãä¿åããå€ãã¹ã¿ãã¯ã®ããšã®æ§é çã«ã¯äžç·ã§ãã - æ°ããã¹ã¿ãã¯ã¯åã®swtch
ãåŒã°ãããšãã¯å€ãã¹ã¿ãã¯ã ã£ãã®ã§ãã - ãããã£ãŠãswtch
ã¯æ°ããã³ã³ããã¹ããéé¿ããæé ãéã«èžãã§ããã°ããã%edi
,%esi
,%ebx
,%ebp
ããããããè²·ããããåœä»€ã¢ãã¬ã¹ã¯æ°ããã³ã³ããã¹ãã®ãã®ã§ããã
ç§ãã¡ã®äŸã§ã¯ãsched
ã¯swtch
ãåŒã³åºããŠcpu->scheduler
ã«ã¹ã€ããããŠãCPUæ¯ã®ã¹ã±ãžã¥ãŒã©ã³ã³ããã¹ãã«ã¹ã€ãããããã³ã³ããã¹ãã¯scheduler
ã«ããä¿åãããswtch
ãåŒã°ãã(2728è¡ç®)ãswtch
ãã©ãã«æ»ããããã¬ãŒã¹ããŠèšã£ããšããsched
ã«ã¯æ»ããã«scheduler
ã«æ»ããã¹ã¿ãã¯ãã€ã³ã¿ã¯çŸåšã®CPUã®ã¹ã±ãžã¥ãŒã©ã¿ã¹ã¯ãæããŠãããinitproc
ã®ã«ãŒãã«ã¹ã¿ãã¯ãæããŠããããã§ã¯ãªãã
åç« ã§ã¯ãswtch
ã®äœã¬ã€ã€ã®è©³çŽ°ã«ã€ããŠç¢ºèªãã; ã§ã¯ãswtch
ãäŸã«åããããããã»ã¹ããã¹ã±ãžã¥ãŒã©ã«ç§»ããããã«ããã»ã¹ã«ç§»ãããã®æ¹æ³ã«ã€ããŠèŠãŠããããCPUã®äœ¿çšãåãæ¢ãããããã»ã¹ã¯ãããã»ã¹ããŒãã«ããã¯ã§ããptable.lock
ãååŸããçŸåšä¿æããŠããå
šãŠã®ããã¯ããªãªãŒã¹ããçŸåšã®ç¶æ
(exit
)ãæŽæ°ããsched
ãåŒã¶ãyield
(2772)ã¯ãã®æ
£ç¿ã«åŸããsleep
åœä»€ãšexit
åœä»€ãå®è¡ããããããã«ã€ããŠã¯åŸã«èŠãããšã«ãããsched
ã¯ãããã®ç¶æ
ã®ããã«ãã§ãã¯ãè¡ã(2757-2762è¡ç®)ããããã®ç¶æ
ã®implicationè¡ã:äœæ
ãªãã°ãCPUã¯ããã¯ãç²åŸããå Žåã¯å²ã蟌ã¿ãç¡å¹åããŠããå¿
èŠãããããã§ããã
æåŸã«ãsched
ã¯swtch
ãåŒã³proc->context
ã®çŸåšã®ã³ã³ããã¹ããä¿åããŠãcpu->scheduler
ã«ããä¿æãããŠããã¹ã±ãžã¥ãŒã©ã³ã³ããã¹ãã«ã¹ã€ãããã(2728è¡ç®)ãã¹ã±ãžã¥ãŒã©ã¯forã«ãŒããå®è¡ãç¶ããå®è¡ã§ããããã»ã¹ãèŠã€ããã¹ã€ããã³ã°ããããšãç¶ããã
xv6ãã¯swtch
ãåŒã³åºããŠããéãptable.lock
ãä¿æãããšãããèŠã: swtch
ã®åŒã³åºãå
ã¯æ¢ã«ãã®ããã¯ãç²åŸããŠããå¿
èŠããããããã¯ã®å¶åŸ¡ã¯ã³ãŒãã®ã¹ã€ããã³ã°ã«æž¡ãããããã®æ
£ç¿ã¯ããã¯ã«ãšã£ãŠéåžžã®ããšã§ã¯ãªã; å
žåçãªæ
£ç¿ã¯ãããã¯ãç²åŸããã¹ã¬ãããããã¯ã®è§£æŸã®è²¬ä»»ãæã€ããšã§ãããããã¯æ£ãããä¿èšŒããããã«ã¯åœç¶ã®ããšã§ãããã³ã³ããã¹ãã¹ã€ããã³ã°ã®ããã«ã¯ãå
žåçãªæ
£ç¿ãç Žå£ããå¿
èŠããããäœæ
ãªãã°ãptable.lock
ã¯ãswtch
ãå®è¡äžã«ã¯çã§ã¯ãªãããã»ã¹ã®ç¶æ
ãšcontextãã£ãŒã«ãã®äžå€æ¡ä»¶ãä¿è·ããŠããããã§ãããptable.lock
ãswtch
ã®éäžä¿æãããŠããªãã£ãå Žåã«çºçããåé¡ã®äŸã瀺ã: ç°ãªãCPUäžã§yield
ãç¶æ
ãRUNNABLE
ã«å€æŽããåŸã«ãã©ã®ããã»ã¹ãå®è¡ããããæ¥ããå¿
èŠãããããswtch
ãåŒã¶åã«ã«ãŒãã«ã¹ã¿ãã¯ã䜿ãããšãæ¢ããããã®çµæã«ãããåäžã®ã¹ã¿ãã¯äžã§å®è¡ããŠãã2ã€ã®CPUã®å®è¡ç¶æ
ããæ£ããèšå®ããããšãã§ããªããªãã
ã«ãŒãã«ã¹ã¬ããã¯ãsched
ã®äžã§ãã€ãããã»ããµã®å©çšãæ¢ããã¹ã±ãžã¥ãŒã©äžã®åäžã®å Žæã«ã¹ã€ããããsched
å
ã§(æ®ã©)åžžã«ããã»ã¹ã«ã¹ã€ãããããåŸã£ãŠãããxv6ãã¹ã¬ãããã¹ã€ããããè¡çªå·ãããªã³ããããšã以äžã®ãããªã·ã³ãã«ãªãã¿ã³ãååšããã¯ãã§ãã(2728è¡ç®),(2766è¡ç®)ã(2728è¡ç®)ã(2766è¡ç®)ã§ããããã®ãããªåœ¢åŒã§2ã€ã®ã¹ã¬ãããã¹ã€ããã³ã°ãçºçãããããšãããã³ã«ãŒãã³ããšèªãã§ãã;ãã®äŸã§ã¯ãsched
ãšscheduler
ãããããã³ã«ãŒãã³ã§ããã
æ°ããããã»ã¹ãsched
å
ã§çµäºããªãäŸãããã第2ç« ã§èŠãããã«ãæ°ããããã»ã¹ãæåã«ã¹ã±ãžã¥ãŒã«ããããšãã§ãããæ°ããããã»ã¹ã¯ãforkret
(2783è¡ç®)ããå®è¡ãéå§ãããforkret
ã¯ptable.lock
ã解æŸããããšã§ããã®æ
£ç¿ãå®ãããã®ååšããŠãã; ããã§ãªããã°ãæ°ããããã»ã¹ã¯trapret
ããã¹ã¿ãŒãããããšã«ãªãã
scheduler
(2708è¡ç®)ã¯åçŽãªã«ãŒããå®è¡ãã: å®è¡å¯èœãªããã»ã¹ãèŠã€ãããããåæ¢ãããŸã§å®è¡ããããšãç¹°æ¿ãããscheduler
ã¯æ®ã©å
šãŠã®åäœäžã«ãptable.lock
ã®ããã¯ãä¿æããŠããããåç¹°ãè¿ãã«ãããŠãã«ãŒãã®å€ã«åºããšãã ãããã¯ã解æŸãã(ãããŠãæ瀺çã«å²ã蟌ã¿ãèš±å¯ãã)ãããã¯ãCPUãã¢ã€ãã«ç¶æ
ã®ãšã(RUNNABLE
ãªããã»ã¹ãçºèŠããããšãã§ããªãã£ããšã)ã«éèŠã§ãããã¢ã€ãã«ç¶æ
ã®ã¹ã±ãžã¥ãŒã©ãããã¯ãä¿æãç¶ããŠãããšãããã»ã¹ãå®è¡ããŠããä»ã®CPUãã³ã³ããã¹ãã¹ã€ããããã·ã¹ãã ã³ãŒã«ã«é¢é£ããããã»ã¹ãå®è¡ããããããã«ç¹ã«ããã»ã¹ãRUNNABLE
ã«èšå®ããæäœãã§ãããéäŒç¶æ
ã®CPUãäºåºŠãšã¹ã±ãžã¥ãŒãªã³ã°ã§ããªããªã£ãŠããŸããå®æçã«å²ã蟌ã¿ãèš±å¯ããçç±ã¯ãã¢ã€ãã«äžã®CPUã§ãäŸãã°ã·ã§ã«ã®ãããªI/OåŸ
ã¡ã®ç¶æ
ã§RUNNABLE
ã®ããã»ã¹ãååšããªãå Žåã®ããã§ãã;
ããã¹ã±ãžã¥ãŒã©ãå²ã蟌ã¿ãåžžã«äžèš±å¯ã«ããŠããå ŽåãI/Oã®å²ã蟌ã¿ã¯ããäºåºŠãšçºçããªããªã£ãŠããŸãã
ã¹ã±ãžã¥ãŒã©ã¯ããŒãã«ãåç
§ããªãããp->state==RUNNABLE
ã§ããããã»ã¹ãã€ãŸãå®è¡å¯èœãªç¶æ
ã«ããããã»ã¹ãæ¢ãç¶ããã
ããã»ã¹ãçºèŠãããšãCPUæ¯ã®çŸåšã®ããã»ã¹ã®å€æ°ã§ããprocãèšå®ããããã»ã¹ã®ããŒãžããŒãã«ãswitchuvm
ã«ããã¹ã€ããããããã»ã¹ãRUNNING
ã«èšå®ããswtch
ãå®è¡ããŠããã»ã¹ã®å®è¡ãéå§ãã(2722-2728è¡ç®)ã
ã¹ã±ãžã¥ãŒãªã³ã°ã®ã³ãŒãã®æ§é ã«ã€ããŠèããããã®äžã€ã®æ¹æ³ã¯ãåããã»ã¹ãåžžã«äžå€æ¡ä»¶ãç¶æããããã«èª¿æŽãããŠãããšããŠããã®äžå€æ¡ä»¶ãçã§ãªããªããšãã¯åžžã«ptable.lock
ãä¿æãããŠãããšèããããšã§ãããäžå€æ¡ä»¶ã®äžã€ã¯ãããããã»ã¹ãRUNNING
ç¶æ
ã§ããã°ãå®è¡ç¶æ
ã¯æŽã£ãŠãããã¿ã€ããŒå²ã蟌ã¿ã®yield
ã¯æ£ããããã»ã¹ããã¹ã€ããããããšãã§ãã; ããã¯ãCPUã®ã¬ãžã¹ã¿ããã®ããã»ã¹ã®å€ãåžãããŠãã(äŸãã°ããããã¯å®éã«ã¯contextã®äžã«ã¯ååšããªã)ã%cr3ã¯ããã»ã¹ã®ããŒãžããŒãã«ãåç
§ããŠããã%esp
ã¯ããã»ã¹ã®ã«ãŒãã«ã¹ã¿ãã¯ãåç
§ããŠãªããã°ãªãããåŸã£ãŠãswtch
ã¯ã¬ãžã¹ã¿ãæ£ããããã·ã¥ããŠãããprocã¯ããã»ã¹ã®proc[]ã¹ããããåç
§ããŠããªããã°ãªããªãã
ä»ã®äžå€æ¡ä»¶ã¯ãããããã»ã¹ãRUNNABLE
ã§ããã°ãã¢ã€ãã«ç¶æ
ã®CPUã§ã¯ãã¹ã±ãžã¥ãŒã©ãå®è¡ããããšãã§ãã; p->contextã¯ããã»ã¹ã®ã«ãŒãã«ã¹ã¬ããã®å€ãæã£ãŠãããããã»ã¹ã®ã«ãŒãã«ã¹ã¿ãã¯ãå®è¡ããŠããCPUã¯ååšãããCPUã®%cr3ã¯ããã»ã¹ã®ããŒãžããŒãã«ãåç
§ããŠããããCPUã®procã¯ããã»ã¹ãåç
§ããŠã¯ããªãã
äžèšã®äžå€æ¡ä»¶ã管çããããšããxv6ãptable.lock
ã1ã€ã®ã¹ã¬ãã(ãã°ãã°yield
ã®äž)ã§ç²åŸããç°ãªãã¹ã¬ãã(ã¹ã±ãŒãŠã©ã¹ã¬ãããããã¯ä»ã®æ¬¡ã®ã«ãŒãã«ã¹ã¬ãã)ã§è§£æŸããçç±ã§ããã
å®è¡ããŠããããã»ã¹ã®ç¶æ
ãRUNNABLE
ã«èšå®ããããã®å€æŽãå§ãŸããšããã®äžå€æ¡ä»¶ãä¿®æ£ããããŸã§ã¯ãlockãä¿æããŠããªããã°ãªããªãïŒ æçã®æ£ãã解æŸãã€ã³ãã¯ãscheduler
ãããã»ã¹ã®ããŒãžããŒãã«ã䜿çšããã®ãæ¢ããprocãã¯ãªã¢ãããšããã§ãããåæ§ã«ãäžåºŠscheduler
ãå®è¡ç¶æ
ã®ããã»ã¹ãRUNNING
ã«å€æŽããå Žåã¯ãã«ãŒãã«ã¹ã¬ãããå®å
šã«å®è¡ããç¶æ
ã«ãªããŸã§(swtch
ããå®è¡ããŠãããäŸãã°yield
ã®äžã§)ããã¯ã¯è§£æŸããããšãã§ããªãã
ptable.lock
ã¯åæ§ã«ãä»ã®éšåã«ã€ããŠãä¿è·ãè¡ã£ãŠãã: ããã»ã¹ã®IDã®å²ãåœãŠãšãããã»ã¹ã®ããŒãã«ã®è§£æŸåŠçãšãexit
ãšwait
ã®çžäºäœçšãšãwakeup
ã®ãã¹ããé¿ããããã®æç¶ã(æ¬¡ç« ãåç
§ã®ããš)ãšãä»ã«ãæ§ã
ãªããšã«å©çšããããptable.lock
ã®ä»ã®æ©èœã«ã€ããŠèããããšã¯ãæ確æ§ã«ã€ããŠã¯ç¢ºå®ã«ãæ§èœã«ã€ããŠã¯ãããããåå²ããŠèããããšã䟡å€ã®ããããšã«ãªãxxxã
ã¹ã±ãžã¥ãŒãªã³ã°ãšããã¯ã¯ãããããã»ã¹ãä»ã®ããã»ã¹ããååšãé ãããšãå©ããããä»ã®ãšããã¯ããã»ã¹ãæå³çã«çžäºäœçšããããšãå©ããããã®æœè±¡åã¯ååšããŠããªããsleep
ãšwakeup
ã¯ãããåãããã®ã§ãããããã»ã¹ãã€ãã³ããåŸ
ã€ããã«ã¹ãªãŒãç¶æ
ã«å
¥ããã€ãã³ããçºçãããšä»ã®ããã»ã¹ã眮ããããšããããšãã§ããããã«ãªããsleep
ãšwakeup
ã¯ãsequence coordinationããããã¯ãconditional synchronizationãã®ã¡ã«ããºã ãšåŒã°ãããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã®æç®ã«ã¯ãä»ã«ã䌌ããããªå€ãã®ã¡ã«ããºã ãååšããã
ãã®æ§é ã説æããããã«ããŸãã¯ç°¡åãªçç£è ãšæ¶è²»è ã®ãã¥ãŒãèããããã®ãã¥ãŒã¯ããã»ã¹ããã³ãã³ããåããšãIDEã®ãã©ã€ããšäŒŒãŠãã(第3ç« ãåç §ã®ããš)ããIDEã®ç¹å®ã®ã³ãŒãããã¯æœè±¡åãããŠããã ãã¥ãŒã¯ããããã»ã¹ãéãŒãã®ãã€ã³ã¿ãä»ã®ããã»ã¹ã«éä¿¡ããããšãèš±å¯ããŠãããããéä¿¡è ã1ã€ã§ãåä¿¡è ã1ã€ã§ãããããããç°ãªãCPUäžã§åäœããŠããã°ãã³ã³ãã€ã©ã¯åŒ·åã«æé©åãããããšã¯ç¡ãã以äžã®ãããªå®è£ ã§å®çŸããããšãã§ãã:
100 struct q {
101 void *ptr;
102 };
103
104 void*
105 send(struct q *q, void *p)
106 {
107 while(`q->ptr` != 0)
108 ;
109 `q->ptr` = p;
110 }
111
112 void*
113 recv(struct q *q)
114 {
115 void *p;
116
117 while((p = `q->ptr`) == 0)
118 ;
119 `q->ptr` = 0;
120 return p;
121 }
send
ã¯ããã¥ãŒã空ã®éã¯å®è¡ãç¶ãããã€ã³ã¿pããã¥ãŒã«æ¿å
¥ãããrecv
ã¯ãã¥ãŒã空ã§ãªãéã¯å®è¡ãç¶ãããã€ã³ã¿ãåãåºããããã»ã¹ãšããŠå®è¡ãããŠãããšãã¯ãsend
ãšrecv
ã¯ã©ã¡ããšãq->ptr
ãå€æŽããããsend
ã¯q->ptr
ããŒãã®ãšãã ãæžã蟌ã¿ãrecv
ã¯p->ptr
ãéãŒãã®ãšãã ãæžã蟌ããåŸã£ãŠãæŽæ°æ
å ±ããã¹ãããããšã¯ãªãã
äžèšã®å®è£
ã¯ã³ã¹ããé«ããããéä¿¡è
ãæ®ã©éä¿¡ãããªããã°ãåä¿¡è
ã¯whileã«ãŒãã®äžã§ãã€ã³ã¿ããã£ãŠæ¥ããŸã§ã¹ãã³ããªããåŸ
ã£ãŠããªããã°ãªããªããåä¿¡è
ã®CPUã¯ãããåä¿¡è
ãCPUãæ¶è²»ããä»ã®æ¹æ³ãååšããã°ãsend
ããã€ã³ã¿ãéä¿¡ãããšãã ãå埩ãããã以å€ã®ãšãã¯ç ã£ãŠããããã
以äžã®ããã«åäœãããsleep
ãšwakeup
ã®2ã€ã®åŒã³åºããæ³åããŠã¿ãããsleep(chan)
ã¯ãä»»æã®å€chan
äžã§ã¹ãªãŒãç¶æ
ã«å
¥ãããããwait
ãã£ãã«ãšåŒã¶ãsleep
ã¯ã¹ãªãŒãç¶æ
ã«å
¥ãããã«ããã»ã¹ãåŒã³åºããä»ã®ä»äºã®ããã«CPUãææŸããwakeup(chan)
ã¯chanäžã§ã¹ãªãŒãç¶æ
ã«å
¥ã£ãŠããå
šãŠã®ããã»ã¹ãåŒã³åºã(ããå¿
èŠãªãã°)ãæ»ãããã«ãããã®sleep
ãåŒã³åºã(xxx)ãchanäžã§ããã»ã¹ãåŸ
ã£ãŠããªããã°ãwakeup
ã¯äœãããªãããã®ãããªsleep
ãšwakeup
ãå©çšããããã«ã以äžã®ããã«ãã¥ãŒã®å®è£
ãå€æŽããã
201 void*
202 send(struct q *q, void *p)
203 {
204 while(q->ptr != 0)
205 ;
206 q->ptr = p;
207 wakeup(q); /* wake recv */
208 }
209
210 void*
211 recv(struct q *q)
212 {
213 void *p;
214
215 while((p = q->ptr) == 0)
216 sleep(q);
217 q->ptr = 0;
218 return p;
219 }
recv
ã¯ã¹ãã³ç¶æ
ã«å
¥ãã®ã§ã¯ãªããCPUãææŸããããã¯è¯ãæ¹æ³ã§ããããããããã®ã€ã³ã¿ãã§ãŒã¹ã§ãå³5-2ã§èª¬æããããã¹ãããwakeup
ããšããŠç¥ãããŠããåé¡ã解決ããããã®ã€ã³ã¿ãã§ãŒã¹ãå©çšããŠsleep
ãšwakeup
ãå®è£
ããããšã¯ãç°¡åãªè©±ã§ã¯ãªããäŸãã°ãrecv
ã215è¡ç®ã®q->ptr==0
ã§ããããšãæ€åºãããšããããrecv
ã¯215ãš216è¡ç®ã®éã«ãããšããsend
ã¯ä»ã®CPUäžã§åäœããŠãã:
send
ã¯q->ptr
ãéãŒãã®å€ã«æžãæããwakeup
ãåŒã¶ããã¹ãªãŒãç¶æ
ã«å
¥ã£ãŠããããã»ã¹ã¯ååšãããäœãèµ·ãããªããrecv
ã¯216è¡ç®ãå®è¡ããsleep()
ãå®è¡ããããšã§ã¹ãªãŒãç¶æ
ã«å
¥ããããã§åé¡ãçãã: recv
ã¯ã¹ãªãŒãç¶æ
ã«å
¥ãããã€ã³ã¿ãåŸ
ã£ãŠããããããã¯æ¢ã«å°çããŠããã次ã®send
ãrecv
ãèµ·ããŠãã¥ãŒäžã®ãã€ã³ã¿ãæ¶è²»ããã®ãåŸ
ã€ããã«ã¹ãªãŒãç¶æ
ã«å
¥ãããã®æç¹ã§ãã®ã·ã¹ãã ã§ã¯ãããããã¯ãçºçããã
ãã®åé¡ã®åå ã¯ãrecv
ã¯q->ptr==0
ãæç«ããªããªã£ããšãã«ã®ã¿ã¹ãªãŒãç¶æ
ã«å
¥ãããããšã¯éãã¿ã€ãã³ã°ã§send
ãå®è¡ããããšããã«ããã以äžã®ããã«ãrecv
ã®ã³ãŒããå€æŽããŠäžå€æ¡ä»¶ãä¿ãšããšããã®ã¯ééãã§ããïŒ
300 struct q {
301 struct spinlock lock;
302 void *ptr;
303 };
304
305 void*
306 send(struct q *q, void *p)
307 {
308 acquire(&`q->lock`);
309 while(q->ptr != 0)
310 ;
311 q->ptr = p;
312 wakeup(q);
313 release(&`q->lock`);
314 }
315
316 void*
317 recv(struct q *q)
318 {
319 void *p;
320
321 acquire(&`q->lock`);
322 while((p = q->ptr) == 0)
323 sleep(q);
324 q->ptr = 0;
325 release(&`q->lock`);
326 return p;
327 }
recv
ããã®ããã«ããŠä¿è·ãããšãããã¯ã322è¡ç®ããã³323è¡ç®ãå®è¡ãããããšããsend
ãé²ããããwakeup
ããã¹ãããããšãåé¿ã§ããããããããã§ããããããã¯ãçºçãã: recv
ã¯ã¹ãªãŒãç¶æ
ã«å
¥ã£ãŠããéã¯ããã¯ãä¿æããŠãããããã¯ã®è§£æŸãåŸ
ã€ããã«éä¿¡è
ãæ°žä¹
ã«åŸ
ã¡ç¶ããããšã«ãªãã
äžèšã®æ¹æ³ããããã¯ãsleep
ã«æž¡ãããšã«ãããåŒã³åºãå
ã®ããã»ã¹ãã¹ãªãŒãç¶æ
ãšããŠããŒã¯ãããã¹ãªãŒããã£ãã«ãåŸ
ã£ãŠããç¶æ
ã«ãªã£ãŠãããã¯ã解æŸã§ããããã«å€æŽãããããã¯ã¯åä¿¡è
ãèªåèªèº«ãã¹ãªãŒãç¶æ
ã«ãããŸã§send
ãå®è¡ãããã®ãé²ããåŸã£ãŠãwakeup
ã¯ã¹ãªãŒãããŠããåä¿¡è
ã確å®ã«èµ·ããããšãã§ãããåä¿¡è
ãã¹ãªãŒãç¶æ
ããèµ·ãããšãé¢æ°ããæããåã«åã³ããã¯ãç²åŸããã
æçµçãªãæ£ããã³ãŒãã¯ä»¥äžã®ããã«ãªã:
400 struct q {
401 struct spinlock lock;
402 void *ptr;
403 };
404
405 void*
406 send(struct q *q, void *p)
407 {
408 acquire(&`q->lock`);
409 while(q->ptr != 0)
410 ;
411 q->ptr = p;
412 wakeup(q);
413 release(&`q->lock`);
414 }
415
416 void*
417 recv(struct q *q)
418 {
419 void *p;
420
421 acquire(&`q->lock`);
422 while((p = q->ptr) == 0)
423 sleep(q, &`q->lock`);
424 q->ptr = 0;
425 release(&`q->lock`);
426 return p;
427 }
recv
ãq->lock
ãä¿æããããšã«ãã£ãŠãsend
ãrecv
ãq->ptr
ããã§ãã¯ããsleep
ãåŒã¶åã«èµ·ããããšããããšãé²ãããã¡ãããåä¿¡åŽã®ããã»ã¹ã¯ã¹ãªãŒãäžã¯q->lock
ã解æŸããªããã°ãªãããåŸã£ãŠéä¿¡è
ã¯èµ·ããããšãã§ããã
åŸã£ãŠãq->lock
ãã¢ãããã¯ã«è§£æŸãã¹ãªãŒãç¶æ
ã«å
¥ãããã«ãåä¿¡è
ã®ããã»ã¹ãèµ·ãããŠããã¹ãªãŒãç¶æ
ã«å
¥ãããšãã§ããã
xv6ã®sleep
ãšwakeup
ã®å®è£
ãèŠãŠã¿ãããåºæ¬çãªã¢ã€ãã¢ã¯ãsleep
ã¯çŸåšã®ããã»ã¹ãSLEEPING
ç¶æ
ã«èšå®ãsched
ãåŒã³ãããã»ããµã解æŸãã; wakeup
ã¯waitãã£ãã«äžã®ã¹ãªãŒãç¶æ
ã®ããã»ã¹ãæ¢ããRUNNABLE
ã«èšå®ããã
sleep
(2803è¡ç®)ã¯ããã€ãã®ãã§ãã¯ããå§ãŸã: ãŸããçŸåšã®ããã»ã¹ã§ããå¿
èŠããããsleep
ã¯ããã¯ãæž¡ãããŠãªããã°ãªããªã(2808-2809è¡ç®)ã次ã«ãsleep
ã¯ptable.lock
ãç²åŸãã(2818è¡ç®)ãããã§ãããã»ã¹ã¯ptable.lock
ãšlk
ãç²åŸããã®ã§ãã¹ãªãŒãç¶æ
ã«å
¥ãããšãã§ãããlk
ãç²åŸããŠããã®ã¯ãåŒã³åºãå
ã«ãšã£ãŠå¿
èŠ(äŸãã°recv
ããã»ã¹)ã§ãã: ããã¯ãä»ã®ããã»ã¹(äŸãã°ãåäœããŠããsend
ããã»ã¹)ãwakeup(chan)
ãåŒã³åºãå§ããŠã¯ããªãããšããããšãä¿èšŒãããã®ã§ãããä»ãsleep
ãptable.lock
ãä¿æããŠãããlk
ã解æŸããŠãå®å
šã§ãã: ä»ã®ããã»ã¹ã¯wakeup(chan)
ãåŒã³åºãå§ããŠãè¯ãããwakeup
ã¯ptable.lock
ãç²åŸã§ãããŸã§åäœããªããåŸã£ãŠãããã»ã¹ãã¹ãªãŒãç¶æ
ã«èšå®ããŠããsleep
ãçµäºããŠããwakeup
ãsleep
ã倱ãããšãé²ãã§ããã
ããã€ãã®è€éãªéšåãååšãã: lk
ã¯&ptable.lock
ãšåäžãªãã°ãsleep
ã¯&ptable.lock
ãç²åŸããããšããŠã次ã«lk
ã解æŸããããšããããããããããã¯ã«ãªãããã®ãããªå Žåã«ã¯ãsleep
ã¯ç²åŸãšè§£æŸããããšãæ¢ããå
šäœãã¹ããããã(2817è¡ç®)ãäŸãã°ãwait(2653è¡ç®)ã¯sleep
ã&ptable.lock
ãåŒæ°ã«ããŠåŒã³åºãã
ããã«ããã€ãã¹ããããé²ããŠãããšãããã»ã¹ã¯wakeup(chan)
ãåŒã³åºããwakeup
(2853è¡ç®)ã¯ptable.lock
ãç²åŸããwakeup1
ãåŒã³åºããwakeup1
ãå®éã«ä»äºãããé¢æ°ã§ãããwakeup
ãptable.lock
ãç²åŸããŠããããšãéèŠã§ãããããã¯ãã®é¢æ°ãããã»ã¹ã®ç¶æ
ãæäœãããŸãããããŸã§ã«èŠãŠããããã«ãptable.lock
ãsleep
ãšwakeup
ãäºãã«ãã¹ãããªãããã«ä¿èšŒãããã®ã ããã§ãããwakeup1
ã¯åé¢ããé¢æ°ã§ãããã¹ã±ãžã¥ãŒã©ããã°ãã°ptable.lock
ãæ¢ã«ç²åŸãããŸãŸwakeup
ãå®è¡ããããšããããã§ãã; ãã®äŸã«ã€ããŠã¯ãåŸã«èª¬æãããwakeup1
(2853è¡ç®)ã¯ããã»ã¹ããŒãã«ãé ã«æ¢çŽ¢ãããSLEEPING
ç¶æ
ã§ãchanãšãããã³ã°ããããã»ã¹ãçºèŠãããšãããã»ã¹ã®ç¶æ
ãRUNNABLE
ã«å€æŽããã次ã«ã¹ã±ãžã¥ãŒã©ãå®è¡ããããšãã«ã¯ãããã»ã¹ãå®è¡å¯èœãªç¶æ
ãšããŠèŠããŠããã®ã§ããã
wakeup
ã¯ãwakeup
ãã©ã®ãããªç¶æ
ã§ãã£ããšããŠããã¬ãŒãå€æ°ãããã¯ããŠããç¶æ
ã§åŒã°ããªããã°ãªããªã; äŸãšããŠãããã¯ãq->lock
ã§ãã£ããšããããäœæ
ã¹ãªãŒãç¶æ
ã®ããã»ã¹ãwakeup
ããã¹ããã®ããšãããšãã¹ãªãŒãç¶æ
ã«å
¥ãåã®å
šãŠã®ã¿ã€ãã³ã°ã§æ¡ä»¶ããã§ãã¯ãããã®æ¡ä»¶ããptable.lock
ã®ã©ã¡ãã(ãããã¯ãã®äž¡æ¹)ãããã¯ããŠããããã§ãããwakeup
ã¯ãããã®ããã¯ãä¿æãããŸãŸå®è¡ãããããwakeup
ã¯æœåšçãªã¹ãªãŒãç¶æ
ã®ããã»ã¹ãç¶æ
ããã§ãã¯ããåããæœåšçãªã¹ãªãŒãç¶æ
ã®ããã»ã¹ãã¹ãªãŒãç¶æ
ã«å
¥ã£ããã©ããããã§ãã¯ããåŸã«å®è¡ãããªããã°ãªããªãã
è€æ°ã®ããã»ã¹ãåäžã®ãã£ãã«ã§ã¹ãªãŒãç¶æ
ã«ãªã£ãŠããå¯èœæ§ããã; äŸãã°ã1ã€ä»¥äžã®ããã»ã¹ãpipeããèªã¿èŸŒã¿ãããããšããŠããå Žåã§ãããäžåã®wakeup
ã®åŒã³åºãã«ããããããã®ããã»ã¹ãå
šãŠèµ·ããããã®ãã¡ã®äžã€ããsleep
ããåŒã³åºãããããã¯ãç²åŸãã(ãã€ãã®å Žåã¯)ãã€ãäžã«æžã蟌ãŸããŠããããŒã¿ãåŒã³åºããä»ã®ããã»ã¹ã¯ãããçºèŠãããšãããã»ã¹ãèµ·ãããã«ãé¢ããããäœãèªãããšãã§ããªããwakeup
ããåœè£
çãªããã®ã§ãããšãã芳ç¹ããããããã®ããã»ã¹ã¯åã³ã¹ãªãŒãç¶æ
ã«å
¥ããªããã°ãªããªãããã®ãããªçç±ããsleep
ã¯ããã»ã¹ã®ç¶æ
ããã§ãã¯ããã«ãŒãã®äžã§åžžã«åŒã³åºãããã
sleep
ãšwakeup
ã®åŒã³åºãå
ã¯ãä»»æã®æ°åããã£ãã«çªå·ãšããŠå©çšããããšãã§ãã; å®éã«ã¯ãxv6ã¯ãã£ã¹ã¯ãããã¡ãªã©ã®åŸ
ã¡ç¶æ
ã«ãªã£ãŠããã«ãŒãã«ã®ããŒã¿æ§é ã®ã¢ãã¬ã¹ãå©çšããã2ã€ã®sleep
/wakeup
ã®ãã¢ãåããã£ãã«çªå·ãå©çšãããšããŠããåé¡ã¯çºçããªã: ãããã¯åœè£
ããwakeup
ãè¡ããããã®åé¡ã蚱容ããããã«ã«ãŒãåŠçãèšè¿°ããŠãããsleep
ãšwakeup
ã®é
åã®å€ãã¯ã¯ã軜éã§ããããš(ã¹ãªãŒããã£ãã«ãåäœãããããã«ç¹å¥ãªããŒã¿æ§é ãå¿
èŠç¡ã)ãšãéæ¥çãªã¬ã€ã€(åŒã³åºãå
ã¯ãçžäºã«éä¿¡ãããŠããå
ã®ããã»ã¹ã«ã€ããŠç¥ãå¿
èŠãç¡ã)ãæäŸããŠãããšããããšã§ããã
åç« ã«ãŠæ±ã£ãåçŽãªãã€ãã¯ããã¡ãã§ãã£ãããxv6ã¯å®éã«sleep
ãšwakeup
ã«ããèªã¿èŸŒã¿å
ãšæžã蟌ã¿å
ã®åæãè¡ããã¥ãŒã2ã€ååšããã
1ã€ãIDEãã©ã€ãã§ãã: ããã»ã¹ããã£ã¹ã¯ã®ãªã¯ãšã¹ãããã¥ãŒã«æ¿å
¥ããsleep
ãåŒã³åºãã
å²ã蟌ã¿ãã³ãã©ãwakeup
ãå©çšããŠããã»ã¹ã«å¯ŸããŠãªã¯ãšã¹ããå®äºããããšãéç¥ããã
ããè€éãªäŸãšããŠããã€ãã®å®è£ ãããããã€ãã®ã€ã³ã¿ãã§ãŒã¹ã«ã€ããŠã¯ã第0ç« ã§èŠã: ãã€ãã®çµç«¯ã«æžã蟌ãŸãããã€ããã«ãŒãã«ãããã¡ã«ã³ããŒãããä»ã®ãã€ãã®çµç«¯ããèªã¿åºãããã 以éã®ç« ã§ã¯ããã¡ã€ã«ã·ã¹ãã ããã€ãåšèŸºããµããŒãããããããã§ã¯pipewriteãšpipereadã®å®è£ ã«ã€ããŠèŠãŠè¡ããã
åãã€ãã¯struct pipeãšããŠè¡šçŸãããlockãšdataãããã¡ãåããŠããã nreadãšnwriteã¯ãããã¡äžããèªã¿èŸŒã¿ãæžã蟌ã¿ããããéãã«ãŠã³ãããŠããããã®ãããã¡ã¯ã©ããã¢ã©ãŠã³ããã: buf[PIPESIZE-1]ã®æ¬¡ã«æžã蟌ãŸãããããã¡ã®äœçœ®ã¯buf[0]ã§ããããã«ãŠã³ã¿ã¯ã©ããã¢ã©ãŠã³ãããªãã ãã®èšæ³ã§ã¯ããããã¡ããã«ã§ããããšã¯ã(nwrite==nread+PIPESIZE)ã®æ¡ä»¶ã§ãã§ãã¯ãè¡ãããããã¡ã空ã§ãããšãnwrite==nreadã§ããããšãåºå¥ããããšãã§ãããããããã¡ã®ã€ã³ããã¯ã¹ã¯ buf[nread]ã®ä»£ããã«ãbuf[nread%PIPESIZE]ã§åç §ããå¿ èŠããããnwriteã®å Žåãåæ§ã§ããã pipelineãšpipereadã¯åæã«ã2ã€ã®ç°ãªãCPUã§çºçãããšä»®å®ãããã
pipewrite(6530è¡ç®)ã¯ãã€ãã®ããã¯ãç²åŸãããšããããå§ãŸããã«ãŠã³ã¿ãšããŒã¿ãããã¯ããŠãé¢é£ããäžå€æ¡ä»¶ã確ä¿ããã
piperead(6551è¡ç®)ã¯åæ§ã«ããã¯ãç²åŸããããšããããããã¯ã§ããªãã
acquire(1571è¡ç®)ã®äžã§ã¹ãã³ãè¡ããããã¯ãåŸ
ã€ã
pipereadãåŸ
ã£ãŠããéãpipewriteã¯æžã蟌ããã€ããèªã¿åºããªããã«ãŒããã - addr[0],addr[1],...addr[n-1] - ããããã®ããŒã¿ããã€ãã«æžã蟌ã(6544è¡ç®)ã
ãã®ã«ãŒãã®éããããã¡ãäžæ¯ã«ãªãå¯èœæ§ããã(6536è¡ç®)ã
ãã®å Žåã«ã¯ãpipewriteã¯wakeup
ãå®è¡ããã¹ãªãŒãç¶æ
ã«ãªã£ãŠããèªã¿èŸŒã¿å
ã®ããã»ã¹ãèµ·ããããããã¡äžã«ããŒã¿ãæºã£ãŠããããšãéç¥ãã&p->nwriteäžã§ã¹ãªãŒãç¶æ
ã«å
¥ããèªã¿èŸŒã¿ããã»ã¹ããããã¡ãããããèªã¿åºãã®ãåŸ
ã€ã
sleep
ã¯pipewriteã®ããã»ã¹ãã¹ãªãŒãç¶æ
ã«å€æŽããåŠçã®äžã§ãp->lockã解æŸããã
次ã«ãp->lockãç²åŸå¯èœã«ãªããšãpipereadã¯ããã¯ãç²åŸããŠãããæ¶è²»ãå§ãã: p->nread!=p->nwriteã§ããããšãçºèŠãããš(6556è¡ç®)(pipewriteã¯p->nwrite==p->nread+PIPESIZE(6536è¡ç®)ã§ãããããã¹ãªãŒãç¶æ
ã«å
¥ã£ãŠãã)ã
ã«ãŒããéããŠããã€ãããããŒã¿ãã³ããŒã(6563-6567è¡ç®)ãnreadãã€ã³ã¯ãªã¡ã³ãããŠã³ããŒãããã€ãæ°ãã«ãŠã³ãããã
ããããŠäœãã€ããæžã蟌ã¿ãå¯èœãªç¶æ
ã«ãªããšãpipereadã¯wakeup
ãåŒã³åºããŠ(6568è¡ç®)ãã¹ãªãŒãç¶æ
ã®æžã蟌ã¿ããã»ã¹ãåŒã³åºããåŒã³åºãå
ã«æ»ãã
wakeup
ã¯ãããã¡ãäžæ¯ã®ããã¹ãªãŒãç¶æ
ã«å
¥ã£ãŠããpipewriteãå®è¡ããŠããããã»ã¹ãçºèŠãããã®ããã»ã¹ãRUNNABLE
ã«èšå®ããã
ãã€ãã®ã³ãŒãã¯èªã¿èŸŒã¿ãšæžã蟌ã¿ã«å¥ã ã®ã¹ãªãŒããã£ãã«ãå©çšãã(p->nreadãšp->nwrite); ããã«ããã·ã¹ãã ã¯äºæããªãã€ãã³ãã«å¯ŸããŠããå¹ççã«å®è¡ããããšãã§ããããã«ãªãã å€ãã®èªã¿èŸŒã¿ããã»ã¹ãšæžã蟌ã¿ããã»ã¹ãååšããŠããŠãå®è¡ã§ããããã«ãªãã ãã€ãã®ã³ãŒãã¯ã¹ãªãŒãç¶æ ããã§ãã¯ããã«ãŒãã®äžã§ã¹ãªãŒãç¶æ ã«å ¥ã; ããè€æ°ã®èªã¿èŸŒã¿ããã»ã¹ãšæžã蟌ã¿ããã»ã¹ãååšãããšãå šãŠã®ããã»ã¹ã®äžã§1ã€ç®ã®ããã»ã¹ãèµ·ãäžãããç¶æ ããŸã falseã§ããããšã確èªããŠåã³ã¹ãªãŒãç¶æ ã«ãªãã
sleep
ãšwakeup
ã¯ãå€ãã®åŸ
ã¡åäœã«çšããããã
é¢çœãäŸãšããŠã第0ç« ã§ã®waitã·ã¹ãã ã³ãŒã«ã«ããã芪ããã»ã¹ãåããã»ã¹ãçµäºããã®ãåŸ
ã€ã·ã¹ãã ã³ãŒã«ãããã
xv6ã§ã¯ãåããã»ã¹ãçµäºãããšãããã¯å³æã«æ»ã¬èš³ã§ã¯ãªãã
代ããã«ãããã»ã¹ã¯ããŸã³ãããã»ã¹(ZOMBIE)ããšããããã»ã¹ç¶æ
ã«å€æŽããã芪ããã»ã¹ãçµäºããããã®waitãåŒã³åºããŸã§åŸ
ã€ã
芪ããã»ã¹ã¯åããã»ã¹ã®ã¡ã¢ãªé åã解æŸããstruct procãåå©çšããããã®æºåããã責任ãæã€ã
ãã芪ããã»ã¹ãåããã»ã¹ãçµäºããåã«çµäºãããšãinitããã»ã¹ãåããã»ã¹ã®çµäºãåŸ
ã€ã
åŸã£ãŠãå
šãŠã®åããã»ã¹ã¯çµäºæã®ã¯ãªãŒã³ã¢ãããããããã«ã芪ããã»ã¹ãæã£ãŠããã
exitãšexitã®ããã»ã¹ã®éã«ã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ãååšããããã«ãåããã»ã¹ãšèŠªããã»ã¹ã®waitãšexitã®ã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ã®å¯èœæ§ãããããšã«æ³šæãããã
waitã¯ãŸããptable.lock
ãç²åŸãããšããããå§ãŸããã
次ã«ããã»ã¹ããŒãã«ãæ¢çŽ¢ããŠåããã»ã¹ãçºèŠããã
waitãçŸåšã®ããã»ã¹ãåããã»ã¹ãæã£ãŠãããããŸã çµäºããŠããªãå Žåãsleep
ã·ã¹ãã ã³ãŒã«ãåŒã³åºããŠãåããã»ã¹ã®äžã€ãçµäºããã®ãåŸ
ã€(2689è¡ç®)ã¡ããããçµäºãããšããã«ã¹ãã£ã³ãè¡ãã
ããã§ãsleep
å
ã§è§£æŸãããããã¯ã¯ptable.lock
ã§ããããã®ãšãã®ç¹æ®ã±ãŒã¹ã«ã€ããŠã¯äžèšã§èª¬æããã
exitã¯ptable.lock
ããã¯ãç²åŸããwaitãã£ãã«äžã§ç ã£ãŠããä»»æã®ããã»ã¹ãèµ·ãããããã¯çŸåšã®èŠªããã»ã¹ã®procã«çžåœãã(2628è¡ç®);
ãããã®ãããªããã»ã¹ãååšããã°ãããã¯waitã«ããåŸ
ã¡ç¶æ
ã«ãªã£ãŠãã芪ããã»ã¹ã§ããã
ããã§ã¯ãŸã æºåã¯æªå®äºã®ããã«èŠãããäœæ
ãªãã°ããŸã exitã¯çŸåšã®ããã»ã¹ãZOMBIEç¶æ
ã«å€æŽããŠããªãããã ãããŸã å®å
šã§ãã:
wakeup
ã¯èŠªããã»ã¹ãRUNNABLE
ãªç¶æ
ã«ããããwaitå
ã§ã®ã«ãŒãã¯exitãptableã解æŸãããŸã§ã¯å®è¡ããããšãã§ããªãã
ptable.lock
ã¯sched
ã«ããã¹ã±ãžã¥ãŒã©ã«å
¥ã£ã段éã§è§£æŸãããããåŸã£ãŠwaitã¯exitãZOMBIEç¶æ
ã«ããã»ã¹ãå€æŽãããŸã§ã該åœããçµäºããã»ã¹ãçºèŠããããšãã§ããªãã
exitãåã¹ã±ãžã¥ãŒãªã³ã°ãããåã«ãçµäºããããã»ã¹ã®å
šãŠã®åããã»ã¹ã¯ã芪ã®åèšå®ãè¡ãããinitproc
ã«æž¡ããã(2630-2637è¡ç®)ã
æåŸã«exitã¯sched
ãåŒã³åºãCPUãææŸãã
芪ããã»ã¹ãwaitäžã§å¯ãŠãããªãã°ããããã¯ã¹ã±ãžã¥ãŒã©ã¯åäœããŠããããšã«ãªãã
sleep
ãåŒã³åºãããšã«ãããptable.lock
ãä¿æããŠããããšã«ãªã; waitã¯ããã»ã¹ããŒãã«ãã¹ãã£ã³ããstate==ZOMBIEãªåããã»ã¹ãæ¢ã(2634è¡ç®)ã
åããã»ã¹ã®pidãèšé²ããstruct procãã¯ãªãŒã³ã¢ãããããã®ããã»ã¹ã®é¢é£ããã¡ã¢ãªé åã解æŸãã(2668-2676è¡ç®)ã
åããã»ã¹ã¯exitäžã«æ®ã©ã®ã¯ãªãŒã³ã¢ãããçµäºããããšãã§ãããã芪ããã»ã¹ãp->kstackãšp->pgdirã解æŸããããšã¯éèŠã§ãã:
åããã»ã¹ãexitãå®è¡ãããšããã®ããã»ã¹ã®ã¹ã¿ãã¯ã¯p->kstackã«å²ãåœãŠãããã¡ã¢ãªäžã«é
眮ãããŠãããããã¯èªåèªèº«ã®ããŒãžããŒãã«ãå©çšããã
ããã¯ãåããã»ã¹ãå®è¡ãçµäºãã(sched
ã«ãã)swtch
ãåŒã³åºãå Žåã«æåŸã«ãã解æŸããããšãã§ããªã(xxx)ã
ããããã¹ã±ãžã¥ãŒã©ãsched
ã«ããåŒã°ããã¹ã¬ããã®ã¹ã¿ãã¯äžã§åäœããã®ã§ã¯ãªããèªåèªèº«ã®ã¹ã¿ãã¯äžã§åäœããçç±ã§ããã
exitãããã»ã¹èªèº«ãçµäºããããšãèš±å¯ãããšãkill(2875è¡ç®)ãããããã»ã¹ããªã¯ãšã¹ãããxxxã killã«ããçŽæ¥ç ç²ãšãªãããã»ã¹ãç Žå£ããã®ã¯éåžžã«è€éãªåŠçã§ããã äœæ ãªãã°ãç ç²ãšãªãããã»ã¹ã¯ä»ã®CPUã§åäœããŠããããã«ãŒãã«ããŒã¿æ§é ãæžãæããŠããæäžã«ã¹ãªãŒãç¶æ ã«ãªã£ãŠãããªã©ã®å¯èœæ§ãããããã§ããã ãããã®è€éæ§ãåé¿ããããã«ãkillã¯éåžžã«å°ããªæ§é ã«ãªã£ãŠãã: åã«ç ç²ãšãªãããã»ã¹ã®p->killedãèšå®ããããããã¯ã¹ãªãŒãç¶æ ãªãã°ããããèµ·ããã ããã«ç ç²ãšãªãããã»ã¹ãã«ãŒãã«ããé¢ãããšããã®ãã€ã³ãã§trapãå®è¡ãããp->killedãèšå®ãããŠãããªãã°ãexitãåŒã³åºãããã ããç ç²ãšãªãããã»ã¹ããŠãŒã¶ç©ºéäžã«ååšãããªãã°ãã·ã¹ãã ã³ãŒã«ããã¹ã¯ããããã¿ã€ãå²ã蟌ã¿ã«ãã£ãŠ(ãããã¯ä»ã®ããã€ã¹ã®å²ã蟌ã¿ã«ãã)ããããŸã«ãŒãã«ç¶æ ã«å ¥ãã
ç ç²ãšãªãããã»ã¹ãã¹ãªãŒãç¶æ
ãªãã°ãwakeup
ã®åŒã³åºãã«ãããã®ããã»ã¹ã¯sleep
ããæ»ã£ãŠããã
ããã¯äœããæç«ãããŸã§åŸ
ã¡ç¶æ
ã§ãããªãã°ãæç«ããããšãªãèµ·ãäžããããšã«ãªããããæœåšçã«å±éºã§ããã
ããããxv6ã¯åžžã«sleep
ãåŒã³åºããšãã¯whileã«ãŒãã«ããã©ããããŠãããããsleep
ããæ»ã£ãŠãããšãã¯åžžã«æ¡ä»¶ããã¹ãããŠããã
ããã€ãã®sleep
ã®åŒã³åºãã¯p->killedãã«ãŒãäžã§ãã¹ãããŠãããããã»ãããããŠãããªãã°ãçŸåšã®åäœããã¹ãŠç Žæ£ããã
ãã®ãããªç Žæ£ã®æäœãè¡ããããæ£ããå¯äžã®ã±ãŒã¹ã§ããã
äŸãã°ããã€ãã®èªã¿æžãã®ã³ãŒã(6537è¡ç®)ã¯ãkilledãã©ã°ãç«ã¡äžãã£ãŠãããšæ»ã£ãŠãã;
ãããã¯ã³ãŒãã¯trapã«æ»ã£ãŠããŠãåã³ãã©ã°ããã§ãã¯ããŠçµäºããã
xv6ã®ããã€ãã®sleep
ã®ã«ãŒãã¯p->killedããã§ãã¯ããŠããããããã¯è€æ°ã®ã·ã¹ãã ã³ãŒã«ãã¢ãããã¯ã§ãããããªã³ãŒããå
¥ã£ãŠããããšã«ããã
IDEãã©ã€ã(4279è¡ç®)ããã®äŸã§ãã: ãã®ã«ãŒãã¯p->killedããã§ãã¯ããŠãããããã£ã¹ã¯ã®æäœã¯å
šãŠããã¡ã€ã«ã·ã¹ãã äžã§ã€ã³ãªãŒãã§å®è¡ãããæ£ããç¶æ
ã§å°±åŽããªããŠã¯ãªããªãããã§ããã
ã¯ãªãŒã³ã¢ãããå®è¡ãããŠäžéšåã®æäœãæ®ã£ããšãã®è€éæ§ãåé¿ããããã«ãxv6ã¯ããã»ã¹ã®ãã«æäœãé
ãããIDEãã©ã€ããããã»ã¹ã殺ããŠãè¯ããã€ã³ãã«å°éãããŸã§ãã«ãåé¿ãã
(äŸãã°ããã¡ã€ã«ã·ã¹ãã ã®æäœãå®äºããããã»ã¹ããŠãŒã¶ç©ºéã«æ»ã£ãŠããæãªã©)ã
xv6ã®ã¹ã±ãžã¥ãŒã©ã¯ã·ã³ãã«ãªã¹ã±ãžã¥ãŒãªã³ã°ã®ããªã·ã§å®è£ ãããŠãããåããã»ã¹ãé çªã«å®è¡ããŠããä»çµã¿ã«ãªã£ãŠããã ãã®ããªã·ããã©ãŠã³ãããã³ããšèªãã§ããã å®éã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ããæŽç·Žãããããªã·ãå®è£ ããŠãããäŸãã°ãããã»ã¹ãåªå 床ãæã€ããšãåºæ¥ãããã«ãªã£ãŠããã ãã®ã¢ã€ãã¢ã¯ãå®è¡å¯èœãªããé«ãåªå 床ãæã£ãŠããããã»ã¹ããã¹ã±ãžã¥ãŒã©äžã§ã¯ããäœãåªå 床ã®ããã»ã¹ãããåªå ããŠéžæããããšããæ¹éã§ããã ãããã®ããªã·ã«ãããæ¥æ¿ã«å®è£ ãè€éã«ãªãå¯èœæ§ããããããã¯ããé«åºŠãªèŠæ±ãæºããå¿ èŠãããããã§ãã: äŸãã°ããªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯å ¬å¹³æ§ãšé«ã¹ã«ãŒããããä¿èšŒããå¿ èŠãããã å ããŠãè€éãªããªã·ã«ããããåªå 床ã®é転ããããã³ã³ãã€ããšãã£ãäºæããªãçžäºäœçšãçºçããå¯èœæ§ãããã åªå 床ã®é転ã¯ãäœãåªå 床ã®ããã»ã¹ãšé«ãåªå 床ã®ããã»ã¹ãããã¯ãå ±æããŠãããäœãåªå 床ã®ããã»ã¹ãããã¯ãç²åŸãããšã é«ãåªå 床ã®ããã»ã¹ãå®è¡ã§ããªããªã£ãŠããŸãããšã§ããã é·ãã³ã³ãã€ã¯ãå€ãã®é«ãåªå 床ã®ããã»ã¹ãäœãåªå 床ã®ããã»ã¹ãç²åŸããŠããå ±æããã¯ã®åŸ ã¡åãããããŠããéã«çºçãã; äžåºŠã³ã³ãã€ãçºçãããšãåªå 床ã®é«ãããã»ã¹ã¯é·ãæéèããå¿ èŠãããã ãã®ãããªåé¡ãé¿ããããã«ã¯ãæŽç·Žãããã¹ã±ãžã¥ãŒã©ã®äžã«ãããã«ã¡ã«ããºã ãè¿œå ããå¿ èŠãããã
sleep
ãšwakeup
ã¯ã·ã³ãã«ã§å¹æçãªæ¹æ³ã§ããããä»ã«ãæ§ã
ãªæ¹æ³ãååšããã
æåã«ææŠããããšã¯ãæ¬ç« ã®æåã§èŠãããã¹ããwakeup
ãã®åé¡ãé¿ããããšã§ããã
ãªãªãžãã«ã®Unixã«ãŒãã«ã§ã¯ãsleep
ã¯åçŽã«å²ã蟌ã¿ãçŠæ¢ã«ããŠãããUnixã¯åäžã®CPUã§åäœããããããã§ååã§ãã£ãã
xv6ã¯ãã«ãããã»ããµäžã§åäœãããããæ瀺çãªããã¯ã«ããsleep
ãããå¿
èŠããã£ãã
FreeBSDã®msleep
ã¯ãåæ§ã®ã¢ãããŒããåã£ãŠããã
Plan 9ã®sleep
ã¯ã¹ãªãŒãç¶æ
ã«å
¥ãçŽåã«ã¹ã±ãžã¥ãŒãªã³ã°ã«ããããã¯ãç²åŸããããã®ã³ãŒã«ããã¯é¢æ°ãå©çšããŠãã;
ãã®é¢æ°ã¯ãã¹ããwakeup
ãé¿ããããã«ãæåŸã®çŽåã®ã¹ãªãŒãç¶æ
ã®ãã§ãã¯ãå®è¡ããŠããã
Linuxã«ãŒãã«ã®sleep
ã¯waitãã£ãã«ã®ä»£ããã«ãæ瀺çãªããã»ã¹ã®ãã¥ãŒãå©çšããŠãã; ãã®ãã¥ãŒã¯ãèªåèªèº«ã®å
éšããã¯ãæã£ãŠããã
wakeup
å
ã§ã®å
šäœã®ããã»ã¹ãã¹ãã£ã³ããŠãchanãšãããã³ã°ããããšã¯éå¹çã§ããã
ããå¹ççãªæ¹æ³ãšããŠãsleep
ãšwakeup
å
ã®chanãã¹ãªãŒãç¶æ
ã®ããã»ã¹ã®ãªã¹ããšçœ®ãæããæ¹æ³ãããããã
Plan 9ã®sleep
ãšwakeup
ã¯ãã®ãããªæ§é ãåã£ãŠãããéåãã€ã³ããããã¯rendezãšããæ§é äœãä¿æããŠããã
å€ãã®ã¹ã¬ããã©ã€ãã©ãªã¯åæ§ã®æ§é äœããæ¡ä»¶å€æ°ãåç
§ããŠãã; ãã®ãããªã³ã³ããã¹ãã§ã¯ãsleep
ãšwakeup
ã®æäœã¯waitãšsignalããåŒã³åºãããã
å
šãŠã®ãã®ãããªã¡ã«ããºã ã¯åæ§ã®ç¹åŸŽãæã£ãŠãã: ã¹ãªãŒãã®æ¡ä»¶ã¯ãã¹ãªãŒãäžã«ã¢ãããã¯ã«ãããããããããã€ãã®ããã¯ã«ãã£ãŠä¿è·ãããŠããã
wakeup
ã®å®è£
ã«ãããç¹å®ã®ãã£ãã«ãåŸ
ã¡åãããŠããå
šãŠã®ããã»ã¹ãèµ·ãäžãããå€ãã®ããã»ã¹ãç¹å®ã®ãã£ãã«ãåŸ
ã£ãŠããç¶æ
ãçºçããã
ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ãã¹ãªãŒãç¶æ
ããã§ãã¯ããŠããå€ãã®ããã»ã¹ãã¹ã±ãžã¥ãŒãªã³ã°ããã
ãã®ãããªæ¹æ³ã§åäœããããã»ã¹ã¯thundering headãšåŒã°ããããã¯æãé¿ãããããã®ã§ããxxxã
æ®ã©ã®æ¡ä»¶å€æ°ã¯ãwakeup
ã®ããã®2ã€ã®ããªããã£ããæã£ãŠãã:1ã€ã®ããã»ã¹ãèµ·ããããã®signalãšãå
šãŠã®åŸ
ã¡ç¶æ
ã®ããã»ã¹ãèµ·ããããã®broadcastã§ããã
ã»ããã©ã¯ãããäžã€ã®èª¿åã®ããã®ã¡ã«ããºã ã§ããã
ã»ããã©ã¯2ã€ã®æäœã«ããæŽæ°ãã€ã³ã¯ãªã¡ã³ããšãã¯ãªã¡ã³ãããã
ã»ããã©ã¯ã€ã³ã¯ãªã¡ã³ãããããšãã§ããããã»ããã©ã®å€ãšããŠããã¯ãªã¡ã³ãããŠ0ãäžåãããšã¯èš±ãããªãã
ãŒãããã¯ãªã¡ã³ããããšãä»ã®ããã»ã¹ãã»ããã©ãã€ã³ã¯ãªã¡ã³ããããŸã§ã¯ã¹ãªãŒãç¶æ
ã«ãªãããããã®2ã€ã®æäœã¯å
šãŠãã£ã³ã»ã«ãããã
æŽæ°å€ã¯å
žåçã«ãå®éã®å€ãäŸãã°ãã€ããããã¡äžã®ããŒã¿ã®ãã€ãæ°ããããã»ã¹ãä¿æããŠãããŸã³ãç¶æ
ã®ããã»ã¹ã®æ°ã«çžåœããã
æ瀺çãªã«ãŠã³ã¿ã®æœè±¡åã«ãããããã¹ããwakeup
åé¡ããé¿ããããšãã§ãã: æ瀺çã«wakeup
ã®æ°ãæ°ããã®ã§ããã
ãã®ã«ãŠã³ã¿ã«ãããäžæãªwakeup
ããthundering herdåé¡ãé¿ããããšãã§ããã
xv6äžã§ã¯ãããã»ã¹ã殺ããããã¯ãªãŒã³ã¢ããããããšã«ãããããè€éãªåé¡ãçããã æ®ã©ã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã§ã¯ããã«è€éã§ããããäœæ ãªãã°ãäŸãã°ãç ç²ãšãªãããã»ã¹ãã«ãŒãã«ã®æ·±ããšããã§ã¹ãªãŒãç¶æ ã§ãããšããŠã ãã®ã¹ã¿ãã¯ãå·»ãæ»ãããã«ã¯ã泚ææ·±ãããã°ã©ãã³ã°ãå¿ èŠã§ããã å€ãã®ãªãã¬ãŒãã£ã³ã°ã·ã¹ãã ã¯ãlongjmpãªã©ã®äŸå€ãã³ããªã³ã°ã«ããæ瀺çãªã¡ã«ããºã ã䜿ã£ãŠã¹ã¿ãã¯ãå·»ãæ»ããŠããã ããã«ãåŸ ã£ãŠããã€ãã³ããçããŠããªãã®ã«ãå¥ã®ã€ãã³ããçºçããããšã«ãã£ãŠã¹ãªãŒãããŠããããã»ã¹ãèµ·ãäžããããšãããã äŸãã°ãããã»ã¹ãã¹ãªãŒãç¶æ ã§ãããä»ã®ããã»ã¹ãsignalãéä¿¡ããå Žåã§ããã ãã®ç¶æ ã§ã¯ãããã»ã¹ãå²ã蟌ãŸããã·ã¹ãã ã³ãŒã«ã«ã-1ãæã£ãŠæ»ã£ãŠãããEINTRããšã©ãŒã³ãŒãã«èšå®ããã ã¢ããªã±ãŒã·ã§ã³ã¯ããã®å€ããã§ãã¯ããŠãäœããã¹ããã決å®ããã xv6ã¯ãã®ã·ã°ãã«ããµããŒãããããã®ãããªåé¡ã¯çºçããªãã
xv6ã¯å®å
šãªèŠä»¶ãæºããkillã¯ãµããŒãããŠããªã: ãããã¯ã¹ãªãŒãã«ãŒãã§ãããp->killedããã§ãã¯ããå¿
èŠãããã
é¢é£ããåé¡ãšããŠãsleep
ã®ã«ãŒããp->killedããã§ãã¯ããŠãããšããŠããsleep
ãškillã®ã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ãçºçãã;
åŸè
ãp->killedãèšå®ããŠç ç²ãšãªãããã»ã¹ãwakeup
ããããšããŠããã®çŽåã«ç ç²ãšãªãããã»ã¹ãp->killedããã§ãã¯ããŠsleep
ãåŒã³åºããå Žåã§ããã
ãã®åé¡ãçºçããå©ããç ç²ãšãªãããã»ã¹ã¯ãåŸ
ã£ãŠããæ¡ä»¶ãçºçããªããšãèªèº«ãp->killedãèšå®ãããŠããããšã«æ°ãã€ããªãã
ãã®åé¡ã¯åŸã«çºçãã(äŸãã°ãIDEãã©ã€ããç ç²ãšãªãããã»ã¹ãåŸ
ã£ãŠãããã£ã¹ã¯ã®æäœããåž°ã£ãŠãããšããªã©)ãã
(äŸãã°ãç ç²ãšãªãããã»ã¹ãã³ã³ãœãŒã«ã®åŸ
ã¡ç¶æ
ã ãããŠãŒã¶ãå
šãå
¥åãããªãã£ãå Žåãªã©)ã¯ãæ°žé ã«çºçããªãã
sleep
ã¯ãããããã¯ãé¿ããããã«ãlk
!=&ptable.lock
ããã§ãã¯ããå¿ èŠããã(2817-2820è¡ç®)ããã®ã³ãŒãã¯ã以äžã®ã®ããã«çœ®ãæããããšã§é€å»ããããšãã§ãã:
if(`lk` != &`ptable.lock`){
acquire(&`ptable.lock`);
release(`lk`);
}
ãã
release(`lk`);
acquire(&`ptable.lock`);
ããã«ããsleep
ãæããããããšãã§ãããïŒã©ã®ããã«ããŠå®çŸãããŠãããïŒ
2. æ®ã©ã®ããã»ã¹ã®ã¯ãªãŒã³ã¢ããã¯exitãããã¯waitã«ãã£ãŠå®è¡ãããããäžèšã«ãããexitã¯p->stackã解æŸããŠã¯ãªããªãã
ãŸãexitã¯éããŠãããã¡ã€ã«ãéããããšãã§ããå¯äžã®é¢æ°ã§ãããäœæ
ãïŒããã¯ãã€ãã«é¢é£ããåé¡ã§ããã
3. xv6ã«ãããŠãã»ããã©ãå®è£
ãããmutexãçšããããšãã§ããããsleep
ãšwakeup
ã¯äœ¿ã£ãŠã¯ãªããªãã
xv6ã®sleep
ãšwakeup
ãã»ããã©ã«çœ®ãæãããã®çµæãå€å®ããã
4. killãšsleep
ã®ã¬ãŒã¹ã³ã³ãã£ã·ã§ã³ãä¿®æ£ãããããã»ã¹ã®ã¹ãªãŒãã«ãŒãã¯p->killedããã§ãã¯ããsleep
ãåŒã³åºãåã«çºçãã
killã¯ãç ç²ãšãªãããã»ã¹ã¯ãçŸåšã®ã·ã¹ãã äžããåé€ãããçµæãšãªãã
5. å
šãŠã®ã¹ãªãŒãã«ãŒããp->killedããã§ãã¯ãããã¶ã€ã³ãèšèšããã
ããã«ããäŸãã°ãIDEãã©ã€ãããä»ã®ããã»ã¹ããã®ããã»ã¹ã殺ãããšãã«è¿
éã«æ»ã£ãŠãããããã«ãªã