Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Processes and CPU scheduling

After this lesson: I can explain how the operating system shares the processor between processes.

50 min lesson3 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

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

WordWhat it meansExample
Processa program that is running now
Schedulerthe part of the system that picks the next process to run
Ready queuethe processes that wait for the processor
Context switchsaving one process and loading another
Time slicethe longest a process may run before it must give way10 ms
Burstthe 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

Command
grep State /proc/self/status

Shown 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

LetterNameWhat the process is doing
RrunningUsing a core now, or ready and next in line
SsleepingWaiting for something, such as a key press
Ddisk sleepWaiting for the disk, and cannot be interrupted
TstoppedPaused on purpose
ZzombieFinished, 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

Command
grep ctxt_switches /proc/self/status

Shown 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?

Command
nproc

Shown 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

TurnProcessRuns fromRuns toTime still needed
1P1 (needs 5)023
2P2 (needs 3)241
3P3 (needs 2)460, finished at 6
4P1681
5P2890, finished at 9
6P19100, 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.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.