Part 1 of 6 · 1 min read
Why this page comes first
In this module you will
- Define a thread and say what it has of its own and what it shares with the other threads of its process.
- Explain why a thread is cheaper to create than a process, with a measurement.
- Show with a C program that threads see the same global variable while forked processes do not.
- Say the four benefits of threads and the main risk.
- Tell concurrency from parallelism.
Level 1 built everything on the process: one program, one address space, one line of execution. That is enough for a shell, but not for a web server that must answer thousands of clients, or a program that must stay responsive while it computes. This page introduces the thread, a second kind of running unit that lives inside a process. Page 2 shows how to create threads in C. The later pages cover how the kernel runs them, how to write safe thread code, and how servers use them. The main sources are "Operating System Concepts" (OSC), chapter 4, "The Linux Programming Interface" (TLPI), chapter 29, and "Operating Systems: Three Easy Pieces" (OSTEP), chapter 26. All programs were compiled with gcc -Wall -Wextra -pthread and run on Linux.
About this example About this example
Real: The C programs, commands and outputs on this page were compiled with gcc -Wall -Wextra -pthread and run on a Linux test machine (2 processors, kernel 6.18, glibc 2.39).
Scripted: The worked traces use small numbers chosen so they can be followed by hand.
Not real: The page simplifies real systems. Actual kernels add more cases, limits and tuning than a short page can show.