It does not run them at once. It swaps them super fast. Processes, the scheduler, context switches, virtual memory and system calls, explained simply.
Open Task Manager or run ps -e | wc -l. You will see hundreds of processes, and your laptop has maybe 8 cores. So how is everything "running at once"?
Short answer: it isn't. The operating system (the main program that manages your computer, the OS) swaps programs on and off each core so fast that it looks like all of them are alive. The trick has three parts: a timer, a scheduler and a fake map of memory.
The free book Operating Systems: Three Easy Pieces (OSTEP) notes that a computer may be "seemingly" running hundreds of processes at once. Notice the word seemingly.
A program is just instructions sitting on your disk. A process is that program while it is running. Think of a recipe versus the meal being cooked.
A running program, plus everything it is using right now: its memory, its registers (tiny storage slots inside the CPU) and the place it is up to in its instructions.
So we have hundreds of meals and only 8 stoves. How do they all get cooked?
OSTEP calls it time sharing. Run one process for a little while, stop it, run another, and so on. Each one gets a turn on the CPU, so each one feels like it has the CPU to itself.
With 8 cores, 8 things can truly run together. The rest wait in line.
So "at once" is an illusion made of tiny turns. But there is a catch. While Chrome is on the core, the OS is not running. So who stops Chrome?
OSTEP describes two ways. The polite way is waiting for the program to hand control back. Programs do this whenever they make system calls (more on those below). The forceful way is a timer interrupt: a hardware clock that can be set to fire every few milliseconds. When it fires, the running process is halted and the OS code runs.
Click Next and follow one full round.
Process A runs on the core
Notice that A never agreed to stop. With the polite way only, a program stuck in an infinite loop can be fixed only by rebooting, as OSTEP points out.
The part of the OS that picks the next process is the scheduler. Its rules are called a policy. OSTEP splits things neatly: the mechanism is how to switch, and the policy is which process to run.
The scheduler wants two things that pull against each other. Everything should finish soon (turnaround time). And things you are looking at should react soon (response time). It also does not know how long a program will run.
OSTEP's answer is the multi-level feedback queue. Its first rules are simple:
So jobs that hog the CPU sink, and jobs that wait for your keyboard stay on top. Your typing feels instant. The book adds that after some time period all jobs are moved back to the top, so nobody is starved.
When the OS decides to change processes, it does a context switch. OSTEP explains that it is conceptually simple: save a few register values of the process that is leaving, and restore the ones of the process that is coming back.
The program counter (which instruction is next) is saved too, so B carries on exactly where it was.
So the CPU is shared. What about memory? There are 100 programs and one RAM.
Because programs never touch real memory. Each process gets its own private address space: a fake map of memory, seen from the process's own point of view. The addresses it sees are virtual. The OS, with serious hardware help, translates them to physical addresses (real spots in RAM).
OSTEP lists three goals: invisible to programs, fast, and protection. Protection means one process cannot read or change another's memory, or the OS's.
Walled off is safe. But then how does a program open a file at all?
It uses a system call. The man page describes it as "the fundamental interface between an application and the Linux kernel". The kernel is the core of the OS. OSTEP explains that the CPU has user mode (restricted) and kernel mode (full power). A program traps into the kernel, the kernel does the job, then returns to user mode.
On Linux, these are safe to run:
nproc # how many CPUs you have
ps -e | wc -l # how many processes exist right now
cat /proc/self/maps # the memory map of that cat process
The /proc/<pid>/maps file lists a process's memory regions: address range, permissions, and a pathname like [heap] or [stack]. Those are virtual addresses.
If you have strace installed, strace -c ls runs ls and then prints a summary table of the system calls it made. Even a tiny program makes quite a few.
Read the OSTEP chapters on paging (how virtual addresses get translated), then try fork() and exec(), the calls that create processes.