What is context switching? Before we can jump into the process schedulingtopic, we have to understand what “contextswitching" is.LP1P2P3P2intelSScheduling is the procedure that decides whichTimerinterrupt.process to run next.Hardware interruptContext switching is the actual switching procedure,fromoneprocesstoanother.21
What is context switching? • Before we can jump into the process scheduling topic, we have to understand what “context switching” is. 21 P1 P2 P3 P2 Scheduling is the procedure that decides which process to run next. Context switching is the actual switching procedure, from one process to another. Timer interrupt. Hardware interrupt
Switchingfrom oneprocesstoanotherSupposethisprocessgivesuprunningontheCPUSystemMemorye.g.,callingsleep().Then:User-spaceWaitingRunningmemoryNow,it is time forthe schedulerto choose the next三processtorun.(1)(3)Program counterScheduler(2)sleep()OtherRegistervaluesintel)Kernel-spaceCore"i722
22 Switching from one process to another. System Memory Kernel-space User-space memory Program counter Other Register values Scheduler Suppose this process gives up running on the CPU, e.g., calling sleep(). Then: Now, it is time for the scheduler to choose the next process to run. Running Waiting sleep() (1) (2) (3)
Switching from one process to anotherSystem MemoryBut,beforethe schedulercan seize thecontrolof theCPU,averyimportant stephastobetaken:User-spaceBackupall registersvaluesmemory三The backup will be stored in theprocessstructure(1)(3)Program counterThecontextofaprocessScheduler(2)sleep()OtherRegisterThe unionofthe user-spacevaluesmemoryandthe registers一valuesoftheprocess(intelKernel-spaceCore"i7backup23
23 Switching from one process to another. System Memory Kernel-space User-space memory Program counter Other Register values Scheduler sleep() (1) (2) backup (3) But, before the scheduler can seize the control of the CPU, a very important step has to be taken: Backup all registers’ values. The backup will be stored in the process structure The context of a process The union of the user-space memory and the registers’ values of the process
Switchingfrom oneprocesstoanotherSay,the scheduler decidesto schedule anotherSystemMemoryprocess.User-spaceThen,the schedule hasto load the context of thememorynewprocessintothemainmemoryandintotheCPU.(4) ProgramcounterWecall theentireOther Registeroperation:valuescontextswitching(intel)Core"i7load24
24 Switching from one process to another. System Memory User-space memory Program counter Other Register values load Say, the scheduler decides to schedule another process. Then, the schedule has to load the context of the new process into the main memory and into the CPU. (4) We call the entire operation: context switching
Context switching has a price to pay... However, context switching may be expensive..- Even worse, the target process may be currently storedin the hard disk.So, minimizing thenumber of context switchingmayhelp boost system performance.Myturn!CPUProcessBProcessCProcessAMainProcessF(running)Process DProcessGCacmeExpensivel/Oswap25
Context switching has a price to pay. • However, context switching may be expensive. – Even worse, the target process may be currently stored in the hard disk. • So, minimizing the number of context switching may help boost system performance. 25 CPU Registers Cache Main Memory Hard Disk Process A (running) Process B Process C Process D Process E Process F Process G Expensive I/O swap My turn!