How one processor runs many programs
A process is a program that is running. The scheduler is the part of the operating system that decides which process uses the processor next. Without a scheduler, one program would keep the processor and everything else would freeze.
A laptop with one core can play music, load a web page and copy a file at the same time. It does this by switching between them many times a second.
By the end of this lesson
Note
I can explain how the operating system shares the processor between processes.
Key words
| Word | What it means | Example |
|---|---|---|
| Process | a program that is running now | |
| Scheduler | the part of the system that picks the next process to run | |
| Ready queue | the processes that wait for the processor | |
| Context switch | saving one process and loading another | |
| Time slice | the longest a process may run before it must give way | 10 ms |
| Burst | the time a process needs the processor in one go |
The cells students fill in are left blank in this preview.
A process is a program that is running
A program is a file. When you start it, the system makes a process: the program plus everything it needs to run now.
The system keeps a record for each process. It holds the process number, its place in the program, its open files and its state. The state says what the process is doing right now.
Read the state of the grep command itself
grep State /proc/self/statusShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Worked example: the state letters Linux prints
| Letter | Name | What the process is doing |
|---|---|---|
| R | running | Using a core now, or ready and next in line |
| S | sleeping | Waiting for something, such as a key press |
| D | disk sleep | Waiting for the disk, and cannot be interrupted |
| T | stopped | Paused on purpose |
| Z | zombie | Finished, but its parent has not collected the result |
The cells students fill in are left blank in this preview.
A context switch swaps one process for another
To pause a process the system copies its registers and its place in the program into its record. Then it loads the saved copy of another process and lets that one run.
This is a context switch. It is pure overhead: while it happens, no program does useful work. A process counts a switch as voluntary when it gave up the core to wait, and as nonvoluntary when the system took the core away.
Count the grep command's own context switches
grep ctxt_switches /proc/self/statusShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
How many cores does the lab have?
nprocShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
The ready queue lists the waiting processes
Every process in the ready state stands in the ready queue. When a core is free, the scheduler takes one process from the queue.
With a time slice, the process runs for at most that long. If it has not finished, it goes to the back of the queue and the next one runs. This way of sharing is called round robin.
Worked example: three processes, slice of 2
| Turn | Process | Runs from | Runs to | Time still needed |
|---|---|---|---|---|
| 1 | P1 (needs 5) | 0 | 2 | 3 |
| 2 | P2 (needs 3) | 2 | 4 | 1 |
| 3 | P3 (needs 2) | 4 | 6 | 0, finished at 6 |
| 4 | P1 | 6 | 8 | 1 |
| 5 | P2 | 8 | 9 | 0, finished at 9 |
| 6 | P1 | 9 | 10 | 0, finished at 10 |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think a computer with one core runs several programs at the same moment. With one core, only one process runs at any instant. The others wait in the ready queue. The switching is so quick that it looks like they all run together.