← Back to Computer Science

Processes, threads, and scheduling

A computer seems to do many things at once. The OS creates that illusion by rapidly switching the processor between processes and threads.

Programs and processes

A program is code stored on disk; a process is a running instance of a program, with its own memory, open files, and state. Opening two browser windows may create several processes from one program. Each process has a process identifier (PID) and is isolated from the others.

Process states

A process moves between states: new, ready (waiting for the CPU), running, waiting or blocked (for example, waiting for disk or network data), and terminated. Most processes spend much of their time waiting, which leaves the CPU free for others.

Context switching

To switch the CPU from one process to another, the OS saves the current process's registers and state and loads the next one's: a context switch. It happens thousands of times a second, creating the illusion of simultaneity on a single core. Each switch has a small cost, so excessive switching wastes time.

Threads

A thread is a sequence of execution within a process. Threads in the same process share memory, which makes communication fast but requires care. A web browser might use one thread to draw the page while another downloads images. On multicore CPUs, threads can truly run at the same time.

Scheduling

The scheduler decides which ready process or thread runs next. Goals include fairness, responsiveness for interactive programs, and throughput. Strategies include first-come first-served, round robin (each gets a short time slice in turn), and priority scheduling. Modern schedulers mix these, favouring interactive tasks so the interface feels smooth.

Concurrency problems

When threads share data, a race condition can occur: the result depends on unlucky timing. If two threads each read a bank balance of 100, add 50, and write back, the balance may end at 150 instead of 200. Locks (mutexes) ensure only one thread at a time runs a critical section. Misused locks can cause deadlock, where threads wait on each other forever.

Deadlock

Deadlock requires four conditions together: mutual exclusion, hold and wait, no pre-emption, and circular wait. A classic picture is two people each holding one chopstick and waiting for the other's. Preventing any one condition, for example by always acquiring locks in the same order, prevents deadlock.

Worked example

Round robin with a 10-millisecond time slice and three ready processes, A, B, and C: A runs for 10 ms, then B, then C, then A again, and so on. If A needs 25 ms of CPU, it finishes during its third turn, after B and C have each had two slices. If B blocks waiting for disk, it leaves the ready queue until its data arrives, and A and C share the CPU.

Key takeaways

A program is stored code; a process is a running instance with its own memory and PID. Processes move between ready, running, and waiting states. Context switches save and restore state to share the CPU. Threads share their process's memory and can run in parallel on multicore CPUs. Schedulers use round robin, priority, and other strategies. Race conditions need locks; misused locks can cause deadlock, which needs four conditions together.

Flashcard

Process

Running instance of a program

Own memory and PID.

Check your understanding

Two threads both add 1 to a shared counter at the same time, and the counter only increases by 1. What is this bug called?

Take the unit quiz

← Previous lesson Next lesson →