Forking & Process SchedulingMechanicsA Quick ReviewHow To Launch a New Process?Can We Generalize?But We Want a Parent and a ChildWhat Gets CopiedShared MemoryCopy-On-WriteIssues of Copy-On-WriteProcess Creation & TerminationSignalsA Signaling SidebarScheduling PrimitivesOur Friend, The Transition DiagramProcess State Transition of Non-Preemptive SchedulingWho’s Happy Right NowSchedulerMore on SchedulerWhere Should PCB Be Saved?Physical Memory & MultiprogrammingJob SwappingAdd Job Swapping to State Transition DiagramThink About SwappingForking & Process SchedulingVivek Pai / Kai LiPrinceton University2MechanicsExams not graded–Hopefully, this weekFilesystem project–Extension + bonus?–This wasn’t supposed to happenToday: forking + scheduling3A Quick ReviewWhat have we covered–How to store data via files–How to virtualize memory•Every process gets uniform address space•Lots of tricks can be played to share memoryWhat’s left–How to share/virtualize the processor–Having processes communicate/cooperate4How To Launch a New Process?Obvious choice: “start process” system callBut not all processes start the same–“testprogram” versus “testprogram >& outfile” versus “testprogram arg1 arg2 >& outfile”The “parent” process wants to specify various aspects of the child’s “environment”–Next step: add more parameters to specify environment5Can We Generalize?What happens as more information gets added to the process’s “environment” – more parameters? New system calls? This gets uglyWhat’s the most general way of setting up all of the environment?So, why not allow process setup at any point?–This is the exec( ) system call (and its variants)6But We Want a Parent and a ChildThe exec call “destroys” the current processSo, instead, destroy a copy of the process–The fork( ) call duplicates the current process–Better yet, don’t tightly couple fork and exec•This way, you can customize the child’s environmentSo what does fork( ) entail?–Making a copy of everything about the process–Ouch!7What Gets CopiedSo far, we’ve covered the following:–VM system–File system–SignalsHow do we go about copying this information?What parts are easy to copy, and what’s hard?What’s the common case with fork/exec?–What needs to get preserved in this scenario?8Shared MemoryHow to destroy a virtual address space?–Link all PTEs–Reference countHow to swap out/in?–Link all PTEs–Operation on all entriesHow to pin/unpin?–Link all PTEs–Reference count............Process 1Process 2w...wPage tablePage tablePhysicalpages9............Copy-On-WriteChild’s virtual address space uses the same page mapping as parent’sMake all pages read-onlyMake child process readyOn a read, nothing happensOn a write, generates an access fault–map to a new page frame–copy the page over–restart the instructionParent processChild processrr...rrPage tablePage tablePhysicalpages10Issues of Copy-On-WriteHow to destroy an address space–Same as shared memory case?How to swap in/out?–Same as shared memoryHow to pin/unpin–Same as shared memory11Process Creation & TerminationFour primitives:Fork – create a copy of this processExec – replace this process with this programWait – wait for child process to finishKill – (potentially) end a running processProcesses form a tree – what happens when parent disappears?12SignalsAsynchronous event delivery mechanismExamples – FPE, segv, ctrl-c, hang up, resumeDefault actions – ignore, abort, core dumpHandler – program-specified routine for signal13A Signaling SidebarWhat’s wrong with this program:int randVal;void SigHand(void){printf(“your rand val is %d\n”, randVal);}int main(int argc, char *argv[]){set up ctrl-c handler;while (1) {randVal = 0;randVal = 1;…randVal = 9;}}14Scheduling PrimitivesBlock – wait on some event/resource–Network packet arrival–Keyboard, mouse input–Disk activity completionYield – give up running for now–Directed (let my friend run)–Undirected (let any process run)Synchronization–We will talk about this later15Our Friend, The Transition DiagramRunningBlockedReadyScheduler dispatchWait forresourceResource becomesavailableCreatea processterminate16Process State Transition ofNon-Preemptive SchedulingRunningBlockedReadyResource becomes available(move to ready queue)Createa processTerminate(call scheduler)Yield(call scheduler)Block for resource(call scheduler)Schedulerdispatch17Who’s Happy Right NowSchedulerA non-preemptive scheduler invoked by explicit block or yield callsThe simplest formScheduler:save current process state (into PCB)choose next process to rundispatch (load PCB and run)Does this work?More on SchedulerShould the scheduler use a special stack?–Yes, because a user process can overflow and it would require another stack to deal with stack overflowShould the scheduler simply be a kernel process?–You can view it that way because it has a stack, code and its data structure–This process always runs when there is no user process20Where Should PCB Be Saved?Save the PCB on its user stack–Many processors have a special instruction to do it “efficiently”–But, need to deal with the overflow problem–When the process terminates, the PCB vanishesSave the PCB on the kernel heap data structure–May not be as efficient as saving it on stack–But, it is very flexible and no other problems21Physical Memory & MultiprogrammingMemory is a scarce resourceWant to run many programsPrograms need memory to runWhat happens whenM(a) + M(b) + M(c) > physical mem?Answer: paging. But what if no paging?22Job SwappingPartially executedswapped-out processesReady QueueCPUI/O WaitingqueuesI/OTerminateSwap outSwap in23Add Job Swapping toState Transition DiagramRunningBlockedReadyResource becomes available(move to ready queue)Createa processTerminate(call scheduler)Yield(call scheduler)Block for resource(call scheduler)SchedulerdispatchSwap outSwap inSwap24Think About SwappingIs swappingNecessaryDesirableGoodIdealThings to considerPerformanceComplexityEfficiencyMoreover, what decides swapping versus paging?Is each appropriate
View Full Document