Show understanding of the purpose of interrupts

Published by Patrick Mutisya · 14 days ago

Cambridge A-Level Computer Science – 4.1 CPU Architecture: Purpose of Interrupts

4.1 Central Processing Unit (CPU) Architecture

Objective: Understand the purpose of interrupts

An interrupt is a signal that temporarily halts the normal sequential execution of a program, allowing the CPU to service an event that requires immediate attention. After the interrupt is serviced, the CPU resumes the original program where it left off.

What is an interrupt?

In a typical fetch‑decode‑execute cycle, the CPU repeatedly performs the following steps:

\$\text{Fetch} \rightarrow \text{Decode} \rightarrow \text{Execute} \rightarrow \text{Update PC}\$

If an external or internal event occurs, an interrupt can insert a new sequence of steps into this cycle.

Why are interrupts needed?

  • Responsiveness: Allows the system to react quickly to time‑critical events (e.g., keyboard input, timer expiry).
  • Efficient use of CPU time: The CPU can perform useful work instead of constantly polling devices.
  • Multitasking support: Enables the operating system to switch between processes or threads.
  • Resource management: Facilitates handling of I/O completion, error conditions, and hardware failures.

Types of interrupts

TypeSourceTypical Use
Hardware interruptExternal devices (keyboard, mouse, network card)Notify CPU that data is ready or a device needs service
Software interrupt (trap)Program instruction (e.g., system call)Request operating‑system services
Timer interruptInternal timer circuitImplement time slicing for multitasking
Exception (fault)CPU detects illegal operation (divide by zero, page fault)Transfer control to error‑handling routine

How an interrupt is handled

  1. Interrupt request (IRQ) raised: A device or condition asserts an interrupt line.
  2. CPU acknowledges: At the end of the current instruction, the CPU checks the interrupt flag.
  3. Save context: The current program counter (PC) and status registers are pushed onto the stack.
  4. Jump to interrupt service routine (ISR): The CPU loads the address of the ISR from an interrupt vector table.
  5. Execute ISR: The routine performs the necessary handling (e.g., read a byte from a keyboard buffer).
  6. Restore context: The saved PC and registers are popped from the stack.
  7. Resume original program: Execution continues from the point where it was interrupted.

Benefits of using interrupts

  • Reduces wasted CPU cycles compared with polling.
  • Improves system throughput and latency.
  • Enables real‑time response for critical hardware events.
  • Supports pre‑emptive multitasking, allowing multiple programs to share the CPU.

Suggested diagram: Flowchart showing the normal fetch‑decode‑execute cycle with a branch that diverts to an interrupt service routine when an IRQ is detected.

Key points to remember

  • An interrupt temporarily suspends the current instruction stream.
  • The CPU must save the current execution state before servicing the interrupt.
  • Interrupts can be masked (disabled) to protect critical sections of code.
  • Prioritisation of interrupts ensures that more urgent events are handled first.