Process Lifecycle & Context Switching
Total Page:16
File Type:pdf, Size:1020Kb
CSC501 Operating SystemsPrinciples Process Lifecycle & Context Switching 1 Last Lecture q Processes vs. Threads PerformanceResponsivenessFexibility Security Processes Threads Better Worse 2 Last Lecture q Processes vs. Threads q Today Q Process Lifecycle Q Context Switching 3 OS Process Management 4 Process Lifecycle New Terminated Admitted Exit Scheduler/Interrupt Ready Running Scheduler Dispatch I/O or Event Completion Waiting I/O or Event Wait 5 Process State q As a process executes, it changes state Q new: The process is being created Q running: Instructions are being executed Q waiting: The process is waiting for some event to occur Q ready: The process is waiting to be assigned to a processor Q terminated: The process has finished execution q These are the important conceptual states Q Actual name and number of states are OS-dependent 6 Process Manipulation q Performed by OS routines q Example operations Q Creation Q Termination Q Suspension Q Resumption q State variable in process table records activity 7 Process Creation q Parent process creates children processes, Q Which, in turn create other processes, Q Forming a tree of processes 8 Question: Which one is the first process? Question: Any other responsibilities? Process Creation q Policy on resource sharing Q Parent and children share all resources Q Children share subset of parent’s resources Q Parent and child share no resources q Policy on execution Q Parent and children execute concurrently Q Parent waits until children terminate q Policy on address space Q Child duplicate of parent Q Child has a program loaded into it 10 Process Creation (Cont.) q UNIX examples Q fork system call creates new process Q exec system call used after a fork to replace the process’ memory space with a new program 11 Process Creation (Cont.) q The UNIX shell is command-line interpreter whose basic purpose is for user to run applications on a UNIX system q cmdarg1 arg2 ... argn Process Termination q Possible scenarios for process termination Q Exit (by itself) Q Abort (by parent) Q Kill (by sysadmin) q Exit Q Process executes last statement and asks the operating system to decide it 13 Process Termination q Abort Q Child has exceeded allocated resources Q Task assigned to child is no longer required Q If parent is exiting v Some operating system do not allow child to continue if its parent terminates v All children terminated - cascading termination q Kill Q Administration purpose 14 Process Suspension q Temporarily ‘‘stop’’ a process Q Prohibit from using the CPU q Why? q What should be done? Q Change its state in PCB Q Save its machine states for later resumption v Process table entry retained v Complete state saved 15 Process Resumption q Resume execution of previously suspended process q What should be done? Q Make process eligible for CPU Q Call scheduler if necessary Question: Does resumption guarantee instantaneous execution? 16 Context Switch q When CPU switches to another process 17 Context Switch q Does system do useful work while swithing? Q NO: Context-switch time is overhead q What should be done? Q Save the state of the old process (suspend) Q Load the saved state for the new process (resume) q How long will it take? Q Time dependent on hardware support 18 Context Switch 19 Context Switch q Save state of currently executing process Q Copy all “live” registers to process control block q Restore state of process to run next Q Copy values of live registers from process control block to registers q How to get into and out of the context switching code? 20 Context Switch q OS is just code stored in memory, so context switching code will be implemented as a subroutine Q saves, restores, and returns q What is special? Q The subroutine returns in the context of another (the next) process! Q Eventually, will switch back to the current process Q To process, it appears as if the context switching subroutine just took a long while to return Example Context Switching in XINU 8(%ebp) 16(%ebp) 12(%ebp) 20 (%ebp) 22 Context Switch q Implications? Q Caches v Physical addresses: no problem v Virtual addresses: cache must either have process tag or must flush cache on context switch Q TLB v Each entry must have process tag or flush TLB on context switch Q Page table v Typically have page table pointer (register) that must be reloaded on context switch 23 XinuImplementation q Read relevant source code in Xinu Q Process queue management v h/q.hsys/queue.csys/insert.c, … Q Proc. creation/suspension/resumption/termination: v sys/create.c, sys/suspend.csys/resume.c, sys/kill.c Q Process scheduling v sys/resched.c Q Other initialization code 24 Next Lecture q Process Scheduling 25.