Interrupts and Polling
COS2621 - Computer Organisation · Input and Output Systems
Interrupts and Polling
In computer systems, input and output (I/O) operations are essential for communication between the computer and external devices. Two primary methods for managing I/O operations are interrupts and polling. Understanding these concepts is crucial for grasping how computers handle multiple tasks efficiently.
What are Interrupts?
An interrupt is a signal sent to the processor by hardware or software indicating that an event needs immediate attention. When an interrupt occurs, the processor temporarily halts its current activities and executes a special routine called an interrupt handler or interrupt service routine (ISR). After completing the ISR, the processor resumes its previous task.
Remember: Interrupts allow the CPU to respond quickly to events, improving efficiency in handling I/O operations.
Types of Interrupts
Interrupts can be classified into several categories:
- Hardware Interrupts: These are generated by hardware devices, such as keyboards, mice, or network cards, to signal that they require processing. For example, when a user presses a key, the keyboard sends a hardware interrupt to the CPU.
- Software Interrupts: These are generated by programs when they require the operating system's services. For instance, a program may request to read from a file, which triggers a software interrupt.
- Timer Interrupts: These interrupts occur at regular intervals, allowing the operating system to perform scheduled tasks, such as updating system time or managing task scheduling.
How Interrupts Work
When an interrupt occurs, the following steps take place:
- The CPU finishes executing the current instruction.
- The CPU saves the context of the current process, which includes the program counter and other registers.
- The CPU determines the source of the interrupt and jumps to the corresponding ISR.
- After executing the ISR, the CPU restores the saved context.
- The CPU resumes execution of the interrupted process.
Example of an Interrupt
Consider a simple example where a user presses a key on a keyboard:
- The keyboard generates a hardware interrupt signal to the CPU.
- The CPU completes the current instruction and saves its context.
- The CPU identifies the interrupt source (the keyboard) and executes the ISR for keyboard input.
- The ISR reads the key pressed and stores it in a buffer for the program to access later.
- Once the ISR completes, the CPU restores its context and continues executing the interrupted program.
What is Polling?
Polling is another method used by the CPU to manage I/O operations. In polling, the CPU repeatedly checks the status of an I/O device at regular intervals to determine if it requires attention. Unlike interrupts, polling does not rely on the device to signal the CPU.
Remember: Polling can lead to inefficient CPU usage, as it wastes processing time checking devices that may not need attention.
How Polling Works
The polling process involves the following steps:
- The CPU sends a request to an I/O device to check its status.
- The device responds with its status, indicating whether it is ready for data transfer or not.
- If the device is ready, the CPU performs the necessary read or write operation.
- The CPU repeats this process at regular intervals until the device signals that it is ready.
Example of Polling
Consider a scenario where a CPU needs to check if a printer is ready to print:
- The CPU sends a status request to the printer.
- The printer responds, indicating whether it is ready or busy.
- If the printer is busy, the CPU waits for a short time and checks again.
- This process continues until the printer indicates it is ready, at which point the CPU sends the print job.
Interrupts vs. Polling
Both interrupts and polling have their advantages and disadvantages. Here are some key differences:
| Aspect | Interrupts | Polling |
|---|---|---|
| Efficiency | More efficient as the CPU only responds when needed | Less efficient as the CPU checks devices at regular intervals |
| Response Time | Faster response time to events | Slower response time, especially if checks are infrequent |
| Complexity | More complex to implement due to the need for ISRs | Simpler to implement as it involves straightforward status checks |
| CPU Utilisation | Better CPU utilisation as the CPU can perform other tasks while waiting for interrupts | Wastes CPU cycles when devices are not ready |
Common Mistakes
Watch out: Students often confuse interrupts with polling. Remember that interrupts allow the device to signal the CPU, while polling requires the CPU to check the device status continuously.
Conclusion
Interrupts and polling are essential techniques for managing I/O operations in computer systems. Interrupts provide a more efficient way to handle events, allowing the CPU to respond quickly when necessary. Polling, while simpler, can lead to inefficient CPU usage. Understanding these concepts is crucial for designing effective computer systems.
Check your understanding
- What is the primary function of an interrupt in a computer system?
- Describe the steps involved when an interrupt occurs.
- What are the advantages and disadvantages of polling compared to interrupts?
- Explain a scenario where polling might be preferred over interrupts.