Edexcel International Advanced Level Computer Science, point 1.2.2
Definition
Multitasking is the ability of an operating system to run more than one program (process) at the same time, or to appear to. On a single CPU core only one process runs at any instant, so the OS switches between them so quickly that they all seem to run together.
Function and why needed
- It lets a user use several programs at once (music, browser, document).
- It keeps the CPU busy: while one process waits for a slow disk or keypress, another can use the processor.
- It lets many users or tasks share one computer.
a. Time sharing
- Definition: the CPU's time is divided into short slices (time slices or quanta), and each process gets a turn.
- Operation: a process runs until its time slice ends (or it must wait). The OS then moves to the next process. A slice is typically a few milliseconds.
- Benefit: every process makes progress, and the system stays responsive.
Each box is one time slice: processes A, B and C take turns on the CPU.
b. Context switching
- Definition: a context switch is stopping one process and starting another, by saving the state of the first and restoring the state of the second.
- The context of a process includes the program counter, the contents of the CPU registers, the stack pointer and the status register. It is stored in a process control block (PCB).
- Operation:
- A timer interrupt or an event stops the running process.
- The OS saves its context (PC, registers) in its PCB.
- The scheduler chooses the next process (1.2.3).
- The OS loads that process's saved context into the CPU.
- The CPU carries on from where that process left off.
- Drawback: the switch itself takes time and does no useful work for any program. Switching too often wastes time (overhead), and cache contents may be lost.
c. Interrupt handling in multitasking
- Definition: an interrupt is a signal to the CPU that something needs attention immediately. The CPU pauses its current work, deals with it, then returns.
- Sources:
- Hardware: a key press, mouse click, disk or network finished, a timer, a hardware fault.
- Software: an error (such as divide by zero), a request from a program for the OS, or a program ending.
- Timer: ends a process's time slice, so the OS can switch. This is what makes time sharing work.
- Why needed: without interrupts the CPU would have to keep checking (polling) each device, which wastes time, and urgent events could be missed.
The interrupt handling process
The CPU checks for interrupts at the end of each fetch-decode-execute cycle. When one has been raised, it follows five steps:
- Suspend the running process. The current FDE cycle is finished first, so no instruction is left half done.
- Save the status in a stack frame. The program counter (PC) and the other registers are pushed onto the stack, so the process can carry on later exactly where it stopped.
- Service the interrupt. The start address of the interrupt service routine (ISR) is loaded into the PC, and the ISR runs and deals with the interrupt.
- Pop the status from the stack frame. The saved PC and register contents are copied back into the CPU.
- Resume the suspended process, from the instruction it was on before the interrupt.
An interrupt service routine (ISR) is a piece of OS code that handles one kind of interrupt: one for the keyboard, one for the timer, one for a printer error, and so on.
Priorities
Interrupts have priorities. A power failure or hardware fault is more urgent than a key press. An interrupt with a lower priority than the current task waits until it can be dealt with. A higher-priority interrupt can interrupt a lower-priority ISR: its status is saved in another stack frame on top of the first, which is one reason a stack (last in, first out) is used.
Benefits of multitasking
- Efficient use of the CPU, since it is never idle while work is waiting.
- Better user experience, with several programs open and responsive.
- Resources are shared among users and tasks.
Drawbacks
- Overhead from context switches, scheduling and interrupts.
- More memory and processing power are needed, since several programs are loaded at once. Running too many memory-hungry tasks can make the system freeze or crash.
- Complexity: the OS must protect processes from each other and manage shared resources. A problem in one task can affect others, causing errors or crashes.
- Too many processes can slow everything down.
Worked example
Task: a user is typing in a word processor (process A) while a file downloads (process B). A key is pressed. Describe what happens.
- The keyboard sends an interrupt.
- The CPU finishes the current FDE cycle of whichever process is running (say B).
- It suspends B and saves B's PC and registers in a stack frame on the stack.
- The keyboard's ISR runs, reads the key code and puts it in a buffer for process A.
- B's status is popped from the stack frame, and B resumes. The scheduler soon gives A its time slice, now that A has input waiting.
- A displays the character, and B has carried on from exactly where it was, without losing any download data.
All of this happens in a tiny fraction of a second, so the user sees both tasks apparently carrying on together.
Exam tip: learn the five interrupt handling steps in order: suspend the process, save its status in a stack frame, service the interrupt, pop the status from the stack frame, resume the process. A question may give some steps in a table or diagram and ask you to fill in the missing ones. For a context switch between processes, say what is saved (the PC and registers) and where (the process control block).
Check yourself
What is multitasking?
Running more than one process at the same time, or appearing to, by switching the CPU between them.
What is a context switch, and what is saved?
Stopping one process and starting another. The state of the first (program counter, registers, stack pointer and status) is saved in its process control block, and the state of the next process is restored.
Why is a context switch a drawback as well as a necessity?
It takes CPU time and does no useful work for any program, so too many switches waste time.
What does the CPU do with the PC and registers when an interrupt is accepted?
It pushes them onto the stack in a stack frame, so that the interrupted process can be resumed later, then loads the PC with the start address of the interrupt service routine. Afterwards, they are popped from the stack to restore the process.
Why are interrupts better than polling?
The CPU doesn't waste time repeatedly checking devices, and urgent events are dealt with straight away.