Polling vs Interrupt
Asked Answered
E

5

7

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"

Enterectomy answered 30/4, 2012 at 18:34 Comment(0)
S
7

@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).

Studbook answered 21/5, 2013 at 15:44 Comment(0)
B
3

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.

Baluchistan answered 30/4, 2012 at 18:37 Comment(5)
Hi, Thanks for the answer, but I am a little confused. What exactly is the meaning of "CPU is interrupted". How can the CPU know it needs to service an interrupt without checking for itEnterectomy
The same way you can know when the phone is ringing without checking for it. The ringing interrupts you. Your brain is constructed such that when the phone rings, whatever it's doing, it is interrupted and notices that the phone is ringing. You don't have to poll the phone to see if it's ringing.Baluchistan
The phone ringing example exactly depicts my confusion. Doesn't it mean that my brain is "polling" the neurons attached to my ear unconsciously.Enterectomy
If it's unconscious, then it's not polling. The crux of polling is that it's an active process.Baluchistan
To further the analogy a little, you can be doing any number of things with your hands while waiting to hear the ring. If the ringer failed, you would be constantly using your hands to pick up the phone and listen to see if anyone was there, if you didn't pick up and listen frequently enough you might miss a phone call or in microcontroller terms an event.Harbird
C
1

"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)

Cognate answered 30/4, 2012 at 20:1 Comment(0)
M
-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

Mendenhall answered 18/11, 2015 at 18:59 Comment(0)
B
-1

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.

Biodynamics answered 3/2, 2016 at 1:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.