The CPU
Primary memory (RAM)
Secondary memory (SSDs and HDDs)
Other hardware (keyboards, mice, displays, networking, printers, scanners)
When multiple processes are running concurrently, the operating system needs to control which processes have access to which resources when.
For instance, if two processes need to read from a hard drive, the OS needs to coordinate which read is done first.
Also, some resources also need to be managed in the sense of controlling what users or processes have access to them.
For instance, on a multi-user computer, you may not want every user to be able to access every file on the computer’s hard drive.
We’ve already discussed how the operating system and the CPU work together to give processes the illusion of having full access to the CPU and RAM via scheduling and virtual memory.
The virtual memory system also control access to memory so different processes don’t access each other’s memory.
Perhaps the next most important resource are all the files stored on a computer’s secondary storage.
The operating system mediates access to secondary storage (SSDs and HDDs) through several layers of abstraction that provide the user-level view of files and directories via the standard library and kernel system calls.
In addition to providing access to the raw data, the file system also allows control over who can access what files.
In Unix-based systems, files are owned by particular users and groups of users and access is controlled via permissions who can read, write, and execute the file in terms of the file’s individual owner, the file’s group owner, or the whole world.
When the user types on the keyboard or moves the mouse, the hardware controller for those devices signals an interrupt that the CPU responds to by running the interrupt handler which is part of the operating system kernel.
The operating system is responsible for handling those interrupts and turning them into events that are passed through a program running as part of the OS called the window manager .
The window manager knows about all the windows and where they are and what programs are associated with them and can determine what actual program should get the event, e.g. “the mouse was clicked”.
The program then does whatever it wants in response to that event.
Though often the program is made out of GUI abstractions also provided by the OS so instead of “the mouse was clicked” it may be written to respond to an event like “this particular UI button was pressed”.
Going in the other direction, programs draw the contents of their windows into memory buffers that are passed to the operating system that then builds up an image of the whole screen, accounting for where windows are and what parts of what windows are covered by other windows.
The whole-screen image is then displayed on the computer’s monitor.
Without, for now, getting into the details of how networking works, we just need to know that data that comes into the computer from the network shows up whenever it shows up via dedicated hardware called a Network Interface Card , or NIC .
The NIC usually uses an interrupt to let the CPU know there’s data that needs to be dealt with and code in the kernel called the network stack deals with that data.
Two ways of dealing with events that occur while code is executing.
A mechanism by which some piece of hardware can let the CPU know that it needs attention.
The CPU checks for interrupts between every instruction so they are attended to right away and runs a piece of code called an interrupt handler in the kernel that decides what to do with the interrupt.
When code running on the CPU (or other hardware) checks whether some piece of hardware needs attention.
If the polling happens in a loop, it can be even lower latency than interrupts since it doesn’t require overhead of handling an interrupt.
However polling in the CPU means the CPU can’t do anything else. This is sometimes called busy waiting .
Sometimes we use polling when we don’t need instantaneous responsiveness so we use a timer (which generates an interrupt when it goes off) to schedule a poll.
For example, we could use a timer to generate an interrupt every millisecond which then runs code to check if some data is ready.
Network cards interrupt the CPU when data arrives. But since when one bit of data has arrived there will probably be more, modern networking code turns off interrupts and then runs a tight loop to poll the card for new data.
This saves a lot of work responding to a new interrupt for every new piece of data that arrives over a high-speed network.
The USB controller in a modern computer polls all the USB devices connected to it at a rate the device can specify. This is dedicated hardware so it doesn’t involve the CPU.
The controller then signals an interrupt to let the CPU know there’s data, such as movement data from a mouse.
Normal mice are polled at 125Hz (every 8ms) to see if they have moved.
High-performance gaming mice are polled at 1kHz (every millisecond) so they have lower latency—they respond eight times as quickly as regular mice.
So-called hard real-time systems such as airplanes’ fly-by-wire systems have to be extremely predictable.
Thus, interrupts happening at unforeseen times are unacceptable.
Such systems instead run carefully structured code that uses polling to check I/O devices.
There is one interrupt: a timer interrupt fires at some fixed rate (such as every 10ms) to start a “frame” of code that has been analyzed to be sure it will run well within the time budget before the next timer goes off.
That code includes polling I/O devices for new data. The frame time budget puts an upper bound on how long it will be before I/O is dealt with.
| Criteria | Interrupts | CPU polling | Timed polling |
|---|---|---|---|
| Event frequency | Good if infrequent | Good if frequent | Good if at known interval |
| CPU usage | Low | High | Controllable |
| Power use | Lowest: CPU sleeps between events. | High: CPU constantly working | Depends on polling rate |
| Event predictability | Best if unpredicable. | Okay if unpredictable. | Best if predictable. |
| Latency | Low | Lowest: no interrupt overhead. | Higher but bounded. |
| Security | Harder to get right | Easier to get right | Easier to get right |