0
0
Operating-systemsComparisonBeginner · 4 min read

Polling vs Interrupt: Key Differences and When to Use Each

In an operating system, polling is a method where the CPU repeatedly checks the status of a device to see if it needs attention, while an interrupt is a signal sent by a device to the CPU to immediately get its attention. Polling wastes CPU time by constant checking, whereas interrupts allow the CPU to work on other tasks until notified.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of polling and interrupts based on key factors.

FactorPollingInterrupt
CPU UsageHigh, because CPU checks repeatedlyLow, CPU works until interrupted
Response TimeSlower, depends on polling frequencyFaster, immediate attention
ComplexitySimple to implementMore complex to handle
EfficiencyInefficient for rare eventsEfficient for sporadic events
Use CaseGood for simple or predictable devicesBest for devices needing quick response
⚖️

Key Differences

Polling is a technique where the CPU actively and repeatedly checks the status of an input/output device to see if it requires processing. This means the CPU spends time in a loop, checking the device even if it does not need service, which can waste valuable processing time.

In contrast, an interrupt is a signal sent by a device to the CPU to indicate it needs immediate attention. When an interrupt occurs, the CPU pauses its current task, saves its state, and executes a special function called an interrupt handler to deal with the device's request. After handling, the CPU resumes its previous work.

Polling is simpler but less efficient because it uses CPU cycles continuously, while interrupts allow the CPU to perform other tasks and only respond when necessary, making them more efficient for devices that generate events unpredictably.

💻

Polling Code Example

This example shows how polling checks a device status in a loop until it is ready.

c
while (true) {
    if (device_is_ready()) {
        process_device_data();
        break;
    }
    // CPU keeps checking repeatedly
}
Output
Processes device data once device is ready
↔️

Interrupt Equivalent

This example shows how an interrupt handler responds when the device signals readiness.

c
void interrupt_handler() {
    if (device_interrupt_occurred()) {
        process_device_data();
    }
}

// CPU runs other tasks and interrupt_handler is called automatically on interrupt
Output
Processes device data immediately when interrupt occurs
🎯

When to Use Which

Choose polling when the device status changes frequently and predictably, or when simplicity is more important than efficiency. It works well for simple devices or during early development.

Choose interrupts when devices generate events unpredictably or require fast response times, as interrupts allow the CPU to perform other tasks and only handle device requests when necessary, improving overall system efficiency.

Key Takeaways

Polling wastes CPU time by constantly checking device status.
Interrupts notify the CPU only when attention is needed, saving resources.
Use polling for simple, predictable device checks.
Use interrupts for efficient handling of sporadic or urgent events.
Interrupts require more complex programming but improve system performance.