An interrupt is a signal sent to the CPU from either hardware or software that temporarily halts the CPU's current activities and allows a special routine, known as an interrupt handler or interrupt service routine (ISR), to run. The CPU saves its current state, switches to the ISR, and resumes its previous task afterward.

Interrupts are vital for efficient multitasking, allowing the CPU to respond quickly to events like input from a keyboard, mouse, or hardware failures without polling these devices.

Polling

Polling is a technique where the CPU continuously checks the status of a device or resource at regular intervals to see if it requires attention. Unlike interrupts, where the device sends a signal to the CPU when it’s ready, polling requires the CPU to actively query the device in a loop.

How it works

  1. The CPU sends a request to the device (e.g., asking if data is ready).
  2. The CPU checks the device's status register to see if the request is completed.
  3. If the device is ready, the CPU processes the data; if not, it keeps checking (polling) until the device is ready.

Types of interrupts

Hardware Interrupts

These are generated by hardware devices like keyboards, network cards, or timers. They are asynchronous, meaning they occur independently of the CPU's current execution. When a device needs attention (e.g., key press, packet arrival), it sends a signal to the CPU to trigger an interrupt.

Software Interrupts

These are generated by software or the operating system itself and are triggered by executing special instructions. Software interrupts are often used for system calls, where an application requests services from the OS.

Exceptions

These occur when CPU detects an error or specific condition during execution, such as division by zero or an invalid memory access. Exceptions are often treated similarly to interrupts but are generated by the CPU itself.

How it works

3-s2.0-B9780128094488000060-f06-09-9780128094488.jpg

  1. Interrupt Signal: A device or software generates an interrupt signal.