Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Foundations

How does your computer run many programs at once?

It does not run them at once. It swaps them super fast. Processes, the scheduler, context switches, virtual memory and system calls, explained simply.

Derpx0618 Oct · 5 min read
Contents
What is a process, exactly?How can 8 cores handle hundreds of programs?How does the OS take the CPU back?Who goes next? (the scheduler)What does the switch itself look like?Why can't a game read my banking tab?How does a program ask the OS for things?Can I see this on my own machine?What should I learn next?Sources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
What is a process, exactly?How can 8 cores handle hundreds of programs?How does the OS take the CPU back?Who goes next? (the scheduler)What does the switch itself look like?Why can't a game read my banking tab?How does a program ask the OS for things?Can I see this on my own machine?What should I learn next?Sources

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.

What is a process, exactly?

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.

Process

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?

How can 8 cores handle hundreds of programs?

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.

Think of it like this

One teacher helping a hundred students. She helps one student at a time, but if she moves around fast enough, nobody feels ignored. The cost is that each student waits a little longer.

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?

How does the OS take the CPU back?

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
Timer
OS (scheduler)
Process B

Process A runs on the core

Step 1 of 5
The OS gets control back because of the hardware timer, not because A chose to stop.

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.

Who goes next? (the scheduler)

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:

  1. A higher-priority job always runs before a lower one.
  2. Equal priority? They take turns (round-robin, each gets a short time slice).
  3. A new job starts at the top.
  4. Use up your time allowance and you move down one level.

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.

What about real Linux?

Since version 2.6.23 Linux's default scheduler is CFS, the "Completely Fair Scheduler" (per sched(7)). A nice value from -20 (important) to +19 (polite) tweaks who gets favored.

What does the switch itself look like?

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.

1Timer fires
2Save A's registers
3Load B's registers
4B resumes where it stopped
B never knows it was paused.

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.

Why can't a game read my banking tab?

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).

No virtual memoryEveryone shares the real RAM. One bug can overwrite another program.
→
Virtual memoryEveryone gets a private fake map. The OS translates to real spots.

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?

How does a program ask the OS for things?

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.

Your app (user mode)
System call: the only door
Kernel (kernel mode)
Hardware
Apps can't skip the door.

Can I see this on my own machine?

On Linux, these are safe to run:

Bash
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.

What should I learn next?

Read the OSTEP chapters on paging (how virtual addresses get translated), then try fork() and exec(), the calls that create processes.

Key takeaways
  • Your computer doesn't run 100 programs at once. It swaps them on and off each core very fast.
  • A timer interrupt lets the OS take the CPU back, the scheduler picks who is next, and a context switch saves and restores their state.
  • Virtual memory gives every process its own private map, so programs can't read each other.
  • Programs ask the OS for anything risky through system calls.

Sources

  • Arpaci-Dusseau — The Abstraction: The Process, OSTEP
  • Arpaci-Dusseau — Mechanism: Limited Direct Execution, OSTEP
  • Arpaci-Dusseau — The Multi-Level Feedback Queue, OSTEP
  • Arpaci-Dusseau — The Abstraction: Address Spaces, OSTEP
  • Linux man-pages — sched(7), syscalls(2), proc_pid_maps(5), strace(1)
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 3 minWhy is 0.1 + 0.2 not 0.3 in code?Foundations · 4 minHow does Git work inside? (Snapshots, not changes)Foundations · 5 minHow do maps apps find the fastest route?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)