Thread and process
A running program is a process: one address space holding its code, its global data and a stack. It starts with one thread, one line of execution. The kernel keeps a record for it, thread ID 1000.
A thread is the part that moves: a program counter saying which instruction is next, the other CPU registers, and its own stack. While it runs, its registers sit in a core. Code and globals belong to the whole process.
Making a thread
Line 1 asks for a second thread. The C library first sets aside memory for its stack, then makes the clone system call: make a thread that shares my memory, my open files and my signal handlers.
The kernel builds a second record, thread ID 1001. It is not a new process: it points at the same address space, so no memory is copied. Its saved stack pointer points into the new stack, its program counter at f.
Running together
The kernel's scheduler chooses threads, not processes. Thread 1001 is ready and core 1 is free, so the kernel loads its saved registers into core 1. Now both threads run at the same time.
Thread 1000 runs line 2 and stores 10 in part[0]. Thread 1001 runs f and stores 20 in part[1]. Both land in the same array, because both threads use one address space. Local variables stay on each thread's own stack.
Ask each thread who it is. getpid() answers 1000 in both: one process. gettid() answers 1000 and 1001: two threads. On Linux a process is a thread group, and its process ID is the ID of its first thread.
Ending a thread
Line 4 waits for thread 1001 to finish. If it is still running, thread 1000 sleeps: the kernel saves its registers into its record and frees core 0 for any other ready thread.
Thread 1001 returns from f, which ends only that thread. The kernel removes its record and wakes thread 1000, which goes back on a core. part[0] + part[1] is 30. The process and its memory carry on.
A new thread copies no memory, and switching between two threads of one process keeps the same page table. The catch: shared memory means threads can trip over each other, which the synchronisation module tackles.
What happens
;; step 1/10 · one-thread
process 1000 one address space:
code the program text
globals int part[2]
stack for thread 1000
+ heap, open files, signal handlers
thread 1000 one record in the kernel
single-threaded: thread ID = process ID
The IDs 1000 and 1001 are examples. Real ones are whatever free numbers the kernel hands out, and a new thread's ID need not be the next number. The record, clone and the flags are how Linux does it with the GNU C library. OSTEP calls the record a thread control block; other systems name things differently, but every kernel that runs threads keeps saved registers and a stack per thread.