Abstract Processes vs Threads
CS Fundamentals Series

Process vs Thread

Master the most popular technical interview question with interactive visualizations, C++ examples, and real-world analogies.

school The Interview Answer

If asked: "What is the difference between a process and a thread?"

"A Process is an independent instance of a program in execution with its own separate memory address space.

A Thread is a unit of execution within a process. Threads share the same memory space (heap, data) and resources of their parent process but maintain their own independent stack and registers."
memory Key Distinction: Processes are isolated; Threads share memory.
speed Implication: Threads are faster to create and switch, but less stable.

1. The Real-Life Analogy

Think of your CPU as an industrial park. Switch the view to understand the difference.

Analogy Visual
The Factories (Processes)

Process = A Factory

Imagine a Process as a standalone Factory.

  • domainIsolation: Each factory has its own land, electricity, and water. If one factory burns down, the one next door is safe.
  • inventory_2Resources: Moving equipment between factories is slow (requires trucks/shipping). This is like Inter-Process Communication (IPC).
  • engineeringOverhead: Building a new factory takes a long time and lots of money (System Resources).

memory Memory Model

PID: 101
Code
Data/Heap
Stack
PID: 102
Code
Data/Heap
Stack

Processes have completely separate memory blocks.

table_chart Technical Comparison

Feature Process Thread
Definition Program in execution Segment of a process
Memory Isolated (Own Heap/Stack) Shared (Shared Heap, Own Stack)
Creation Cost High (OS must allocate memory map) Low (Just a stack & registers)
Context Switch Slow (Flush cache/TLB) Fast (Keep memory map)
Communication Complex (Pipes, Sockets, IPC) Simple (Shared Variables)
Stability High (Independent) Low (One crash kills all)

terminal Code Lab: Shared Memory Simulation

See how memory sharing actually works in C++.

#include <unistd.h>
#include <iostream>

int global_val = 10;

int main() {
    pid_t pid = fork();

    if (pid == 0) {
        // Child Process
        global_val = 20;
        std::cout << "Child Val: " << global_val << std::endl;
    } else {
        // Parent Process
        sleep(1); // Wait for child
        std::cout << "Parent Val: " << global_val << std::endl;
    }
    return 0;
}

Terminal Output

Click 'Run' to compile and execute...
Key ConceptCopy-on-Write: When fork() is called, the OS duplicates the process memory. The child's modification to global_val happens in its own separate memory copy. The parent's variable remains untouched.