3

I come from the world of web programming and usually the server sets a superglobal variable through the specified method (get, post, etc) that makes available the data a user inputs into a field. Another way is to use AJAX to register a callback method to an event that the AJAX XMLhttpRequest object will initiate once notified by the browser (I'm assuming...). So I guess my question would be if there is some sort of dispatch interface that a systems programmer's code must interact with vicariously to execute in response to user input or does the programmer control the "waiting" process directly? And if there is a dispatch is there a loop structure in an OS that waits for a particular event to occur?

I was prompted to ask this question here because I'm in a basic programming logic class and the professor won't answer such a "sophisticated" question as this one. My book gives a vague pseudocode example like:

    //start
    sentinel_val = 'stop';
    get user_input;
while (user_input not equal to sentinel_val)
     {
         // do something.
         get user_input;
     }
     //stop

This example leads me to believe 1) that if no input is received from the user the loop will continue to repeat the sequence "do something" with the old or no input until the new input magically appears and then it will repeat again with that or a null value. It seems the book has tried to use the example of priming and reading from a file to convey how a program would get data from event driven input, no?

I'm confused :(

Cian E
  • 1,079
  • 1
  • 8
  • 9
  • 4
    I don't know whether to laugh or cry. –  Feb 18 '10 at 01:17
  • +1 @Neil, sometimes I wish SO had a way for me to write the answer I want to write without being downvoted into oblivion. – Carl Norum Feb 18 '10 at 01:23
  • 1
    @Carl Why not try? No downvotes here that I can see. –  Feb 18 '10 at 01:26
  • Ben and Codism got this right. You'll be interested in reading about interrupts and NOP commands http://en.wikipedia.org/wiki/Interrupt http://en.wikipedia.org/wiki/NOP – Dolph Feb 18 '10 at 01:26
  • Thank you very much everyone, especially Ben and Tom, for the answers. I guess I'm taking away that the highest and lowest levels of software abstraction are asynchronous. A high level event handler will temporarily stop its process and be stored in a memory stack until a hardware interrupt is made (via its api), triggered by new input, forces the cpu to store in memory its data. This data will then be made available to the handler(s) immediately if the input type corresponds and the process will be reinitiated from the OS's process stack stored in memory...if I understood correctly. – Cian E Feb 18 '10 at 02:30
  • Yeah...Sort of. :) There's a lot to get through to understand how this all hangs together. If you're really interested, the O'Reilly book "Understanding the Linux Kernel" is a great code-centric overview of how modern operating systems are put together. The Tanenbaum book Modern Operating Systems is broader, but more academic. – Ben Zotto Feb 18 '10 at 02:54

4 Answers4

5

At the lowest level, input to the computer is asynchronous-- it happens via "interrupts", which is basically something external to the CPU (a keyboard controller) sending a signal to the CPU that says "stop what you're doing and accept this data". (It's complex, but this is the general idea). So the CPU stops, grabs the keystroke, and puts it in a buffer to be read, and then continues doing what it was doing before the interrupt.

Very similar things happen with inbound network traffic, and the results of reading from a disk, etc.

At a higher level, it gets more dependent on the operating system or framework that you're using.

With keyboard input, there might be a process (application, basically) that is blocked, waiting for user input. That "block" doesn't mean the computer just sits there waiting, it lets other processes run instead. But when the keyboard result comes in, it will wake up the one who was waiting for it.

From the point of view of that waiting process, they called some function "get_next_character()" and that function returned with the character. Etc.

Frankly, how all this stuff ties together is super interesting and useful to understand. :)

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
2

An OS is driven by hardware event (called interrupt). An OS does not wait for an interrupt, instead, it execute a special instruction to put the CPU a nap in a loop. If a hardware event occurs, the corresponding interrupt will be invoked.

Codism
  • 5,928
  • 6
  • 28
  • 29
2
It seems the book has tried to use the example of priming and reading from a file
to convey how a program would get data from event driven input, no?

Yes that is what the book is doing. In fact... the unix operating system is built on the idea of abstracting all input and output of any device to look like this.

In reality most operating systems and hardware make use of interrupts that jump to what we can call a sub-routine to perform the low level data read and then return control back to the operating system.

Also on most systems many of the devices work independent of the rest of the operating system and present a high level API to the operating system. For example a keyboard port (or maybe a better example is a network card) on a computer process interrupts itself and then the keyboard driver presents the operating system with a different api. You can look at standards for devices to see what these are. If you want to know the api the keyboard port presents for example you could look at the source code for the keyboard driver in a linix distro.

Hogan
  • 69,564
  • 10
  • 76
  • 117
1

A basic explanation based on my understanding...

Your get user_input pseudo function is often something like readLine. That means that the function will block until the data read contains a new line character.

Below this the OS will use interrupts (this means it's not dealing with the keyboard unessesarily, but only when required) to allow it to respond when it the user hits some keys. The keyboard interrupt will cause execution to jump to a special routine which will fill an input buffer with data from the keyboard. The OS will then allow the appropriate process - generally the active one - to use readLine functions to access this data.

There's a bunch more complexity in there but that's a simple view. If someone offers a better explanation I'll willingly bow to superior knowledge.

Tom Duckering
  • 2,727
  • 1
  • 23
  • 27
  • Thanks, this answer provided me that initial lightbulb or entry point for more detailed answers. – Cian E Feb 18 '10 at 01:47