Introduction to Interrupts in OS

Learn via video courses
Topics Covered

Overview

Interrupts are the events that can be caused by hardware or software that signals the processor to complete the ongoing instruction and immediately handle the Interrupt Service Routine (ISR) which contains the information for dealing with the interrupt.

What are Interrupts?

Bob and Alice have been enjoying the journey so far in the world of operating systems and today they are up for a new topic.

Professor: Alice can you please tell me about the kernel?

Alice: Yes Professor! Kernel is …

Bob: Wait, wait Alice!

Alice: Bob! Why did you interrupt me?

Bob: I was just checking that is this the similar kind of interrupt that our computer system faces.

Professor: Haha! That’s correct Bob, but you should now be interrupting Alice like this.

Bob: Sorry Alice!

Professor: Let’s see What are interrupts in the next part.

Interrupts are the events that take place to inform the operating system to stop the current execution of the current process and handle the Interrupt Service Routine (ISR). ISR is responsible for finding out which software or hardware caused the interruption and informing the CPU about it. CPU will service the request and after the completion of the request, CPU resumes the execution of the process which CPU was previously executing. Interrupts can be caused by hardware as well as software.

Bob: But Professor why do we require interrupts?

Professor: Just like when you interrupted Alice to have my attention, similarly when the process is in execution, and if any other software or hardware needs the attention of the operating system they raise the interrupt.

Alice: So Professor, System calls are one form of interrupt?

Professor: No Alice, System Calls are not asynchronous, whereas interrupts are asynchronous. Professor: Now we will be studying the different types of interrupts.

Hardware Interrupts:

When an external device wants the attention of the operating system to service a certain request, it raises an interrupt which is called a hardware interrupt. All the external devices are connected to a single Interrupt Request Line and the Interrupt Request Line is used for the interrupts.

The hardware interrupts are further categorized into two types:

  1. Maskable Interrupts: Hardware interrupts that can be ignored or disabled are called maskable interrupts.
  2. Non-Maskable interrupts: Hardware interrupts that can’t be ignored or disabled are called non-maskable interrupts.

Alice: Professor, can you give an example of any hardware request?

Professor: When we press the key on the keyboard, it triggers a hardware interrupt to gain the attention of the OS and inform OS that the key is pressed. The below image shows the classification of interrupts. Now we will learn about software interrupts.

Types of Interrupts

Software Interrupts:

Software interrupts generally take place when there are exceptions in the process or by using special instructions that cause the interrupts. While having the system calls in our system, we generally have the software interrupt. Division by zero throws an exception which causes the software interrupt, whereas while we use fork() system call, fork() also invokes a software interrupt.

Software Interrupts Type Diagram

As we can see in the above image when the process executes the fork() system call, an interrupt is generated which is a software interrupt and this interrupt will be handled by ISR, Once the interrupt request is serviced, the control is passed back to the process which was executing priorly.

Professor: Now we know what are interrupts in the operating system and also the types of interrupts. But now let’s see how the CPU responds to the interrupt.

CPU Response to Interrupts:

When the interrupt occurs, the CPU completes the execution of ongoing instructions and handles the ISR. However once the interrupt is resolved, the CPU continues to execute from where an execution was stopped prior to the interrupt.

Bob: But professor, how can CPU be able to do that?

Professor: CPU do the Context Switching.

Context Switching is the process where the state of the current process is saved and stored, while another process is brought for execution. Once the execution of the new process is completed, the CPU restores the state of the old process and continues the execution from where the execution of the process was left.

So following steps are involved while handling the interrupts:

  1. The First step involved in handling the interrupt is to check the priority of the interrupt.
  2. If the priority is low compared to the current process under execution, then the interrupt is saved in the memory.
  3. If the priority is high compared to the current process under execution, CPU saves the context of the current process.
  4. CPU loads the new process which invoked the interrupt and executes that.
  5. On completion of the requested service, CPU loads the process that was under execution prior to the interrupt and resumes the execution from where the execution was interrupted.

Conclusion

  • Interrupts are the events that signal the processor to service the request.
  • Interrupts can be caused by hardware as well as software.
  • Hardware interrupts are of two types: Maskable and Non-Maskable Interrupts.
  • Software interrupts are generally caused by exceptions and special instructions eg. fork()
  • CPU handles the interrupt and on completion of the service request, resumes the execution of the process from where the execution was left.