views:

467

answers:

6

I was wandering if there's a way in witch the OS doesn't need to cycle ad infinitum waiting for the input from keyboard (or other input device) and if there are any OS using that. I can't believe that we do need to waste cycling just to wait for input, why can't the input do something once is pressed instead of having the machine to wait for the action.

Long story short:

How keyboard input is currently handled - polling or interrupts?

+2  A: 

You can just check keyboard input using a non blocking function and if there is nothing, cooperatively yield, using sleep(1) or similar construct.

You don't need to waste the CPU you've been scheduled.

It's totally possible a blocking keyboard function might yield by itself and the OS will only resume the thread once it has input.

Coincoin
+9  A: 

Most modern programs don't get input in a loop as you describe. You can use event handling or interrupts to avoid wasted cycles.

Bill the Lizard
The program isn't sitting in a loop, waiting for something to happen - but the operating system might be.
Steve Melnikoff
A: 

The only time I've seen polling is when dealing with a raw DirectX input device (since they fall outside the scope of the typical H/W architecture). You have to poll those devices (which could be mice, keyboards, joysticks, gamepads, etc...) to get their current state. As everyone else has stated, the typical keyboard is handled via interrupts.

Erich Mirabal
Even those devices you mention report changes in their status to the OS using interrupts (the obsolete PC joystick port actually did require active polling to read). The DirectX API just allows polling the latest states, which is sometimes more convenient.
TrayMan
+7  A: 

All device input from all devices on all Linux-based OS is all interrupt-driven. Busy waiting (active polling) for data is not used.

Windows is probably all interrupt-driven, also. (Windows has that DOS legacy hiding inside it -- polling may still happen in there.)

All of Linux works the same way. The kernel waits for interrupts, queues up the interrupts and checks the scheduler to handle the highest-priority interrupt next. Process scheduling is always lower priority than interrupt scheduling.

The keyboard interrupts are handled by a driver which buffers the information. A window manager (Gnome, for example) fetches stuff from the buffer to create a stream of keyboard interrupts.

You can buy numerous really good books on OS design that cover the relationship between device drivers and the kernel. Start with http://lwn.net/Kernel/LDD3/

Clock interrupts, BTW, are how process scheduling happens. Absent any device activity, the clock will interrupt periodically, forcing the kernel to look at the schedule, possibly changing which process is executing. A process that uses a lot of CPU has it's priority lowered. A process that does a lot of I/O spends most of it's time waiting for I/O to finish, so it's priority is raised.


Edit

Also, there are -- sometimes -- DMA devices which bypass kernel interrupt handling for block transfers of bytes. An interrupt initiates the transfer, but the device lives on the bus and accesses memory directory. Video displays, disks (and in the olden times, network devices) may be DMA. Keyboards, however, are so low volume that DMA isn't a helpful optimization.

S.Lott
"All device input from all devices on all Linux-based OS is all interrupt-driven." This is techincally correct, but the interrupts do not always come from the device whose input is being read. Linux kernel sometimes uses polling to periodically check a device status, in which case the interrupt comes from the system timer.
Rafał Dowgird
+3  A: 

Usually it goes like this:

  • a process (an application) executes a (blocking) system call meaning "get me a keypress"
  • the OS puts the process in the "waiting for IO" state, kicking it off the CPU
  • ... time passes, other processes happily run, disks spin, lights blink ...
  • user presses a key, an interrupt is generated
  • the OS gets the interrupt, reads the keypress, checks if there are any processes waiting for this particular device (keyboard) input
  • finds the waiting process, moves it to "runnable" state
  • as soon as there is a free CPU, the process gets it and resumes its execution after the system call

So, there is no polling (active wait) at any stage.

Edit: As far as I remember, the Linux kernel sometimes switches to polling for devices that would otherwise flood it with interrupts (think fast network card receiving a huge number of packets). Under these conditions, it saves CPU time instead of wasting it - OS gets a large chunk of data with one poll instead of many small chunks with many interrupts. When polling no longer gets any data, the OS switches back to waiting-for-intterupt mode.

Rafał Dowgird
... unless you are using a USB keyboard, in which case there is plenty of polling.
joeforker
There is? Why is that? PS/2 keyboard have less polling?
LuRsT
USB devices cannot generate interrupts, they have to be polled by the operating system which periodically asks them if they have any new data. PS/2 keyboards do generate an interrupt when there is new data. Of course the application does not need to do any polling.
joeforker
@joeforker: Sure about that? I've just grepped /proc/interrupts for usb: 16: 135 IO-APIC-fasteoi uhci_hcd:usb4, HDA Intel 17: 109098658 IO-APIC-fasteoi uhci_hcd:usb3, eth1 18: 0 IO-APIC-fasteoi uhci_hcd:usb1, ehci_hcd:usb5 19: 0 IO-APIC-fasteoi uhci_hcd:usb2, libataSeems that there are some usb drivers registered for interrupts...
Rafał Dowgird
PS. Sorry for the poor formatting
Rafał Dowgird
http://www.codinghorror.com/blog/archives/000832.html
joeforker
I found that suprising but it appears to be true: although the USB host controller generates interrupts, the USB _device_ itself cannot generate an interrupt spontaneously without the host sending a "would you like an interrupt now?" message.
pjc50
@pjc50: That is my understanding of the mechanism, too.
Rafał Dowgird
A: 

USB devices cannot generate interrupts and are therefore polled by the operating system many times per second. Gamers sometimes like to increase this polling frequency to get a more responsive mouse.

Games often use polling to handle input, but other applications are far more likely to sleep until the operating system sends them input.

joeforker