A1.3.5 · Operating systems and control systems
Multitasking and resource allocation
By the end you can
Explain how the operating system runs many processes at once and shares out resources, and how it handles the problems this causes: contention, starvation and deadlock.
You can stream music, edit a document and download a file at the same time, while dozens of background services run unseen. This is , and making it work, and keeping it working, is one of the operating system's hardest jobs. This section brings together the ideas from A1.3.2 and A1.3.3 and looks at what goes wrong when many processes compete for the same things.
How multitasking works#
On one core only one process runs at a time. The OS creates the effect of many by giving each a short turn and switching between them hundreds of times a second. Each switch is a : the running process's registers and program counter are saved in its record in the process table, and the next process's are loaded. On a multi-core CPU several processes really do run at once, one per core, and the scheduler also decides which core each one uses.
Multitasking is about processes, each with its own memory. Multithreading is the same idea inside one process: several threads (A1.3.2) share the process's memory and run concurrently. Sharing memory makes threads cheap to create and switch between, but it is also exactly why the contention problems below arise.
The scheduler (A1.3.3) has to balance competing goals: interactive programs need to respond quickly, background jobs need to make progress, important tasks should go first, and yet every process should get a fair share. Context switches make multitasking possible, but each one costs time, so switching too often slows everything down.
Sharing out the resources#
- Memory: with , memory is split into fixed-size pages placed wherever there is room; with segmentation, a program is split into variable-size logical parts (code, data, stack), each protected separately. Virtual memory extends RAM onto disk.
- Devices: accessed only through drivers and the OS, which queues requests so two programs do not use a device in a conflicting way.
- Files: the file system controls who may open a file and stops two programs writing to it at once.
- Limits: the OS monitors how much CPU, memory and disk each process uses and can cap it (a quota), so one runaway process cannot bring the system down.
Resource contention#
Contention happens when several processes want the same resource at the same moment and it can only serve one. If two processes write to the same file at once, their data can be interleaved and corrupted.
Shared data has the same problem in a subtler form. Two threads (or two processes sharing data) each add 1 to a shared counter that holds 5. Each reads 5, each adds 1, each writes 6: one update is lost, and the counter says 6 when it should say 7. The result depends on the exact timing of the two processes, which is why this is called a , and why it is so hard to find: it may happen once in a million runs. The cure is mutual exclusion: only one process at a time may be in the code that uses the shared resource (its critical section).
Locks and semaphores
A is the simplest tool: a process must take the lock before using the resource and give it back afterwards; anyone else who wants it waits. A generalises this to a pool of identical resources. It is a counter: taking a resource decreases it, releasing one increases it, and at zero a process must wait. Taking and releasing are each done in one indivisible step, so two processes can never both see the last free resource. A binary semaphore can only be 0 or 1 and behaves exactly like a lock; a counting semaphore starts at the number of resources. Here is a counting semaphore guarding three database connections:
| Event | Semaphore | Result |
|---|---|---|
| Start | 3 | three connections free |
| A asks for a connection | 2 | A gets one |
| B asks for a connection | 1 | B gets one |
| C asks for a connection | 0 | C gets one |
| D asks for a connection | 0 | none left: D waits |
| A releases its connection | 0 | handed straight to D |
| B releases its connection | 1 | one more is free |
Connections: freefreefree
Waiting queue: empty
Process A
idle
Process B
idle
Process C
idle
Process D
idle
Process E
idle
A readers–writer lock relaxes the rule where it is safe to: any number of processes may read a resource at once, because reading changes nothing, but a writer must have it to itself. Higher-level tools such as monitors wrap the shared data and the code that uses it together, so the lock is taken and released automatically, and let a process wait inside until some condition is true (“the queue is not empty”).
Badly managed contention causes its own problems:
- : a process never gets the resource because others always get there first.
- : a high-priority process needs a lock held by a low-priority process, which itself cannot run because medium-priority work keeps pre-empting it. The urgent process is stuck behind the least urgent. (A fix: temporarily raise the lock holder's priority.)
- Livelock: processes keep reacting to each other (each politely backing off and retrying) so they stay busy but never make progress.
- : too many processes for the RAM, so pages are swapped in and out constantly and almost no real work gets done.
Deadlock#
is the worst case: a group of processes each holding something another one needs, all waiting in a cycle. Nobody can continue, and nobody will let go. It needs four things at once: resources that only one process can use at a time; processes that hold one resource while waiting for another; no way to take a resource away from a process; and a circular chain of waiting. Remove any one and deadlock cannot happen.
Process A needs the Printer, then the File
Holds: nothing
Process B needs the File, then the Printer
Holds: nothing
Press Next step. The processes take turns.
The OS can deal with deadlock in four ways:
Prevention
Design the system so one of the four conditions can never hold: for example, every process must request resources in the same fixed order, so a cycle cannot form.Avoidance
Check each request before granting it, and refuse (make the process wait) if granting it could lead to deadlock. The Banker’s algorithm does this by checking that every process could still finish.Detection
Allow requests freely, but regularly check for a cycle of processes waiting for each other.Recovery
When a deadlock is found, break it: end one of the processes, or take a resource away from it and roll it back.
Check yourself
Multitasking, semaphores and deadlock
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
6 questions · 26 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Explain, with an example, how deadlock can occur between two processes.
[4]Explain: Give reasons: say what happens AND why, or how one thing causes another.
2.Describe two ways an operating system can deal with deadlock.
[4]Describe: A detailed account: say what happens, step by step if it is a process.
3.A server can handle 3 database connections at once. Describe how a counting semaphore controls access to them.
[3]Describe: A detailed account: say what happens, step by step if it is a process.
4.Explain what thrashing is and why it happens.
[3]Explain: Give reasons: say what happens AND why, or how one thing causes another.
5.Discuss the problem of resource contention in a multitasking system and the strategies an operating system uses to reduce its effects.
[6]Discuss: Look at the question from more than one side, weigh the points against each other, and come to a conclusion.
6.Discuss the challenges an operating system faces in managing multitasking and resource allocation.
[6]Discuss: Look at the question from more than one side, weigh the points against each other, and come to a conclusion.
Past papers
Old exam questions on this topic
From HL Paper 1, May 2014 to November 2025. These papers were set on the previous syllabus, so the wording may differ from today's exam, but the topic is the same. Find the questions and markschemes in your school's past-paper pack.
| Paper | Question | Marks | About |
|---|---|---|---|
| May 2024 TZ1 | 9 | 4 | Two resource management techniques |
| May 2022 | 8 | 3 | Three resource management techniques |
| May 2021 | 15(a) | 4 | Two resource management techniques |
| May 2016 | 9 | 4 | OS functions for multitasking |
In a nutshell
- Multitasking: the OS runs many processes by switching the CPU between them (and running them at once on several cores). Each switch is a context switch: save one state, load another.
- Resource allocation: the OS shares memory (pages, segments, virtual memory), devices (drivers), files and CPU time, and can set limits (quotas) so no process hogs a resource.
- Contention: processes competing for the same resource. Unprotected shared data gives race conditions (lost updates). Mutual exclusion (locks, binary and counting semaphores, readers–writer locks, monitors) stops two using it at once.
- Starvation: a process never gets a resource. Priority inversion: a high-priority process stuck waiting for a resource held by a low-priority one.
- Deadlock: processes each hold a resource the other needs, waiting in a cycle. Handled by prevention, avoidance, or detection and recovery.
- Thrashing: too little RAM, so the system spends its time swapping pages instead of working.
