The layers
At the bottom is the hardware: the CPU (processor) that carries out instructions, memory (RAM) that holds running programs, a disk that keeps files, and devices such as a speaker. Many programs want these parts at the same time.
The operating system (OS), such as Windows, macOS, Linux, Android or iOS, is the program that manages the hardware for every other program. Its core is the kernel, which sits between the apps and the hardware.
Running programs
When you open an app, the OS copies the program from the disk into memory and starts it. A running program is called a process. Here three run at once: a browser, a music player and a notes app.
A CPU core can run only one process at a time. The scheduler, a part of the kernel, picks which process goes next and lets it run for a short turn, a time slice of a few thousandths of a second.
When the slice ends, a timer wakes the kernel. It saves exactly where the process stopped and loads the next one: a context switch. This happens many times a second, so all three apps seem to run at once.
The OS gives every process its own part of memory. The browser can use its own part, but it cannot read or change the music player's. If it tries, the OS stops it, so one app cannot break another.
Asking the OS
Apps are not allowed to touch the hardware directly. Instead they ask the kernel with a system call: a request for a service. In Python, open() makes one: the notes app asks the OS to open list.txt for writing.
f.write() keeps "milk" in a buffer, a waiting area in memory. f.close() hands it to the kernel in a write system call. The file system decides where on the disk the bytes go, and the disk's driver stores them.
Every kind of device is controlled in its own way. A driver is code in the OS that knows how to talk to one kind of device. The music app asks to play sound, and the sound driver sends it to the speaker.
The kernel runs in kernel mode, with full control of the hardware. Apps run in user mode, with limits, and must ask for everything. So if one app crashes, the OS cleans up and the other apps keep running.
What happens
;; step 1/10 · hardware hardware the parts you can touch CPU carries out instructions, one after another memory RAM: holds the programs that are running disk keeps files, even when the power is off speaker one of many devices (screen, keyboard, Wi-Fi ...) every app wants to use them, all at once
Most CPUs today have several cores, and each core runs one process at a time, so a few processes really do run at the same moment. The OS still switches every core between many processes. How long a time slice lasts depends on the OS and on how busy the computer is. The OSTEP textbook uses a timer that interrupts every 10 milliseconds as its example, which would allow about 100 switches a second.