Polling Vs Interrupts
We may have many external devices attached to the CPU like a proximity sensor, temperature sensor, buttons, etc. These devices also need CPU attention.
Lets take the example of a computer. The processor in a computer is busy in displaying a PDF and you click the window media player icon on the desktop. Though the CPU does not have any idea when an event like this would occur, but it has to respond to such inputs from the I/O devices.
Interrupt and Polling are the two ways to handle the events generated by the devices that can happen at any moment while CPU is busy in executing another process.
Polling and Interrupt are different from each other in many aspects. But the basic point that distinguishes Polling and Interrupt is that in polling CPU keeps on checking I/O devices at regular interval whether it needs CPU service, whereas, in interrupt, the I/O device interrupts the CPU and tells CPU that it need CPU service. Some differences between Interrupt and Polling in the comparison are mentioned in the chart below, please have a look.
|BASIS FOR COMPARISON||INTERRUPT||POLLING|
|Basic||Device notify CPU that it needs CPU attention.||CPU constantly checks device status whether it needs CPU’s attention.|
|Mechanism||An interrupt is a hardware mechanism.||Polling is a Protocol.|
|Servicing||Interrupt handler services the Device.||CPU services the device.|
|Indication||Interrupt-request line indicates that device needs servicing.||Comand-ready bit indicates the device needs servicing.|
|CPU||CPU is disturbed only when a device needs servicing, which saves CPU cycles.||CPU has to wait and check whether a device needs servicing which wastes lots of CPU cycles.|
|Occurrence||An interrupt can occur at any time.||CPU polls the devices at regular interval.|
|Efficiency||Interrupt becomes inefficient when devices keep on interrupting the CPU repeatedly.||Polling becomes inefficient when CPU rarely finds a device ready for service.|
|Example||Let the bell ring then open the door to check who has come.||Constantly keep on opening the door to check whether anybody has come.|
Content: Interrupt Vs Polling
Definition of Interrupt
An interrupt is a hardware mechanism that enables CPU to detect that a device needs its attention. The CPU has a wire interrupt-request line which is checked by CPU after execution of every single instruction. When CPU senses an interrupt signal on the interrupt-request line, CPU stops its currently executing task and respond to the interrupt send by I/O device by passing the control to interrupt handler. The interrupt handler resolves the interrupt by servicing the device.
Although CPU is not aware when an interrupt would occur as it can occur at any moment, but it has to respond to the interrupt whenever it occurs.
When the interrupt handler finishes executing the interrupt, then the CPU resumes the execution of the task that it has stopped for responding the interrupt. Software, hardware, user, some error in the program, etc. can also generate an interrupt. Interrupts handling nature of CPU leads to multitasking, i.e. a user can perform a number of different tasks at the same time.
If more than one interrupts are sent to the CPU, the interrupt handler helps in managing the interrupts that are waiting to be processed. As interrupt handler gets triggered by the reception of an interrupt, it prioritizes the interrupts waiting to be processed by the CPU and arranges them in a queue to get serviced.
Definition of Polling
As we have seen in interrupts, the input from I/O device can arrive at any moment requesting the CPU to process it. Polling is a protocol that notifies CPU that a device needs its attention. Unlike in interrupt, where device tells CPU that it needs CPU processing, in polling CPU keeps asking the I/O device whether it needs CPU processing.
The CPU continuously test each and every device attached to it for detecting whether any device needs CPU attention. Every device has a command-readybit which indicates the status of that device i.e. whether it has some command to be executed by CPU or not. If command bit is set 1, then it has some command to be executed else if the bit is 0, then it has no commands. CPU has a busy bitthat indicates the status of CPU whether it is busy or not. If the busy bit is set 1, then it is busy in executing the command of some device, else it is 0.
Algorithm for polling
- When a device has some command to be executed by CPU it continuously checks the busy bit of CPU until it becomes clear (0).
- As the busy bit becomes clear, the device set write-bit in its command register and writes a byte in data-out register.
- Now the device sets (1) the command-ready bit.
- When CPU checks the devices command-ready bit and finds it set (1), it sets (1) its busy bit.
- The CPU then reads the command register of the device and executes the command of the device.
- After command execution, CPU clears(0) the command-ready bit, error bit of the device to indicate successful execution of the command of the device and further it clears (0) its busy bit also to indicate that the CPU is free to execute the command of some other device.
Key Differences Between Interrupt and Polling
- In interrupt, the device notifies the CPU that it needs servicing whereas, in polling CPU repeatedly checks whether a device needs servicing.
- Interrupt is a hardware mechanism as CPU has a wire, interrupt-request line which signal that interrupt has occurred. On the other hands, Polling is a protocol that keeps checking the control bits to notify whether a device has something to execute.
- Interrupt handler handles the interrupts generated by the devices. On the other hands, in polling, CPU services the device when they require.
- Interrupts are signalled by the interrupt-request line. However, Command-ready bit indicate that the device needs servicing.
- In interrupts, CPU is only disturbed when any device interrupts it. On the other hand, in polling, CPU waste lots of CPU cycles by repeatedly checking the command-ready bit of every device.
- An interrupt can occur at any instant of time whereas, CPU keeps polling the device at the regular intervals.
- Polling becomes inefficient when CPU keeps on polling the device and rarely finds any device ready for servicing. On the other hands, interrupts become inefficient when the devices keep on interrupting the CPU processing repeatedly.
To help you understand better, here is a video. In the video, in one scenario, Mario keeps going to the Princess at regular intervals and asks her if she will marry him. That is Polling
In another scenario, the Princess comes to Mario when she is ready to marry him. That is Interrupt.
Other posts by Vivek