OS Lecture25 Scheduling and Context Switching in Xv6
OS Lecture25 Scheduling and Context Switching in Xv6
xv6
Mythili Vutukuru
IIT Bombay
https://www.cse.iitb.ac.in/~mythili/os/
Context switching in xv6
• Every CPU has a scheduler thread (special process
that runs scheduler code)
• Scheduler goes over list of processes and switches to
one of the runnable ones
• After running for some time, the process switches
back to the scheduler thread, when:
– Process has terminated
– Process needs to sleep (e.g., blocking read system call)
– Process yields after running for long (timer interrupt)
• Scheduler thread runs its loop and picks next process
to run, and the story repeats
• Context switch only happens when process is already
in kernel mode.
– Example: P1 running, timer interrupt, P1 moves to
kernel mode, switches to scheduler thread, scheduler
switches to P2, P2 returns to user mode
2
Scheduler and sched
• Scheduler switches to user process in “scheduler” function
• User process switches to scheduler thread in the “sched”
function (invoked from exit, sleep, yield)
3
Who calls sched()?
• Yield: Timer interrupt
occurs, process has run
enough, gives up CPU
• Exit: Process has called
exit, sets itself as zombie,
gives up CPU
• Sleep: Process has
performed a blocking
action, sets itself to sleep,
gives up CPU
4
struct context
6
swtch function (1)
• Both CPU thread and process maintain a context
structure pointer variable (struct context *)
• swtch takes two arguments: address of old context
pointer to switch from, new context pointer to switch to
• When invoked from scheduler: address of scheduler’s
context pointer, process context pointer
7
swtch function (2)
• What is on the kernel stack when a process/thread has just invoked the swtch?
– Caller save registers (refer to C calling convention)
– Return address (eip)
• What does swtch do?
– Push remaining registers on old kernel stack (only callee save registers need to be saved)
– Save pointer to this context into context structure pointer of old process
– Switch esp from old kernel stack to new kernel stack
– ESP now points to saved context of new process
– Pop callee-save registers from new stack
– Return from function call (pops return address, caller save registers)
• What will swtch find on new kernel stack? Where does it return to?
– Whatever was pushed when the new process gave up its CPU in the past
• Result of swtch: we switched kernel stacks from old process to new process,
CPU is now executing new process code, resuming where the process gave up its
CPU by calling swtch in the past
8
swtch function (3)
• When swtch function call is made, kernel stack
of old process already has (reading from top):
eip, arguments to swtch (address of old context
pointer, new context pointer)
• Store address of old context pointer into eax
– Address of struct context * variable in eax
• Store value of new context pointer into edx
– edx points to new context structure
• Push callee save registers on kernel stack of old
process (eip, caller save already present)
• Top of stack esp now points to complete context
structure of old process. Go to address saved in
eax (old context pointer) and rewrite it to point
to updated context of old process
– struct context * in struct proc is updated
• Switch stacks: Copy new context pointer stored
in edx (top of stack of new process) into esp
– CPU now on stack of new process
• Pop registers from new context structure, and
return from swtch in new process
– CPU now running new process code
9
Summary of context switching in xv6
• What happens during context switch from process P1 to P2?
– P1 goes to kernel mode and gives up CPU (timer interrupt or exit or
sleep)
– P2 is another process that is ready to run (it had given up CPU after
saving context on its kernel stack in the past, but is now ready to run)
– P1 switches to CPU scheduler thread
– Scheduler thread finds runnable process P2 and switches to it
– P2 returns from trap to user mode
• Process of switching from one process/thread to another
– Save all register state (CPU context) on kernel stack of old process
– Update context structure pointer of old process to this saved context
– Switch from old kernel stack to new kernel stack
– Restore register state (CPU context) from new kernel stack, and
resume new process
10