Advertisement

what is an interrupt in OS how is an interrupt generated how is it processed

What is an ‘Interrupt’?

An interrupt is a signal to the processor from a device attached to a computer or from a program within the computer indicating an event that needs immediate attention.
Interrupts provide a mechanism by which other modules (I/O, memory) may interrupt the normal processing of the processor.
An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing.
The processor responds by suspending its current activities, saving its state, and executing a function called an ISR to deal with the event.
This interruption is temporary, and, after the interrupt handler finishes, the processor resumes normal activities.

Types of Interrupts

There are two types of interrupts:
1)Hardware interrupts 
2) Software interrupts.
Hardware interrupts: are used by devices to communicate that they require attention from the operating system.
‘Hardware interrupts’ are generated when a key is pressed or when the mouse is moved.
Software interrupt: is caused by executing a program/instruction within the computer that requires the operating system to stop and figure out what to do next. (e.g. int 21h in assembly)
‘Software interrupts’ are generated by a program requiring disk input or output.

Classes of Interrupts

Interrupts can be of any of the four types:
Program interrupt: Generated by some condition that occurs as a result of an instruction execution e.g. overflow, division by zero.
Timer interrupt: Generated by a timer within a processor. This allows the operating system to perform certain functions on a regular basis. Used in pre-emptive multitasking (time for a task).
I/O interrupt: Generated by an I/O controller, to signal normal completion of an operation, or to request service from the CPU.
Hardware Failure: Generated by a failure such as power failure or memory parity error (a bit), or to signal a variety of error conditions.

Importance of Interrupts

  • The purpose of interrupts is to improve processing efficiency.
  • For example, most external devices are much slower than the CPU.
  • Suppose that the processor is transferring data to a printer using the ‘instruction cycle’ scheme.
  • After each ‘write’ operation, the processor must pause and remain idle until the printer catches up.
  • The length of this pause may be on the order of many hundreds or even thousands of instruction cycles that do not involve memory.
  • Clearly, this is a very wasteful use of the processor.
  • During the wait cycles, the CPU can work on other tasks until interrupted.

Program Flow Control 

The user program performs a series of WRITE calls to an I/O device interleaved with processing. Code segments 1, 2 and 3 refer to the sequence of instructions that do not involve I/O.
The WRITE calls are to an I/O program (driver) that is a system utility and that will perform the actual I/O operation (to manage & control computer resources).
Because the I/O operation may take a relatively long time to complete, the user program is hung up waiting for the operation to complete; hence the user program is stopped at the point of the WRITE call for some considerable period of time.
what is an interrupt in micro controller

Stages of an I/O Write Operation 

The I/O program consists of three sections:
Section-1: A sequence of instructions, labeled 4 in the figure, to prepare for the actual I/O operation. This may include copying the data to be output into a special buffer and preparing the parameters for a device command.
Section-2: The actual I/O command. Without the use of interrupts (Fig.a), once this command is issued, the program must wait for the I/O device to perform the requested function (or periodically poll the device). The program may wait to determine if the operation is done.
Section-3: A sequence of instructions, labeled 5 in the figure, to complete the operation. This may include setting a flag indicating the success or failure of the operation.

Interrupt Cycle added to Instruction Cycle

  • With interrupts, the processor can be engaged in executing other instructions while an I/O operation is in progress.
  • As before, the user program reaches a point at which it makes a system call in the form of a WRITE call.
  • The I/O program that is invoked in this case consists only of the preparation code and the actual I/O command.
  • After these few instructions have been executed, control returns to the user program.
  • Meanwhile, the external device is busy accepting data from computer memory and printing it.
  • The I/O operation is carried along with the execution of user program.

Interrupt Handler 

When the external device becomes ready to be serviced – that is, when it is ready to accept more data from the processor – the I/O module for the external device sends an interrupt request signal to the processor. The processor responds by suspending the operation of the current program, branching off to a program to service that particular I/O device, known as the interrupt handler. (an OS program)
The processor resumes the original program execution after the device is serviced.
The points at which such interrupts occur are shown with X 

Transfer of Control via Interrupts

From the point of view of the user program, an interrupt is just that: an interruption of the normal sequence of execution.
When the interrupt processing is completed, execution resumes
Thus, the user program does not have to contain any special code to accommodate interrupts; 
The processor and the operating system are responsible for suspending the user program and then resuming it at the same point.
Transfer of Control via Interrupts

Instruction Cycle with ‘Interrupts Cycle'

  • To accommodate interrupts, an interrupt cycle is added to the ‘instruction cycle’.
  • In the interrupt cycle, the processor checks to see if any interrupts have occurred, indicated by the presence of an interrupt signal.
  • If no interrupts are pending, the processor proceeds to the fetch cycle and fetches the next instruction of the current program.  
Instruction Cycle with ‘Interrupts Cycle'

Steps of ‘Interrupt Handler Process’

If an interrupt is pending, the processor does the following:
It suspends execution of the current program being executed and saves its context to the stack. It also saves the current contents of the PC (program counter) that contains the address of the next instruction to be executed.
It sets the program counter to the starting address of the interrupt handler routine. (it determines nature of interrupt and does action).
The processor now proceeds to the fetch cycle and fetches the first instruction in the ‘interrupt handler program’, which will service the interrupt. (this program is part of the operating system).
When the interrupt handler routine is completed, the processor can resume execution of the user program at the point of interruption. 

‘Program Timing’, Short I/O Wait

Timing diagram based on the flow of control.
In Fig. (a), without interrupts, during I/O operations, the processor waits while an I/O operation is performed.
With interrupts, the I/O operation is carried in concurrent with processor executing.
This saves valuable processing time.
Result: Gain in ‘system performance’.
Program Timing’, Short I/O Wait