An operating system manages multitasking by scheduling processes, allocating processor time, and saving process states during context switches. It prevents deadlock by controlling resource allocation so that at least one necessary deadlock condition cannot occur.
Managing multitasking
Multitasking allows multiple processes to appear to run simultaneously. On a single-core processor, only one process executes at once, but the scheduler switches the CPU between processes.
Each process may be ready, running, or blocked. The OS stores its state, program counter, register values, and resources in a process control block (PCB). During a context switch, the OS saves its PCB and restores another, allowing execution to resume.
| Mechanism | Role |
|---|---|
| Time slicing | Gives each process a limited period of CPU time |
| Priority scheduling | Selects higher-priority processes first |
| Interrupts | Request CPU attention when events occur |
| Memory management | Separates and protects process address spaces |
Preventing deadlock
A deadlock occurs when processes are blocked because each waits for a resource held by another. It requires the four Coffman conditions:
| Condition | Prevention method |
|---|---|
| Mutual exclusion | Make resources shareable where possible |
| Hold and wait | Require all resources to be requested together |
| No pre-emption | Permit the OS to reclaim resources |
| Circular wait | Enforce a fixed resource request order |
Breaking any one condition prevents deadlock. For example, if a process must request a printer before a scanner, two processes cannot hold one device while waiting for the other.
The OS may instead use deadlock avoidance, granting requests when the system remains in a safe state. With deadlock detection and recovery, it detects a cycle, terminates a process, or pre-empts resources.
Exam technique
In IB responses, distinguish multitasking from parallel processing and explain context switching through PCBs. A misconception is that scheduling alone prevents deadlock; instead, identify the conditions and explain how breaking one prevents it.