Thread: How do applications interact with the hardware?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    21

    Question How do applications interact with the hardware?

    I understand that when a program runs, it stores data (like variables) and also instructions which act upon that data in the RAM. The CPU executes the instructions, and the data is played around with according to those instructions. (I am not talking about special-cases or reflective programming languages that treat instructions as data).

    My question is this: how does this translate into an actual hardware output and input? How does the manipulation of the data done by the instructions actually generate a monitor/speaker output, or receive keyboard/mouse input?

    After doing some research I figured out that it has to do with the underlying OS, and that the OS acts as a "bridge" between the application and the hardware, but I didn't connect all the pieces of the puzzle yet. What exactly happens behind the scene when we call Printf(); or a similiar function?

    Topics that may be (or may not be) related:
    Windows API - Wikipedia, the free encyclopedia
    OpenGL - Wikipedia, the free encyclopedia

    Any shared thoughts would be appreciated.

  2. #2
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    I'm no expert, but maybe this is what you're looking for?
    How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    On x86 there are two main methods for communication with hardware: IN and OUT instructions which read/write data on the I/O address space; and memory-mapped I/O which communicates with a piece of hardware through a range of memory addresses. In addition to these, most devices use interrupts to communicate asynchronously with the CPU(s).

    All of this is mastered and controlled by the OS itself. Normally, user programs don't have the correct hardware privileges to do these things. System-level software usually does it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by butteflymentor View Post
    What exactly happens behind the scene when we call Printf()
    Printf() just sends data to the process's standard output. That's a pipe, probably governed by the parent process, a shell. In turn, the parent process of the shell is probably some kind of console/terminal application.

    This all happens in userland.

    User space - Wikipedia, the free encyclopedia

    WRT to the printf example, you have some more userland layers depending upon more context (eg, if this is a terminal emulator on a GUI desktop), but eventually, something has to make a system call:

    System call - Wikipedia, the free encyclopedia

    Because, as you've recognized, the kernel is the gatekeeper of the hardware, and contains all the relevant drivers, etc. System calls make use of a kernel level API.
    Last edited by MK27; 04-16-2012 at 01:30 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Interact with websites
    By geniusisgenius in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2010, 10:02 AM
  3. Get Installed applications list and applications activity
    By arunarora in forum C++ Programming
    Replies: 5
    Last Post: 05-25-2009, 09:41 AM
  4. interact with web sites
    By awnjoor in forum C Programming
    Replies: 8
    Last Post: 07-26-2006, 07:10 PM
  5. how to interact with hardware
    By askme in forum C Programming
    Replies: 3
    Last Post: 03-26-2003, 12:28 PM