Skip to content
systemcoding.dev
Concurrency

Processes vs Threads: What Is the Difference

A process owns an address space and a thread is a schedulable flow inside one. See what each shares and isolates on Linux, with fork and pthread_create code.

7 min read
Summarize in AI
Flat illustration of parallel threads meeting at a mutex, with the Linux, Erlang and C logos.

Every program you run starts as one process with one flow of control. The moment that program needs to do two things at once, a compute task and a network wait for example, it has two choices that look similar on the surface and behave very differently underneath: ask the kernel for a second process, or ask for a second thread inside the first. Both give you concurrent execution. They differ in what the new flow shares with the old one, and that single question decides how fast you can create it, how it can fail, and how it can corrupt your data.

This post uses Linux with glibc 2.41 and GCC 15. The code is C, but the ideas apply to every language runtime on top of the same kernel.

What a process owns

A process is the kernel's unit of resource ownership. When you start ./server, the kernel builds a private virtual address space for it, a table of open file descriptors, a set of signal handlers, a current working directory, user and group credentials, and a process ID. Nothing in that bundle is visible to any other process unless one of them deliberately shares it through a pipe, a socket, a shared memory mapping or a file.

That isolation is the point. If process A writes through a bad pointer, the write lands in A's own pages and A crashes. Process B keeps running with its memory intact. The memory management unit enforces the boundary in hardware, and the kernel only has to program the page tables.

The cost is that isolation has to be built. Creating a process with fork() copies the parent's page tables (the pages themselves are shared copy-on-write until one side writes), duplicates the descriptor table and sets up a new task. Talking to the new process needs an explicit channel.

What a thread owns

A thread is a flow of control: a program counter, a register set, a stack and a scheduling state. It is the unit the scheduler runs. Every process has at least one, the main thread, and a process can have many.

Threads inside one process share nearly everything else: the same address space, the same heap, the same global and static variables, the same open files, the same signal dispositions, the same working directory and the same process ID. What each thread keeps for itself is small: its stack, its register state, its signal mask, its errno, its thread-local storage and its thread ID.

Sharing is what makes threads fast to create and cheap to communicate between, since one thread can hand another a pointer and the other can dereference it directly. It is also what makes them dangerous, because two threads can read and write the same variable at the same instant with no kernel referee. That hazard has its own name, a data race, and later posts in this cluster spend a lot of time on it.

The same program, both ways

The clearest way to see the difference is to change one global variable from a child process and from a thread, then look at it from the parent.

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

static int counter = 0;

static void *bump(void *arg)
{
    (void)arg;
    counter += 10;
    return NULL;
}

int main(void)
{
    pid_t pid = fork();
    if (pid == 0) {
        counter += 1;
        printf("child process sees counter = %d\n", counter);
        _exit(0);
    }
    waitpid(pid, NULL, 0);
    printf("parent after child exited: counter = %d\n", counter);

    pthread_t tid;
    pthread_create(&tid, NULL, bump, NULL);
    pthread_join(tid, NULL);
    printf("main after thread joined: counter = %d\n", counter);
    return 0;
}

Compile it with gcc -pthread -o procs_vs_threads procs_vs_threads.c. The program prints:

Text
child process sees counter = 1
parent after child exited: counter = 0
main after thread joined: counter = 10

The child process incremented its own copy of counter, so the parent still saw 0. The thread incremented the one and only copy, so the main thread saw 10. Same variable name, same source file, opposite visibility. The waitpid and pthread_join calls also make the output order fixed: each flow finishes before the parent prints.

How Linux models both with one structure

Linux does not keep separate kernel objects for processes and threads. Both are tasks, represented by a task_struct. What differs is which resources two tasks share, and that is chosen when the task is created with the clone family of system calls. A task created with CLONE_VM shares the address space of its creator, CLONE_FILES shares the descriptor table, CLONE_SIGHAND shares signal handlers, and CLONE_THREAD puts the new task in the same thread group, which is what makes getpid() return the same value in all of them.

fork() is clone with none of those flags, so everything is copied. pthread_create() in glibc's NPTL is clone with all of them, so everything is shared. A task in between, one that shares the descriptor table but not memory, is legal and occasionally useful. The man page for clone(2) lists every flag.

You can see the thread group at work with gettid(), which glibc has exposed since version 2.30:

ids.cc
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

static void *show(void *arg)
{
    (void)arg;
    printf("thread: pid=%d tid=%d\n", getpid(), gettid());
    return NULL;
}

int main(void)
{
    printf("main:   pid=%d tid=%d\n", getpid(), gettid());
    pthread_t t;
    pthread_create(&t, NULL, show, NULL);
    pthread_join(t, NULL);
    return 0;
}

The numbers differ on every run, so here is one possible output:

Text
main:   pid=48211 tid=48211
thread: pid=48211 tid=48212

The process ID is shared and the thread ID is unique. The main thread's TID equals the PID, which is why a single-threaded program never notices the distinction.

Comparing them side by side

PropertyProcessThread
Address spacePrivateShared with the process
Heap and globalsPrivate (copy-on-write after fork)Shared
File descriptorsPrivate table, inherited by copyShared table
StackOne per process initiallyOne per thread
Crash blast radiusThat process onlyThe whole process
Creation costHigher: page tables and tables copiedLower: little to copy
CommunicationPipes, sockets, shared memory, filesPlain memory, with synchronization
Debugging a raceRare, communication is explicitCommon, sharing is implicit

The creation cost row deserves a caution. We are not quoting a number because the real figure depends on the kernel, the size of the address space and the hardware, and it should be measured on your own machine with a tool such as perf. The mechanism is what matters: a thread skips the copying a process needs.

Choosing between them

Pick threads when the flows cooperate on shared data, when you need low-latency handoff, or when creating many short-lived workers is part of the design. A thread pool serving requests from one in-memory cache is the textbook case.

Pick processes when isolation is worth more than speed. A web server that runs each untrusted request handler in its own process survives a handler crash. A build system that runs compilers as separate processes cannot have one compiler overwrite another's memory. Browsers, database servers such as PostgreSQL, and many network daemons put the boundary where a failure must not spread.

Mixing is normal. A process per tenant, with a thread pool inside each, is a common shape. One rule from POSIX is worth remembering when you mix them: calling fork() in a multithreaded program copies only the calling thread into the child, and the child may only call async-signal-safe functions until it calls exec. The man page for fork(2) and POSIX both state this, and ignoring it produces deadlocks that appear once in a thousand runs.

When this distinction is blurry

Do not treat the table as a law. Some systems run threads without a kernel at all. Green threads and goroutines are scheduled by a language runtime on top of a few OS threads, so "a goroutine" is neither a process nor a thread in the sense used here. Containers and namespaces change what a process can see without changing its memory isolation. On a small RTOS, "tasks" share one address space and behave like threads, and there may be no MMU to give you processes at all.

Windows adds another variation: creating a process is much heavier than on Linux, which is one reason Windows programs lean on threads more than Unix programs do. The concept transfers, the cost model does not.

Share

Keep reading

Flat illustration of user space above the system call boundary, the kernel and the hardware, with the C, FreeBSD and Linux logos.
Systems Programming

How argc and argv Reach main in a C Program

The kernel copies argc and argv onto the new process's stack on execve, then startup code hands them to main. See the layout and measure it with a program.

6 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.