• Servers and Tools

Process Scheduling

From the class:  Processes

You've seen a few examples of processes running, and you should have a good intuitive sense of how your code gets executed and now gets turned into a process. And we've talked about the fact that the operating system creates this kind of data structure force under the hood called a process. And I want to dive a little bit into more detail about what's inside of that data structure that we can't really see.

I've drawn a diagram to the best of my ability. I'm not an artist, but hopefully you get the idea. And let's take a look at some of the different parts. So first off on the left, we have our code. So these little squiggly lines here are our lines of code like our JavaScript or Ruby files that we were just looking at. And you can imagine that all of this code is loaded into the process into memory.

This thing right here is IP, it's short for Instruction Pointer. So imagine a pointer that is keeping track of which line of code we're actually executing next. And that's what this does. It's a bit of a simplification, but I hope you get the idea. So this is our code. This is going to be our running program that gets executed sequentially, one line at a time.

Over on the right, we have our call stack. And that's used for calling and returning from functions. So just like you would see in a JavaScript debugger window where we look at the call stack of our functions calling and returning, the same thing is happening here.

And down below, I just listed out some of the other attributes of the process. For example, we have the memory that's available to the process that is confined to just this process, so that one process can't access the memory of another process. Then there's a table of file descriptors. Those are files that are opened up by the process and readable or writable by the process. And then some security attributes, like who owns the process and who is the group owner of the process. So these are different attributes that are associated with a running process, and I hope that this gives you a bit of a visual.

Next up what I want to do is to take a look in a more active view what the processes are doing. And we can use another command called top. And again, we can learn more about it by using a manual pages man top. Sounds a bit like a haircut. And just kind of scroll down and you can get a sense. This is quite a hefty command. There's a lot going on here.

And I'm just going to show you the basics, so I'm going to just type top. And it's going to show us a list of all the processes. Except this view is actually moving. Notice that every once in awhile, the CPU column here will kind of light up and a process starts using the CPU.

And there's also information about the memory that a process is using. And of course over on the left we have the pid that we've seen so far. Now let's say on our computer we only have one CPU-- Central Processing Unit. And you can think of a CPU as the machinery that runs instructions, that runs our program. And if we only have one CPU, we can only be running one process at a time.

But notice down here that there's all these processes, and they seem to be running simultaneously. And that's because the operating system performs a really critical function force called scheduling. What it does is it keeps track of which process is running at any given time. It'll allots the process a little bit of time to run, and then will automatically swap it out and then swap in a new process to run.

Now, all of this happens very, very quickly. So it gives the impression that all these processes are running at the same time. But actually, the operating system is scheduling each one to run for a couple of milliseconds at a time, and then it swaps it out. So when it swaps out a process, this entire diagram over here, the stack, the memory, the file descriptor, security, all of that gets stored away into memory or onto disk. And when the program is ready to run again, the operating system pulls it back and loads it up and starts executing a few more instructions.

Now let's say instead of having one CPU we actually have two. So now we have more than Central Processing Unit. That means that we can run two processes simultaneously, and they will actually run at the very exact same time. So the more CPUs generally means the more work we can do given more processes. So more than one process can run at the exact same time.