3.3.1.2 Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical sources of interrupts should be identified and any algorithms and data structures should be described).
Interrupts Interrupt An interrupt is a signal from a device (or other source such as a running program) to request processing time from the CPU. An interrupt will cause a break in the execution of the current routine. Virtually all computers provide a mechanism by which a program currently being executed may be interrupted by another process that has an important need for processor time – the process that requires the attention sends an interrupt signal to the processor along one of the control lines. When the processor receives an interrupt, it temporarily suspends the execution of the current program and control of the processor is passed to an interrupt service routine (also known as a handler) – the currently executing program is suspended in such a way that its execution can be resumed without error after the interrupt has been ‘handled’.
Classification of interrupts Interrupts are classified by their source. The most common classes of interrupts are: Program Generated by some condition that occurs as a result of an instruction execution such as arithmetic overflow, division by zero and an illegal memory access (an attempt to use memory that is outside a user’s allowed memory space) Timer Generated by the timer within the processor. This allows the operating system to perform certain functions at regular intervals I/O Generated by an I/O controller (i.e. a peripheral), to signal: input from a user (e.g. a key press or mouse click); completion of an operation (e.g. file transferred, printing completed) an error condition (e.g. printer out of paper); Hardware failure Generated by a failure of a device such as a hard disk or by a memory parity error. A parity error was detected within the system memory. Parity checking is used to detect memory corruption between the time that data is written to memory and the time that it is read back. This error message means that there is a problem associated with the system memory. Depending on the system, you may be told some specifics about what part of memory caused the error. Diagnosis: There are many possible causes of memory parity errors, some of them only related indirectly to the memory, or even having nothing to do with the memory at all. In particular, a memory error at start up is often indicative of a wide variety of possible problems. Recommendation: Diagnose the parity error further here.
Priority of interrupts Some interrupts are for conditions that need immediate attention (e.g. hard disk failure) and others are for conditions that may be ignored for a while (e.g. printer running out of paper). It is, therefore, necessary to assign a priority to each type of interrupt so that when two or more interrupts occur at the same time, the processor can compare interrupt priorities to determine which one to process first. If an interrupt is already being serviced when another interrupt occurs, then interrupts of equal or lower priority must await the completion of the current one. Only an interrupt of a higher priority is allowed to interrupt the servicing of another. In the case of two or more interrupts occurring simultaneously the interrupts are serviced in order of priority. Interrupts caused by hardware failure are given the highest priority in order to protect against data loss.
Examples of interrupt priorities The interrupts with a priority of 1 or 2 must never be ignored; it is not possible for a programmer to disable these interrupts. But some programs disable the keyboard interrupt while they are executing to prevent a process from being cancelled.
Interrupt handling The processor checks for interrupts at the end of every Fetch-Execute cycle. If an interrupt has occurred, it is ‘handled’ and then the processor returns to where it left off:
Interrupt handling Interrupt requests When a device is installed, it is assigned an Interrupt Request (IRQ) number. This number is used when a device wants the attention of the processor: The device (or process) generates an interrupt signal This is sent to the processor along one of the control lines The interrupt flag in the status register is set The IRQ number of the device (or process) is stored in the interrupt register.
Interrupt handling Identifying the interrupt A typical sequence of actions when an interrupt occurs would be: The fetch-execute cycle is completed; The interrupt register is checked to identify the source of the interrupt; The priority of the current task is compared with the priority of the interrupt; If the interrupt has a higher priority: Interrupts of a lower priority are disabled; The interrupt is serviced…
Interrupt handling Servicing the interrupt Each different type of interrupt is associated with a specific Interrupt Service Routine (ISR), also called an interrupt handler – these are the routines that are executed when an interrupt occurs. For example, when you press a key on your keyboard, this triggers the keyboard interrupt handler. This handler will execute, perform its action and then pass back control of the processor to the program that was executing before the interrupt occurred. The complete list of interrupts and their interrupt handlers is stored in a table called the interrupt vector table, which is stored in the first 1 Kbyte of addressable memory.
Interrupt handling Servicing the interrupt The current contents of the program counter, is placed on ‘the stack’; The contents of the other registers are placed on ‘the stack’; The address of the interrupt service routine is obtained and put into the program counter; The interrupt service routine is executed; There is a check for further interrupts… …which are serviced if they have a higher priority that the previously suspended task; Otherwise… … the saved register values (other than the program counter) are restored from the stack; The program counter is restored from the stack and execution of the original task is resumed.