Skip to content
Step 8 of 105–7 minutes

How Does a Program Interact With Hardware?

A program that only does calculations in CPU registers is completely useless if it cannot communicate with the outside world.Input / Output (I/O) is how software accepts data from you (keyboard, mouse, microphone) and delivers results back to you (screen, speakers, SSD, network).

However, your program is strictly forbidden from touching physical hardware wires directly. All hardware communication must go through System Calls and Device Drivers managed by the Operating System.

User Mode vs Kernel Mode Security

Why can't your C code or Python script send signals directly to the monitor or SSD? Because if any student or website could manipulate hardware directly, a buggy program could write garbage over your disk or spy on your keystrokes. The CPU operates in User Mode (unprivileged). Whenever it needs hardware, it asks the OS Kernel (privileged mode) via a System Call.

The Bank Teller Analogy

Think of a bank: Customers are not allowed to walk into the back vault and grab cash from the safe! Instead, you walk up to a teller counter (the System Call API), slide your withdrawal slip through the glass window, and the teller (the OS Kernel) verifies your identity and retrieves the money for you.

The Journey of a Keypress & printf

Hardware-to-Application I/O Pipeline

1. Physical Device

You press the 'A' key on keyboard (closes electrical switch).

2. Device Driver

Hardware chip sends scan code. Driver translates it into character 'A'.

3. OS System Call

Kernel verifies app permissions and passes data across security boundary.

User Program receives char 'A' → Calls printf("Result: %d", 42)
Monitor Driver updates GPU framebuffer → Pixels draw "42"

How Does Hardware Notify the CPU? Interrupts vs. Polling

How does the CPU know you clicked your mouse or tapped a key? There are two possible strategies:

1. Polling (The Inefficient Way)

Continuous Asking

The CPU runs in a tight loop repeatedly asking the keyboard: "Did you press a key yet? Did you press a key yet?"This burns 100% of CPU power, drains batteries, and prevents the CPU from doing any useful calculations.

2. Hardware Interrupts (The Modern Way)

Signal When Ready

The CPU completely ignores the keyboard. When you press a key, the keyboard hardware sends an electrical pulse down a dedicated wire called the Interrupt Line. The CPU immediately pauses, executes an Interrupt Handler in the OS Kernel to collect the key, and resumes where it left off.

Blocking vs Non-Blocking I/O

I/O operations (like reading from an SSD or fetching from the Internet) are millions of times slower than the CPU:

Traditional Synchronous / Blocking Mode (e.g. standard C scanf / read)

When your program executes scanf() or reads from a file, the OS puts your program to sleep (Suspended state). Your program does not execute another single instruction until the user actually presses a key or the hard drive finishes reading.

Program state: SLEEPING (Waiting for I/O completion). CPU core runs other processes in the meantime.

Common Beginner Misconceptions

  • "A print statement is fast because it's only one line of code."
    Reality: Printing to the terminal is millions of times slower than register arithmetic! Calling printf() formats text in libc, triggers a sys_write system call, forces a CPU user- mode switch, signals the terminal emulator, invokes a font glyph rasterizer, and composites pixels to the GPU framebuffer.
  • "A device driver is a hardware chip."
    Reality: A driver is software! It is a specialized privileged kernel module written by hardware vendors that teaches the operating system how to talk the specific electrical protocol of a USB controller, SSD, or GPU.
  • "The CPU constantly checks every device in a loop."
    Reality: That would be wasteful polling! Modern hardware uses Hardware Interrupts (IRQs). Devices signal the CPU only when an event occurs, leaving the CPU free for useful computations.
Conceptual Why — Explain the Mechanism

Why are user-mode applications strictly forbidden from sending electrical voltages directly to physical hardware controller pins?

Lesson 08 Key Takeaways

  • I/O connects software to the outside world (keyboards, screens, storage, network).
  • User Mode programs must request hardware access through OS System Calls.
  • Device drivers translate generic operating system requests into specific hardware signals.
Notessaved locally+

Notes stay in this browser. They are not uploaded.

Suggest an edit

Learning graph

Discussion

0 comments

Loading discussion…

Sign in to ask a question.