Pratt
Pratt

Reputation: 861

Polling vs Interrupt

I have a basic doubt regarding interrupts. Imagine a computer that does not have any interrupts, so in order for it to do I/O the CPU will have to poll* the keyboard for a key press, the mouse for a click etc at regular intervals. Now if it has interrupts the CPU will keep checking whether the interrupt line got high( or low) at regular intervals. So how is CPU cycles getting saved by using interrupts. As per my understanding instead of checking the device now we are checking the interrupt line. Can someone explain what basic logic I am getting wrong.

*Here by polling I don't mean that the CPU is in a busy-wait. To quote Wikipedia "Polling also refers to the situation where a device is repeatedly checked for readiness, and if it is not the computer returns to a different task"

Upvotes: 7

Views: 12500

Answers (5)

There are two different methods(Polling & interrupt) to serve I/O of a computer system. In polling, CPU continuously remain busy, either an input data is given to an I/O device and if so, then checks the source port of corresponding device and the priority of that input to serve it.

In Interrupt driven approach, when a data is given to an I/O device, an interrupt is generated and CPU checks the priority of that input to serve it.

Upvotes: -1

Huda Butt
Huda Butt

Reputation: 1

Interrupt: An event generated by a device in a computer to get attention of the CPU. Provided to improve processor utilization. To handle an interrupt, there is an Interrupt Service Routine (ISR) associated with it. To interrupt the processor, the device sends a signal on its IRQ line and continue doing so until the processor acknowledges the interrupt. CPU then performs a context switch by pushing the Program Status Word (PSW) and PC onto the control stack. CPU executes the ISR. whereas Pooling is the process where the computer waits for an external device to check for it readiness. The computer does not do anything else than check the status of the device Polling is often used with low-level hardware Example: when a printer connected via a Parrnell port the computer waits until the next character has been received by the printer. These process can be as minute as only reading 1 Byte

Upvotes: -1

Dan
Dan

Reputation: 7737

@David Schwartz and @RKT are right, it doesn't take any CPU cycles to check the interrupt line.

Basically, the processor has a set of interrupt wires which are connected to a bunch of devices. When one of the devices has something to say, it turns its interrupt wire on, which triggers the processor (without the help of any software) to pause the execution of current instructions and start running a handler function.

Here's how it works. When the operating system boots, it registers a set of callbacks (a table of function pointers, actually) with the processor using a special instruction which takes the address of the first entry of the table. When interrupt N is triggered, the processor pulls the Nth entry from the table and runs the code at the location in memory it refers to. The code inside the function is written by the OS authors in assembly, but typically all it does is save the state of the stack and registers so that the current task can be resumed after the interrupt handler has been called and then call a higher-level common interrupt handler which is written in C and that handles the logic of "If this a page fault, do X", "If this is a keyboard interrupt, do Y", "If this is a system call, do Z", etc. Of course there are variations on this with different architectures and languages, but the gist of it is the same.

The idea with software interrupts ("signals", in Unix parlance) is the same, except that the OS does the work of setting up the stack for the signal handler to run. The basic procedure is that the userland process registers signal handlers one at a time to the OS via a system call which takes the address of the handler function as an argument, then some time in the future the OS recognizes that it should send that process a signal. The next time that process is run, the OS will set its instruction pointer to the beginning of the handler function and save all its registers to somewhere the process can restore them from before resuming the execution of that process. Usually, the handler will have some sort of routing logic to alert the relevant bit of code that it received a signal. When the process finishes executing the signal handler, it restores the register state that existed previous to the signal handler running, and resumes execution where it left off. Hence, software interrupts are also more efficient than polling for learning about events coming from the kernel to this process (however this is not really a general-use mechanism since most of the signals have specific uses).

Upvotes: 7

RKT
RKT

Reputation: 187

"CPU is interrupted" : It will leave (put on hold) the normal program execution and then execute the ISR( interrupt subroutine) and again get back to execution of suspended program.

CPU come to know about interrupts through IRQ(interrupt request) and IF(interrupt flag)

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182759

It doesn't take any CPU cycles to check the interrupt line. It's done by dedicated hardware, not CPU instructions. The reason it's called an interrupt is because if the interrupt line is asserted, the CPU is interrupted.

Upvotes: 3

Related Questions