Full Byte Developer.

Threads vs processes

Processes get separate address spaces; threads share memory inside one process and need care with races.

Published here , updated · 5 sources

A process is an isolated program image with its own private address space. Threads are lighter concurrent paths of control that usually share that same space inside one process [1] [2]. Pair those ideas with Python multiprocessing vs threading plus concurrency vs parallelism.

Quick start: see two PIDs vs one address space idea

After fork, parent and child are separate processes [1]. A minimal C program is the textbook demo; from the shell you can still watch the pattern Bash uses daily:

bash
/bin/sh -c 'echo parent=$$; ( echo child=$$ )'

On a draft Linux host:

text
parent=12345
child=12346

Two different PID numbers mean two process entries in the kernel, even though the snippet was short [1]. For threads inside one process, ps -L -o pid,tid,cmd -p $$ on a multithreaded program shows one PID and multiple thread IDs; run that against a real pthread program when you teach the distinction.

Draw fork vs shared VM

fork() creates a child that starts as a duplicate of the parent [1]. Parent and child have separate memory spaces; at the moment of fork the contents match, but later writes and mappings in one do not affect the other [1]. The child receives a new process ID [1].

clone() creates a child with selectable sharing of address space, file table, signal handlers, and namespaces via flags [2]. Thread libraries typically share the virtual address space (CLONE_VM in common setups) so threads see the same heap while keeping separate stacks [2].

Compare failure and communication

ChoiceMemoryTypical failure boundaryTalk to siblings
Process (fork + exec)Separate VM [1]One crash may not kill siblingPipes, sockets, shared mem APIs
Thread (shared VM)Shared heap/globals [2]One bad write corrupts allMutex, atomics, channels in lang

Processes pay inter-process communication cost but gain isolation. Threads avoid marshaling large buffers but require synchronization for mutable shared state [2].

Copy-on-write in plain words

Linux does not eagerly duplicate every page at fork [1]. Parent and child share read-only physical pages until one writes, then the kernel splits the mapping. That detail matters for memory accounting, not for security boundaries: logically they remain separate processes [1].

Map into Python

CPython threads share one process and fight over the GIL; processes spawn interpreters. See python-multiprocessing-vs-threading. Pick threads for shared in-process caches with locks; pick processes when isolation or CPU parallelism in Python matters [1] [2].

Map into shells and servers

Each command you start after Enter is usually a new process created via fork and exec, not a new thread inside Bash [5]. Web servers may use thread pools inside one process or worker processes; the table above is how you read their architecture diagrams.

For PID, credentials, and /proc vocabulary, see what is a process. For the Enter key path, see what happens when you press Enter.

Scheduling note

Linux schedules threads as tasks (clone children) and processes as tasks. From the kernel's view both are runnable entities with PIDs or TGIDs depending on setup [2]. Userland "thread" is mostly shared memory plus synchronization discipline.

exec after fork in shells

fork alone duplicates the parent; execve replaces the child program image with ls or python [1] [2]. Bash follows that pattern for externals while keeping built-ins in-process [5]. Understanding exec explains why type ls shows a file path but type cd does not.

Environment and working directory are preserved across fork and adjustable before exec [2]. That is how pipelines and subshells inherit PWD and exported variables unless a subshell option says otherwise.

Containers and namespaces (outline)

Modern Linux adds namespaces so a process can think it owns its own PID tree or network stack [2]. Containers bundle clone flags with cgroup limits. Threads versus processes still matter inside a container: one OOM in a threaded process kills the cgroup task group you scheduled together.

When to choose which

Choose processes when you need fault isolation, separate privilege levels, or separate memory limits (cgroups). Choose threads when you share a large read-mostly dataset and synchronization is tractable. If neither fits, message-passing between processes (actor style) often beats a giant locked heap.

Traps I still see

Spawning threads expecting crash isolation. One segfault in a thread can take the whole process [2].

Using processes to share a giant mutable in-memory structure without a shared-memory design. You will serialize or copy.

fork is free on a 64 GB parent RSS. Fork still triggers bookkeeping; respect CoW but measure.

Treating "multithreaded server" as automatically faster on CPU-bound Python.

Threads are always lighter than processes on Linux. Thread creation still involves kernel bookkeeping via clone [2].

Confusing green threads or async tasks with OS threads. Different layers.

Signals and shared address space

A signal delivered to a threaded process can interrupt any thread unless masked [2]. Process-wide state such as signal dispositions is shared. That is one reason threaded programs isolate signal handling carefully while multi-process designs delegate signal policy per child [1].

Zombie children appear when a parent never collects exit status [1]. Bash usually waits on foreground jobs; background jobs need wait if scripts depend on exit codes [5].

Kernel vocabulary shortlist

A task is schedulable. A thread group may share one PID in top while exposing multiple TIDs [2]. getpid() in C returns the thread group leader ID in many pthread programs. When documentation says "process," check whether it means OS process or pthread.

File descriptors are per-process; threads inside a process share the table [2]. One thread closing a descriptor races with another thread still reading unless you serialize access.

Measure resident set size separately for threaded and multi-process designs. Two processes each importing pandas pay import cost twice; two threads share one import at the cost of shared mutable risk [1] [2].

Run the PID demo, then read whether your runtime maps threads to clone with shared VM. Save one ps -L line in your notes when you teach fork versus pthreads so the diagram stays tied to real PIDs. Concurrency vs parallelism names overlap versus simultaneous CPU use on the CPU, while Python multiprocessing vs threading shows how CPython maps those ideas onto interpreters and the GIL.

Send this to the friend who uses threads and processes as synonyms.

Two panels comparing forked processes with separate address spaces versus threads sharing one heap with distinct stacks.
fork isolates memory; clone with shared VM is the usual thread shape on Linux.

Keep this guide

By Full Byte Developer

Filed under Dev explainers

Questions about this guide

What does fork create?

Linux fork(2): a new child process with its own memory space. Writes in one process do not change the other's mappings after the split.

How is a thread different on Linux?

clone(2) can create a child that shares virtual memory and other context via flags. Typical threads share the address space and keep their own stack.

Which should I pick in Python?

Shared-memory concurrency with care: threads (and mind the GIL). Isolated CPU work: processes. See the multiprocessing versus threading post.

Do threads avoid IPC cost?

They share memory, so they skip marshaling across address spaces, but they inherit race conditions and need synchronization.

Is a child process a full copy of RAM?

fork(2) shares physical pages until one side writes; copy-on-write splits mappings. Logical isolation still applies.

Sources

Next in this stack