Interrupts and Polling What are the Interrupt and Polling methods? Interrupt and polling are two methods for an external hardware device to receive the attention of the CPU. In the case of an interrupt, the hardware device notifies the CPU that it requires attention while, in the case of polling, a routine of CPU instructions regularly checks the status of the device to find out whether it requires attention. Sometimes, it is necessary to pause the current task that the CPU is performing and perform some routine that is required by some external hardware device. Two mechanisms to initiate this are interrupts and polling. What is an Interrupt? An interrupt is a signal that indicates that the CPU must immediately execute a routine that is required by some external hardware device. For example, there can be a regular interrupt from of a timer device to indicate that a certain time period has elapsed. This takes the burden of determining that time period away from the CPU, however, when the CPU is notified that the time period has elapsed, it will need to execute some routine, known as an interrupt service routine (ISR). In this case, the ISR might update the system clock or perform a computer screen update. Another example of an interrupt could be a signal from a networking device indicating that a data packet has arrived. When an interrupt occurs, the CPU pauses the task it is currently executing and executes the corresponding routine for the hardware device that is interrupting, which is known as an Interrupt Service Routine (ISR). After handling the interrupt, the CPU resumes the task it was executing when the interrupt occurred. There are two types of interrupts: hardware and software interrupt. Hardware Interrupt A hardware interrupt is when an external hardware device provides an interrupt signal to the CPU that it requires the attention of the CPU. For example, pressing a key on the keyboard triggers a hardware interrupt. It causes the CPU to read that keystroke. The signal that initiates a hardware interrupt is called an interrupt request (IRQ) and is applied to the IRQ pin of a microprocessor. Each hardware device producing an interrupt request signal (IRQ) must also provide an interrupt number to the CPU and this is usually handled by a hardware device called a peripheral interrupt controller (PIC) which has a separate input pin for the IRQ signal coming from each hardware device that can interrupt the CPU. The PIC then outputs a single IRQ signal to the microprocessor IRQ pin as well as an interrupt number (depending on which PIC input pin the IRQ signal occurred) which the microprocessor can read from an I/O port to identify the hardware device that generated the IRQ signal. The CPU can then determine which hardware device needs attention and execute the corresponding ISR. There are two types of hardware interrupts: maskable interrupt and non-maskable interrupt. The CPU can prevent maskable interrupts from occurring whereas the CPU cannot prevent non-maskable interrupts from occurring. The interrupt signal for these two types of hardware interrupt are applied to different pins on the microprocessor. The CPU usually only disables maskable interrupts (this is done by a specific software instruction) while it is busy with a critical task that should not be interrupted. Once the critical task has completed, the CPU will enable the maskable interrupts (with another specific software instruction), which will then allow maskable interrupts to interrupt the CPU. Non-maskable interrupts cannot be disabled by the CPU and this would be, for example, a battery power critically low, where the CPU might just have enough time for an orderly shutdown before the power goes down. Software Interrupt (Exception) A Software Interrupt is an interrupt that is caused by an exceptional condition within the CPU and is also called an exception. Dividing a number by zero will cause a divide by zero exception, which is a software interrupt that will indicate that the CPU should abort the current task that caused the exception. The CPU then executes an ISR that has been specifically written for that exception which will determine the course of action that the CPU will take. Another example of an exception is a CPU instruction that attempts to access a memory location outside the allowable address range for the currently executing program. This will generate a memory access exception. What is Polling? Polling is a software routine where the CPU regularly checks whether a hardware device requires its attention. Since it is mostly used with input/output (I/O) devices, it is also called polled I/O or software driven I/O. For example, a printer may be regularly checked by a print service routine to see if it is ready to receive more data to print. The operating system will regularly execute the print service routine. This I/O is not normally interrupt driven I/O as it is not urgent and therefore better to allow the operating system to handle it synchronously, i.e. at regular intervals. Key differences between Interrupt and Polling An interrupt is an event triggered by a signal from external hardware devices other than the CPU that causes the CPU to perform a certain action. In contrast, polling is a synchronous CPU activity where the CPU checks the status of an external hardware device, as instructed by a software program, to determine whether further CPU action is required for that hardware device. The main difference between interrupt and polling is that for interrupts, a hardware device notifies the CPU that it requires attention, while in polling, the CPU regularly checks the status of hardware devices to find out whether they require attention. Therefore an interrupt is asynchronous and can occur at any time determined by the interrupting device (or exception event) whereas polling is synchronous and occurs at regular intervals determined by software instructions. The method of polling can be more efficient when a hardware device often requires attention but the attention is not urgent whereas the method of interrupt is often more efficient when a hardware device seldom needs attention but the attention is urgent. Polling requires execution by the CPU of software instructions to check whether a hardware device needs attention and this takes CPU time which is wasted when the hardware device seldom requires attention. When interrupts occur, the contents of important CPU registers must be saved so that the task that the CPU was doing when it was interrupted can be resumed after the interrupt service routine has completed. Saving important CPU register contents in memory is the first instructions executed in an ISR and restoring the contents of the CPU registers from the stored values in memory are the last instructions of an ISR and these two processes take CPU time. This process of saving and restoring important CPU register contents that the current CPU task is or might be using is called context saving and can become inefficient if an interrupt occurs very often but is not urgent. Since the interrupt could occur at any time, it cannot be determined which CPU registers the current CPU task would be using and therefore, usually, all CPU registers are saved when an interrupt occurs and restored after the interrupt has been serviced. Efficiency and justification of each method is therefore impacted by how often a hardware device will need attention and how urgent the attention is.