KERNELTID 1000 · PID 1000runningTID 1001 · PID 1000ready1pthread_create(&t, 0, f, 0);2part[0] = 10;3part[1] = 20; /* in f */4pthread_join(t, 0);part[0]0part[1]0STACK 1000STACK 100110011020100010003010001001
Back: the kernel and one record per threadFront: the process, one address space shared by its threadsPucks: each thread's registers, on a core while it runsAt the back, the kernel and its records, one per thread. In the middle, two CPU cores. In front, the process: one address space with the code, the global array part and one stack per thread. Each thread is a puck holding its registers: on a core while it runs, on its record while it waits. Line 1's clone request adds a record but no new address space; both threads write into the same array; when thread 1001 ends only its record and stack go. Each thread also has a kernel stack used while it is inside a system call; the figure shows only the user stacks. After a thread ends, the C library may keep its stack to reuse for the next thread.

Thread and process

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

Making a thread

Running together

Ending a thread

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.