Talking to devices
Devices are slow
A CPU does billions of steps a second. A disk read can take a few milliseconds: time for millions of steps. Disks, networks, and keyboards are all I/O devices, and all far slower than the CPU. So while one process waits on a device, the OS runs another one on the CPU.
Polling
The simplest way to wait is to keep asking. The CPU reads the device's status register again and again until it says "done". This is polling. It is fine if the device answers almost at once, but a slow device wastes the CPU's time.
Interrupts
Instead, a device can send the CPU an interrupt when it is done. Until then the CPU runs other work. When the interrupt comes, the OS runs a small interrupt handler, then carries on. But a very busy device, like a network card getting a million packets a second, can swamp the CPU. Then the OS can poll instead, or group many events into one interrupt.
DMA
Copying a big block byte by byte keeps the CPU busy. With DMA (direct memory access), the CPU tells the device where in memory the data should go. The device copies it by itself and sends one interrupt when it is finished.
Drivers
Every kind of device speaks its own language. A driver is the part of the OS that knows how to talk to one kind of device. The rest of the OS and the apps just say "read" or "write". Drivers run inside the OS and are a large part of its code, so a buggy driver can crash the whole system.
Remember
- Devices are far slower than the CPU; keep the CPU busy meanwhile.
- Polling: keep asking. Simple, but wastes time on slow devices.
- Interrupts: the device rings when done. Too many can swamp the CPU.
- DMA: the device copies data to memory by itself.
- A driver hides one device's details from the rest of the OS.