Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    Threads: Several Workers Inside One Process

    A thread is one independent sequence of instructions inside a process — a "worker". Every process

    Chapter 7 of 124 min read

    The Plain Picture#

    A family rents one hotel room (a process). Inside, three family members each do their own thing — one reads, one packs, one makes calls. They share the room's wardrobe, fridge and suitcase (shared memory), but each has their own to-do notepad (their own stack and "where am I" bookmark).

    Renting a second room for each person would give more privacy, but costs much more and makes sharing the suitcase awkward.

    The Real Names#

    • A thread is one independent sequence of instructions inside a process — a "worker". Every process starts with one thread (the main thread) and can create more.

    • Threads in the same process share the heap, globals, code, and open files.

    • Each thread has its own stack, registers and program counter.

    • The scheduler (Ch 4) schedules threads, not just processes, so threads of one process can truly run in parallel on different cores.

    Under the Hood#

                     ONE PROCESS (e.g. StudyBuddy's server)
      ┌─────────────────────────────────────────────────────────────┐
      │   SHARED:  code  ·  globals  ·  HEAP  ·  open files/sockets │
      │                                                             │
      │   ┌────────────┐   ┌────────────┐   ┌────────────┐          │
      │   │ Thread 1   │   │ Thread 2   │   │ Thread 3   │          │
      │   │ own stack  │   │ own stack  │   │ own stack  │          │
      │   │ own regs   │   │ own regs   │   │ own regs   │          │
      │   └────────────┘   └────────────┘   └────────────┘          │
      └─────────────────────────────────────────────────────────────┘

    Process vs thread — the classic interview question#

    ProcessThread
    Memoryown, privateshared with other threads in the same process
    Creating oneheavier (new memory map, kernel records)lighter
    Switching betweenslower (page tables change, TLB flushed)faster (same memory map)
    Communicationneeds files, pipes, sockets, shared memoryjust read/write the same variables
    If one crashesothers keep runningusually the whole process dies
    Safetyisolated by the OSyou must prevent them corrupting shared data (Ch 8)

    When to use which: processes when you want isolation (one crash shouldn't kill the others — e.g. a web server running 4 worker processes). Threads when tasks need to share a lot of data cheaply.

    In Java#

    Java
    Thread t = new Thread(() -> System.out.println("hello from a thread"));
    
    t.start(); // ask the OS to run it concurrently
    
    t.join(); // wait until it finishes

    (Python has threads too, but with an important twist called the GIL — covered in Python Concurrency (coming soon).)

    Gotchas

    • Threads share everything on the heap — including bugs. Two threads writing the same variable without coordination produce wrong results. That's the next chapter.

    • One thread crashing (for example, with a segmentation fault) usually takes down the whole process.

    Check yourself

    3 questions
    1

    What do threads in the same process share, and what is private to each?

    Show answer

    Shared: heap, globals, code, open files. Private: stack, registers, program counter.

    2

    Give three differences between a process and a thread.

    Show answer

    Any three: processes have private memory vs threads share; threads are cheaper to create and switch; threads communicate through shared variables vs processes need IPC; one thread crash kills the process vs process crashes are isolated.

    3

    Why might a web server use several processes instead of several threads?

    Show answer

    Isolation — one worker crashing or leaking memory doesn't take down the others.