# Interrupt-Based Input/Output (I/O)

## Interrupt-Based Input/Output (I/O)

### Problem with Programmed I/O

In programmed I/O, the CPU continuously polls the I/O device to check if it is ready to transfer data. This polling process is inefficient because it consumes significant CPU time, preventing the CPU from performing other tasks while waiting for the device.

### Solution: Interrupt-Based I/O

Interrupt-based I/O addresses the inefficiency of programmed I/O by allowing the CPU to initiate an I/O operation and then proceed with other tasks. The I/O device operates independently and signals the CPU with an interrupt when the operation is complete or requires attention.

### How Interrupt-Based I/O Works

1. **Initiation**: The CPU sends a command to the I/O device to start an operation (e.g., reading data from a disk).
2. **Independent Operation**: The I/O device performs the requested operation without CPU involvement.
3. **Interrupt Generation**: When the device completes the operation or encounters an event (e.g., data is ready), it sends an interrupt signal to the CPU.
4. **Interrupt Handling**: The CPU temporarily pauses its current task, saves its state, and executes an **Interrupt Service Routine (ISR)** to handle the I/O event.
5. **Resumption**: After the ISR completes, the CPU restores its previous state and resumes the interrupted task.

### Example

Consider a keyboard input scenario:

* In programmed I/O, the CPU repeatedly checks if a key has been pressed, wasting cycles.
* In interrupt-based I/O, the CPU does not monitor the keyboard. When a key is pressed, the keyboard generates an interrupt. The CPU then executes an ISR to process the keypress (e.g., storing the character in memory) and resumes its prior task.

### Types of Interrupts

Interrupts can originate from various sources, categorized as follows:

| **Type**     | **Description**                           | **Examples**                                 |
| ------------ | ----------------------------------------- | -------------------------------------------- |
| **Hardware** | Generated by external devices.            | Keyboard press, mouse click, printer status. |
| **Software** | Triggered by software events or requests. | System calls, software exceptions.           |
| **Internal** | Caused by CPU or processor events.        | Divide-by-zero error, arithmetic overflow.   |

### Advantages

* **Efficiency**: The CPU is not blocked while waiting for I/O operations, allowing it to perform other computations.
* **Resource Utilization**: Frees up CPU cycles for multitasking or other processes.
* **Real-Time Suitability**: Ideal for real-time systems where timely responses to events (e.g., sensor inputs) are critical.
* **Scalability**: Supports multiple devices, each capable of generating interrupts as needed.

### Comparison with Other I/O Methods

| **Method**                     | **Description**                                                             | **CPU Time Utilization**             |
| ------------------------------ | --------------------------------------------------------------------------- | ------------------------------------ |
| **Programmed I/O**             | CPU continuously polls device until I/O is complete.                        | High: CPU is fully occupied.         |
| **Interrupt-Based I/O**        | Device notifies CPU via interrupts when ready.                              | Medium: CPU only acts on interrupts. |
| **Direct Memory Access (DMA)** | Device transfers data directly to/from memory with minimal CPU involvement. | Low: CPU only initiates transfer.    |

