Articles

Process vs Thread: What's the Difference?

A process is a running program with its own memory; a thread is an execution path inside it that shares that memory. How they differ and when to use each.

The Lycoris Team The Lycoris Team · · 5 min read
A laptop showing code under blue light

A process is a running instance of a program with its own private address space, file handles, and system resources. A thread is a single sequence of execution inside a process; every process has at least one, and additional threads share the process’s memory and resources. The short version: processes are isolated from each other, threads are not. That one difference drives everything else — cost, safety, communication, and how failures spread.

What the operating system gives a process

When you launch a program, the kernel creates a process and hands it:

  • A virtual address space. The process sees its own private range of memory addresses, mapped onto physical RAM by the kernel and the CPU’s memory management unit. One process cannot read another’s memory by accident. See virtual memory explained for how that mapping works.
  • Resources. Open files, network sockets, environment variables, a working directory, and security credentials such as the user it runs as.
  • At least one thread. The main thread starts executing the program’s entry point.

The kernel tracks all of this in a process control block. Switching the CPU from one process to another means swapping the active address-space mapping, which also invalidates cached translations in the TLB. That is part of why process switches cost more than thread switches.

What a thread owns

A thread is much lighter. Each thread has its own:

  • Program counter — where it is in the code.
  • Register set — the CPU state it was using.
  • Stack — local variables and the chain of function calls.

Everything else — the heap, global variables, open files — is shared with every other thread in the same process. Two threads can read and write the same object in memory directly, with no copying and no kernel involvement.

Process vs thread at a glance

ProcessThread
MemoryPrivate address spaceShares the process’s address space
Creation costHigher (new address space, resource tables)Lower (new stack and registers)
Context switchSlower (address-space change)Faster (same address space)
CommunicationPipes, sockets, shared-memory segments, filesOrdinary variables in shared memory
IsolationA crash usually affects only that processA crash typically takes down the whole process
Synchronization needsLow by defaultHigh: shared data needs locks or atomics
Typical useIsolation, security boundaries, separate programsParallel work on shared data within one program

Sharing memory is both the benefit and the danger

Because threads share memory, communication between them is nearly free. A worker thread can fill a buffer that the main thread reads a moment later. But that same sharing creates the classic hazards of concurrent programming. If two threads update the same counter without coordination, one update can overwrite the other — a race condition. Preventing that requires synchronization primitives such as mutexes and semaphores (compared in mutex vs semaphore), atomic operations, or designs that avoid shared mutable state altogether.

Processes sidestep most of this. Since they don’t share memory by default, they must communicate explicitly through inter-process communication (IPC): pipes, sockets, message queues, or a deliberately mapped shared-memory region. IPC is slower and more verbose, but it makes data flow visible, and a bug in one process cannot silently corrupt another’s memory.

Creating processes cheaply

On Unix-like systems, a new process is traditionally created with fork(), which duplicates the calling process. Copying an entire address space would be expensive, so kernels use copy-on-write: parent and child share the same physical pages until one of them writes, at which point only that page is copied. A fork() is often followed by exec(), which replaces the child’s program image with a new executable. That pairing is how shells launch commands.

Threads in practice: kernel and user threads

Most modern operating systems schedule kernel threads directly: each thread you create is visible to the OS scheduler and can run on any CPU core. Many runtimes add a layer on top — user-space threads (also called green threads, fibers, or coroutines) that the language runtime multiplexes onto a smaller pool of kernel threads. These are cheaper still to create and switch, which is why some runtimes can juggle very large numbers of them. The trade-off is that the runtime, not the kernel, decides when they yield.

Hardware adds one more layer of terminology. A CPU with simultaneous multithreading exposes two or more hardware threads per core. Those are execution contexts in the processor, not software threads, but the OS schedules software threads onto them.

  • Web browsers typically run sites in separate processes so a crashing or malicious page can’t touch other tabs, while each process uses multiple threads for rendering, networking, and compositing.
  • Databases vary: some use a process per connection for isolation, others a thread per connection or a thread pool for efficiency.
  • Node.js runs JavaScript on a single main thread driven by the event loop, and offers worker threads for CPU-heavy work and the cluster module for multiple processes.
  • Web servers often combine both: several worker processes, each with a pool of threads.

When to choose which

Reach for multiple processes when you need fault isolation, different privilege levels, separate programs that happen to cooperate, or a way to sidestep a runtime that cannot run code in parallel across threads. Reach for threads when tasks need to share large data structures, communicate frequently, or start and stop often enough that process creation would dominate. Many robust systems use both: processes for boundaries, threads for throughput within each boundary.

The takeaway

A process is a container — its own memory, its own resources, its own failure domain. A thread is a worker inside that container, sharing everything except its stack and registers. Threads are cheaper and communicate faster but demand careful synchronization; processes are heavier but isolated by default. Pick processes for safety boundaries and threads for shared-memory parallelism, and expect real systems to combine the two.

The Lycoris Team The Lycoris Team · · 5 min read

mmap Explained: How Memory-Mapped Files Work

mmap maps a file into a process's address space so it can be read like memory. How memory-mapped files work, when they beat read(), and their pitfalls.

#Computer Science #Performance #Backend
Chisato Chisato · · 6 min read

epoll vs io_uring: Linux Async I/O Models Compared

epoll tells you when a file descriptor is ready; io_uring lets you submit the I/O itself and collect results later. How each works and when to use which.

#Computer Science #Performance #Backend
The Lycoris Team The Lycoris Team · · 5 min read

What Is Little's Law? Capacity Planning Explained

Little's Law relates the number of requests in a system, their arrival rate, and how long each one takes — a simple formula for sizing capacity.

#Computer Science #Performance #Backend