Efficient computing relies on the Operating System's ability to multitask and manage the speed differences between the CPU and external devices.
Interrupts 🔔
An interrupt is a signal sent from a device or software to the CPU, requesting immediate attention.
Examples:
- Keyboard key press ⌨️
- Printer out of paper 🖨️
- Software error (e.g., divide by zero) ❌
- Timer interrupt ⏱️
Buffers 📦
A buffer is a temporary memory area used to hold data while it is being moved, compensating for speed differences between hardware.
Examples:
- Streaming video (prevents stuttering) 🎥
- Sending a document to a printer 📄
- Hard drive data transfer 💾
5b. Where Interrupts Come From
The syllabus expects the sources of interrupts by category, not just examples. There are four:
- Hardware interrupt — generated by a physical device. A key is pressed, a mouse is moved, a printer runs out of paper, a device is unplugged.
- Software interrupt — generated by a running program when something goes wrong or a service is needed. Division by zero, an attempt to access memory the program does not own, or a request for an operating system service.
- Timer (clock) interrupt — generated at fixed intervals by the system clock. This is what makes multitasking possible: it periodically takes the CPU away from the current process so the OS can give another one a turn.
- Input/output interrupt — generated when a device has finished a transfer or needs more data. A disk has completed a read; a printer buffer has emptied.
The timer interrupt deserves attention. Every other interrupt is a response to
something happening. The timer interrupt happens whether or not anything has occurred, and it is
the mechanism behind processor management: without something to take the CPU back at regular
intervals, a single program could keep it forever and nothing else would ever run.
5c. Interrupt Priority
Interrupts do not all matter equally, so each is given a priority. If an interrupt arrives while another is already being serviced, the CPU compares the two:
- If the new interrupt is higher priority, the current service routine is itself suspended — its state is saved in the same way — and the more urgent one is handled first.
- If it is lower or equal priority, it waits in a queue until the current routine has finished.
This is why a power-failure warning or a hardware fault is dealt with immediately while a printer asking for more data waits its turn. Without priorities, a trivial but frequent interrupt could delay a critical one indefinitely.
6. How the OS Handles Interrupts 🛠️
When an interrupt occurs, the OS must ensure the current task is not lost. It follows these steps:
1. Completion
The CPU finishes its current Fetch-Decode-Execute cycle.
2. Status Check
The CPU checks for any pending interrupts before starting the next cycle.
3. Preservation
The current state (contents of registers and Program Counter) is saved to the stack.
4. ISR Execution
The OS identifies the source and loads the appropriate Interrupt Service Routine (ISR).
5. Restoration
Once handled, the saved state is loaded back from the stack, and the original process resumes.
💡 Exam Tip: Remember, the main purpose of a Buffer is to allow the CPU to continue with other tasks while the slower hardware processes the data, whereas an Interrupt is all about getting the CPU to drop everything to handle an urgent request.
7. Buffers and Interrupts Working Together
Exam questions rarely ask about a buffer or an interrupt on its own. They describe printing, and expect you to explain how the two operate as one mechanism. Here is that sequence:
- The user sends a document to print. The CPU transfers the data into the printer buffer — quickly, because that is a memory-to-memory transfer.
- The CPU is now free and returns to other tasks. It does not wait for the printer, which is thousands of times slower than it is.
- The printer works through the buffer at its own speed, printing as it goes.
- When the buffer is nearly empty, the printer sends an interrupt to the CPU requesting more data.
- The CPU saves what it was doing, refills the buffer, and resumes the interrupted task.
- Steps 3 to 5 repeat until the whole document has been printed.
Why both are needed. The buffer alone would not be enough — something has
to tell the CPU when to refill it, and polling the printer constantly would waste exactly the
time the buffer was meant to save. The interrupt alone would not be enough either — without
somewhere to put the data, the CPU would have to feed the printer one character at a time and
would spend its life waiting. The buffer handles the speed difference; the interrupt
handles the timing.