Operating Systems · Cambridge syllabus 4.1 · Lesson 3 of 3
Interrupts
An interrupt is a signal to the processor that something needs attention. Hardware interrupts come from devices, such as a key being pressed or the mouse being moved; software interrupts come from programs, such as an attempt to divide by zero or two processes trying to use the same memory location. The processor finishes its current instruction, saves what it was doing and runs an interrupt service routine (ISR) to handle the interrupt, then carries on with what it was doing or stops the program, depending on the interrupt.
By the end, you can
- Understand what an interrupt is and why it is needed.
- Describe how an interrupt is generated.
- Give examples of hardware interrupts and software interrupts.
- Describe how an interrupt is handled using an interrupt service routine (ISR).
- Describe what happens as a result of an interrupt.
What an interrupt is
An interrupt is a signal sent to the processor to tell it that something needs attention straight away. Without interrupts, the processor would have to keep checking every device and program to see whether anything had happened, which would waste most of its time; with them, it can get on with its work and respond only when needed.
An interrupt is generated either by a piece of hardware or by software. A hardware interrupt comes from a device: pressing a key on the keyboard, moving the mouse, or a printer reporting that it has run out of paper. A software interrupt comes from a program: an attempt to divide by zero, or two processes trying to access the same memory location.
Worth remembering
- An interrupt is a signal to the processor that something needs attention.
- Hardware interrupts: pressing a key, moving the mouse.
- Software interrupts: division by zero, two processes using the same memory location.
Handling an interrupt
The processor checks for interrupts at the end of each fetch–decode–execute cycle. If there is one, it does not stop half way through an instruction; it finishes the current instruction first.
Then it handles the interrupt. It saves the current state of the task it was working on, such as the contents of its registers, so it can come back to it later. It works out which interrupt has happened and runs the interrupt service routine (ISR) for it: a small program that knows how to deal with that particular interrupt, such as reading the key that was pressed.
What happens as a result depends on the interrupt. For a key press, the ISR passes the key to the program that is waiting for it, the saved state is restored, and the processor carries on with the task it was doing as if nothing had happened. For a division by zero, the program that caused it cannot carry on correctly, so the result is usually an error message and the program being stopped.
In an answer about interrupts, give the steps in order: the interrupt is generated, the current instruction is finished, the state is saved, the ISR is run, and then the task resumes or the program is stopped.
Worth remembering
- Interrupts are checked at the end of each fetch–decode–execute cycle.
- The current task is saved and an interrupt service routine (ISR) is run.
- Afterwards the saved task resumes, or the program that caused it may be stopped.
Worked example
A user presses a key while a document is open. The keyboard sends an interrupt; the processor saves the state of the current task, runs the keyboard's ISR, which reads the key and passes it to the word processor, and then goes back to the task it was doing.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
A software interrupt: it is caused by a program, not by a device.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.