 Ok, we are now going to see how a context switch occurs. A context switch involves switching from one process to another. If we are to make such a switch, we need to save the state of the previous process and load the state of the new one. We are going to look at how this happens in OS 161. We start by trying to determine how a context switch occurs. We know that a scheduler is responsible for causing a context switch. However, if you grep through the code for schedule, you will find that there is only one function, a defined in Kern thread thread.c, that does nothing. So it's empty. And from the comments, you can see that until this is written, the default scheduler runs threads in a round robin fashion. Ok, so this was sort of a dead end to what we really wanted to see, which is what leads to a context switch. So how do we proceed from here? Ok, so let's open up the include file thread.h and look at what are the functions thread.c defines. Ok, so there's schedule. We've already looked at this one. There is thread yield, which looks promising, causes the current thread to yield to the next roundable thread. So let's look at thread yield. So thread yield simply calls thread switch. So let's find thread switch. So searching through the function up there, we found thread switch. So thread switch, immediately from the comments, you can see that we have found the context switch path because it says it's a high level machine independent context switch code. So going through the code, it sets current thread, it checks if it's idle, does a bunch of spin lock acquires, checks the state of the thread, but we need to get to the switch part because that's what we're interested in. Ah, ok, so here it figures out what the next thread to run is and once that's done, actually the part where it figures out the next thread to run is happening here. And once that is done, it calls a function called switch frame switch. And from the comments, you can see that it's happening possibly in assembly because the file has a .s extension. So we're going to search for the function switch frame switch. Ok, switch frame switch seems to be defined in switch.s. So let's open up this file. So the function is right here. So remember, as part of the context switch, we have to save the state of the previous process and then load the state of the next one, right? So that's clearly what this function is doing because here we can see that it's saving all the registers. It's storing the stack pointer, the program counter and then restoring all the registers of the other process. And once that's done, it just returns. Now here's the trick. When switch frame switch returns, it returns here. And the point is that when the thread that returns here and runs all of this right now is actually the next thread, not the previous one or the current one. This is described in detail in the comments here. I encourage you to go through this. This should make some of the code a little clearer to you. That explains how the scheduler invokes a context switch and how a context switch occurs.