HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
The Operating System and Interrupts
  1. 1What an operating system does
  2. 2Interrupts and where they come from
  3. 3Handling an interrupt
  1. Computer Science
  2. /The Operating System and Interrupts
  3. /Handling an interrupt

Course 4.2·Lesson 3 of 3

Handling an interrupt

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
The Operating System and Interrupts
  1. 1What an operating system does
  2. 2Interrupts and where they come from
  3. 3Handling an interrupt

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.

  1. The interrupt is generated. A device or a program sends a signal to the processor.
  2. The processor checks for interrupts at the end of each fetch-decode-execute cycle, so the current instruction is finished first.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Read it first. Then run it. After which step does the task stop, and after which step does it carry on?

1. Change interrupt_at = 3 to interrupt_at = 5. What changes in the output?

2. Change the message to a division by zero. What should the ISR do now?

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.

  1. What is an interrupt service routine?
  1. Why does the processor save the state of its task before it runs the ISR?
  1. When does the processor check for an interrupt?
  1. Two interrupts are waiting: one of priority 2 and one of priority 5 (a larger number is more urgent). Which is serviced first?
  1. 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.
No payment. You can leave at any time.

Problems (3)

A user presses a key while a program is running…Medium · High School (9–10) · by StemPy Lab
A processor receives 800 interrupts each second from its devices…Hard · High School (9–10) · by StemPy Lab
A processor is running a spreadsheet. At the end of a fetch-decode-execute cycle, three interrupts…Hard · High School (9–10) · by StemPy Lab
That was the last lessonBack to the course: take the test
← The Operating System and Interrupts
← Interrupts and where they come fromNext →

Discussion (0)

Log in to join the discussion.

Loading…

1 Python program
5 questions to check yourself

This lesson is free to read. Enroll free to keep your place and take the tests.

Python Labs (1)

Which interrupt goes first?Python Lab · by StemPy Lab

Question Sets (1)

Operating Systems and Interrupts: Concept Check10 questions · by StemPy Lab

Notes (1)

The Operating System and Interrupts: Quick RevisionNote · by StemPy Lab