Part 2 of 6 · 2 min read
Threads
Think of it this way. A restaurant kitchen with one cook can prepare one dish at a time. Hiring more cooks lets several dishes be made at once, and all cooks share the same stove, the same fridge and the same recipe book. Each cook holds a separate knife and a separate place in the recipe. The analogy stops in one place: real cooks can talk to each other, but threads can only coordinate through the memory they share, and bad coordination leads to wrong results (Level 4).
In plain words: In Level 1, a process had one line of execution: one program counter and one stack. A program with several lines of execution is multithreaded. Each line of execution is a thread. All threads of a process live in the same address space (Level 1, page 1). They share the code, the global variables, the heap and the open files. Each thread has its own program counter, its own registers and its own stack, because each one is at a different place in the code and has its own function calls.
A single-threaded process is just a process with one thread. Every process you met in Level 1 had exactly one.
Thread
A thread is a unit of execution inside a process. It has its own program counter, register values and stack, and it shares the process's code, global data, heap and open files with the other threads of that process. The kernel can schedule each thread separately.
Example: a web server can run one thread per client; all of them read the same configuration in memory, but each has its own stack of function calls for its own client.
Check yourself: 1. How many threads does a process from Level 1 have? 2. Which three things does every thread have of its own? 3. Which memory areas do the threads of a process share?