When an interrupt arrives, the processor saves the state of its task and runs an interrupt service routine (ISR) for that interrupt. Then it restores the saved state and carries on. You follow these steps and see what happens as a result of each kind of interrupt.
about 25 min1 simulation1 practice question
Lesson 3 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
Marking the page
Go back to the book and the friend who taps your shoulder. You do three things. You mark your page. You answer the question. You open the book at the mark and read on. The processor does the same three things for an interrupt.
The steps
You met the fetch-decode-execute cycle in the chapter on computer hardware. An interrupt is dealt with between two cycles.
The interrupt is generated. A device or a program sends a signal to the processor.
The processor checks for interrupts at the end of each fetch-decode-execute cycle, so the current instruction is finished first.
It checks the priority. Each interrupt has a priority. If the interrupt is more urgent than the current task, the task is stopped. If not, the interrupt waits.
It saves the state of the task. The contents of the registers, such as the address of the next instruction, are saved. The task is paused.
It runs the interrupt service routine (ISR). An ISR is a small program that deals with one kind of interrupt. There is a different ISR for a key press, for a division by zero, and so on.
It restores the state. When the ISR has finished, the saved register contents are put back. The task carries on from exactly where it stopped.
If several interrupts are waiting, the one with the highest priority is serviced first.
A flowchart. A rounded box Start leads to a rectangle Run the next fetch-decode-execute cycle, then a diamond Interrupt waiting? The No branch goes back to the cycle. The Yes branch goes to a diamond Higher priority than the task? Its No branch goes back to the cycle. Its Yes branch goes to a rectangle Save the state of the task, then a subroutine box with double sides Run the interrupt service routine, then a rectangle Restore the state of the task, and back to the cycle.
The flowchart uses the symbols of the syllabus: a rounded box for start and stop, a diamond for a decision, a rectangle for a process, and a rectangle with double sides for a subroutine, here the ISR.
What happens as a result
The ISR decides the result. Some examples:
A key is pressed. The ISR reads the key and stores it, and the character can appear on the screen. The task carries on.
The mouse is moved. The ISR reads the movement, and the pointer moves on the screen.
A program divides by zero. The ISR may show an error message, and the program may be stopped, because it cannot carry on with a wrong value.
Two processes try to access the same memory location. The OS stops one of them from using it, so the data of the other is safe. The program may be stopped with an error.
How much time do the routines take?
An ISR is short, but some devices send many interrupts. The model shows what share of each second goes on the routines.
In the exam. "Describe how an interrupt is handled" scores marks for each step in the right order: the signal is sent; the processor checks at the end of the cycle; the current task is saved; the ISR is run; the task is restored and continues.
Practise with new numbers
Try it in Python
The program runs a task of 5 steps. An interrupt arrives during step 3, and the program prints each step the processor takes to deal with it.
Common mistakes
Leaving out saving the state. Without it, the task could not carry on from where it stopped.
Stopping in the middle of an instruction. The processor finishes the current cycle first.
One ISR for everything. Each kind of interrupt has its own routine.
Saying the task starts again from the beginning. It carries on from where it was stopped.
Remember these
The processor checks for interrupts at the end of each fetch-decode-execute cycle.
It saves the state of the task, runs the interrupt service routine, then restores the state and carries on.
The highest priority interrupt is serviced first.
The ISR decides the result: a key is stored, an error message is shown, a program may be stopped.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
What is an interrupt service routine?
Why does the processor save the state of its task before it runs the ISR?
When does the processor check for an interrupt?
Two interrupts are waiting: one of priority 2 and one of priority 5 (a larger number is more urgent). Which is serviced first?
What may happen as a result of a division by zero?
Enroll free in The Operating System and Interrupts
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.