Í\ûßJOS`,Û!¥J00848231,zhangchitc@gmail.comApril27,2011Contents1Introduction32User-levelEnvironmentCreationandCooperativeMultitasking32.1Round-RobinScheduling.......................32.2SystemCallsforEnvironmentCreation..............83Copy-on-WriteFork123.1User-levelpagefaulthandling....................123.1.1SettingthePageFaultHandler...............123.1.2NormalandExceptionStacksinUserEnvironments..133.1.3InvokingtheUserPageFaultHandler...........163.1.4User-modePageFaultEntrypoint.............193.1.5Testing.............................243.2ImplementingCopy-on-WriteFork.................254PreemptiveMultitaskingandInter-Processcommunication(IPC)324.1ClockInterruptsandPreemption..................324.1.1Interruptdiscipline......................324.1.2HandlingClockInterrupts.................344.2Inter-Processcommunication(IPC).................354.2.1IPCinJOS...........................354.2.2SendingandReceivingMessages..............354.2.3TransferringPages......................351Í\ûß`¥J,008482314.2.4ImplementingIPC......................362Í\ûß`¥J,008482311Introduction(-;ÂN-Ñ'fµ×ÜJOS`ü(µ;uïå~0F/Ù!üÜÜlab1üæ=@åÙÌ eE ¹ TG÷µ,à²IùíÕ²ãæÆ2User-levelEnvironmentCreationandCooperativeMultitaskingÙ*èMITc²ãÔæÆÆ2¥ýÔZÓãèÊwe/ð¾2.1Round-RobinSchedulingExercise1.Implementround-robinschedulinginsched_yield()asdescribedabove.Don’tforgettomodifysyscall()todispatchsys_yield().Modifykern/init.ctocreatethree(ormore!)environmentsthatallruntheprogramuser/yield.c.Youshouldseetheenvironmentsswitchbackandforthbetweeneachotherfivetimesbeforeterminating,likethis:...Hello,Iamenvironment00001001.Hello,Iamenvironment00001002.Hello,Iamenvironment00001003.Backinenvironment00001001,iteration0.Backinenvironment00001002,iteration0.Backinenvironment00001003,iteration0.Backinenvironment00001001,iteration1.Backinenvironment00001002,iteration1.Backinenvironment00001003,iteration1....Aftertheyieldprogramsexit,theidleenvironmentshouldrunandinvoketheJOSkerneldebugger.Ifanyofthisdoesnothappen,thenfixyourcodebeforeproceeding.schedyield()ýpÔUô¥4ãkern/sched.c:schedyield()1void2sched_yield(void)3{4structEnv*curenvptr=curenv;56if(curenv==NULL)7curenvptr=envs;89intround=0;3Í\ûß`¥J,0084823110for(curenvptr++;roundNENV;round++,curenvptr++){1112if(curenvptr=envs+NENV){13curenvptr=envs+1;14}1516if(curenvptr-env_status==ENV_RUNNABLE)17env_run(curenvptr);18}1920//Runthespecialidleenvironmentwhennothingelseisrunnable.21if(envs[0].env_status==ENV_RUNNABLE)22env_run(&envs[0]);23else{24cprintf(Destroyedallenvironments-nothingmoretodo!\n);25while(1)26monitor(NULL);27}28}6î9kern/syscall.cû øsÑ:66(kern/init.c-ûß/¨KúuseridleåúuseryieldÙ*(7ý1/\!sysyield()ûß(vböSpøso:kern/init.c:i386init()1//Shouldalwayshaveanidleprocessasfirstone.2ENV_CREATE(user_idle);3ENV_CREATE(user_yield);4ENV_CREATE(user_yield);5ENV_CREATE(user_yield);£H/¨JOSåSpúoèà:/(RoundRobinVeb@åzå/n qemu-hdaobj/kern/kernel.img-serialmon:stdio6828decimalis15254octal!Hooray!Passedalltestcasesforstdlib!!Physicalmemory:66556Kavailable,base=640K,extended=65532Kcheck_page_alloc()succeeded!page_check()succeeded!check_boot_pgdir()succeeded!enabledinterrupts:12Setuptimerinterruptsvia8259Aenabledinterrupts:012unmaskedtimerinterrupt[00000000]newenv00001000[00000000]newenv00001001[00000000]newenv00001002[00000000]newenv00001003Hello,Iamenvironment00001001.Hello,Iamenvironment00001002.Hello,Iamenvironment00001003.Backinenvironment00001001,iteration0.Backinenvironment00001002,iteration0.Backinenvironment00001003,iteration0.Backinenvironment00001001,iteration1.Backinenvironment00001002,iteration1.Backinenvironment00001003,iteration1.Backinenvironment00001001,iteration2.Backinenvironment00001002,iteration2.Backinenvironment00001003,iteration2.Backinenvironment00001001,iteration3.4Í\ûß`¥J,00848231Backinenvironment00001002,iteration3.Backinenvironment00001003,iteration3.Backinenvironment00001001,iteration4.Alldoneinenvironment00001001.[00001001]exitinggracefully[00001001]freeenv00001001Backinenvironment00001002,iteration4.Alldoneinenvironment00001002.[00001002]exitinggracefully[00001002]freeenv00001002Backinenvironment00001003,iteration4.Alldoneinenvironment00001003.[00001003]exitinggracefully[00001003]freeenv00001003WelcometotheJOSkernelmonitor!Type’help’foralistofcommands.QuestionInyourimplementationofenv_run()youshouldhavecalledlcr3().Beforeandafterthecalltolcr3(),yourcodemakesreferences(atleastitshould)tothevariablee,theargumenttoenv_run.Uponloadingthe%cr3register,theaddressingcontextusedbytheMMUisinstantlychanged.Butavirtualaddress(namelye)hasmeaningrelativetoagivenaddresscontext--theaddresscontextspecifiesthephysicaladdresstowhichthevirtualaddressmaps.Whycanthepointerebedereferencedbothbeforeandaftertheaddressingswitch?ìHeÞ~ envrun()ÌwSãkern/env.c:envrun()1void2env_run(structEnv*e)3{4if(curenv!=e){5curenv=e;6curenv-env_runs++;7lcr3(curenv-env_cr3);8}