CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 4 of 6 · 3 min read

Compare

In plain words: Two programs show the main difference. Each makes a second line of execution that sets a global variable counter. The child process sets it to 7 and the thread sets it to 42:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>

int counter = 0;                       /* a global: one copy per process */

static void *thread_body(void *arg) { (void)arg; counter = 42; return NULL; }

int main(void) {
    pid_t pid = fork();
    if (pid == 0) { counter = 7; _exit(0); }
    waitpid(pid, NULL, 0);
    printf("after the child process changed it: counter = %d\n", counter);

    pthread_t t;
    pthread_create(&t, NULL, thread_body, NULL);
    pthread_join(t, NULL);
    printf("after the thread changed it:        counter = %d\n", counter);
    return 0;
}
after the child process changed it: counter = 0
after the thread changed it:        counter = 42

The child had a copy of the address space (Level 1, page 3), so its change of counter stayed in its own copy. The thread works in the same address space, so the main thread sees 42. This is the central trade-off:

  • Threads share memory easily and cheaply. Sharing data between processes needs pipes or shared memory (Level 1, page 7).
  • Processes are isolated. A bug in one cannot overwrite the variables of another.

Threads also cost less to start. A timing program makes 5,000 processes with fork and wait, and 5,000 threads with pthread_create and pthread_join, and divides by 5,000. Three runs on the test machine (two processors):

5000 fork+wait:             102 us each
5000 pthread_create+join:   35 us each      ratio: 2.9
5000 fork+wait:             110 us each
5000 pthread_create+join:   33 us each      ratio: 3.3
5000 fork+wait:              99 us each
5000 pthread_create+join:   32 us each      ratio: 3.1

A thread was about three times faster to create and collect than a process here. The reason is that fork must copy the process's memory map and descriptors, while a new thread only needs a new stack and a new set of registers. The real gap varies by machine and grows when the process is large. A thread is also cheaper to switch to than another process, since the address space stays the same (Level 1, page 5).

The price is risk. If a thread crashes, the whole process ends. A test with a thread that writes through a null pointer printed nothing from main and the shell reported Segmentation fault, exit status 139. In the process version, only the child would have died and the parent would have continued.

Common mistake

Treating threads as free. Every thread that shares data with others can cause wrong results when two of them change the same variable at the same time. That problem, a race condition (Level 4), is the main cost of threads.

Check yourself: 1. Why does the main thread see 42 but not 7? 2. Why is creating a thread faster than creating a process? 3. What happens to the other threads if one thread dereferences a null pointer?