Skip to content

Processes vs Threads

First PublishedByAtif Alam

A process is an instance of a running program with its own address space (memory), PID, and kernel state. A thread is a unit of execution that shares the address space of a process; threads in the same process share memory (global variables, heap) but have their own stack and register state. Linux implements threads as lightweight processes via clone() with shared address space (CLONE_VM); each has a TID (thread ID), and the main thread’s TID equals the process PID.

AspectProcessThread
Address spaceSeparate (own virtual memory)Shared within process
Creationfork() (or clone()); then often exec()pthread_create() (uses clone() with shared VM)
IDPID (process ID)TID (thread ID); main thread TID = PID
IsolationMemory and failures isolatedOne thread can crash the whole process
OverheadHigher (new address space, kernel structures)Lower (shared address space)
CommunicationIPC (pipes, sockets, shared memory, etc.)Shared memory (pointers, globals)
Failure impactOne process crash does not kill othersOne thread (e.g. segfault) can kill the process
ToolPurpose
ps -T, ps -eLfList threads (TID, PID).
top -H, htopThread view; toggle with H.
strace -fTrace fork/clone and all threads.
pstack, gdbThread backtraces (info threads, thread apply all bt).
perf, valgrindProfiling; helgrind for data races.
/proc/<pid>/task/One directory per thread (TID).
  • View threadsps -T -p <pid> or ps -eLf; top -H or enable thread view in htop. Each line is a thread (TID in second column for ps -T).
  • Per-thread infols /proc/<pid>/task/ lists TIDs; each has its own status, stack, etc.
  • Backtraces — In gdb: info threads, thread <n>, bt. thread apply all bt for all threads.
  • Trace creationstrace -f -e clone,fork ./program shows clone/fork calls when threads/processes are created.
ProcessThread
Address spaceOwnShared
Creationfork/exec or clonepthread_create (clone with CLONE_VM)
IDPIDTID
IsolationYesNo (shared memory)
OverheadHigherLower
CommunicationIPCShared memory
Failure impactIsolatedCan kill process

Multiple processes — Each has its own virtual address space; the kernel maps each to (possibly different) physical pages. No shared memory unless explicitly set up (shared memory, mmap).

Process A (PID 1000) Process B (PID 1001)
+------------------+ +------------------+
| code | | code |
| data | | data |
| heap ^ | | heap ^ |
| ... | | | ... | |
| stack v | | stack v |
+------------------+ +------------------+
(separate pages) (separate pages)

Single process, multiple threads — Same virtual address space; each thread has its own stack (and register state), but code, data, and heap are shared.

Process (PID 1000)
+------------------+
| code (shared) |
| data (shared) |
| heap (shared) ^ |
| ... |
| stack T1 v |
| stack T2 v |
| stack T3 v |
+------------------+
  • Crashes — A segfault in one thread kills the whole process; use gdb and thread apply all bt to see which thread and where.
  • Races — Shared memory without synchronization causes data races; use thread sanitizers or helgrind.
  • Resource usagetop -H shows per-thread CPU; a single runaway thread can max one CPU.