Topic 3.4 · Process & Thread Management

Threads, Multithreading & Thread Synchronization

3 min read1 question to test yourself

1Definition

A Thread is the smallest independent unit of CPU execution within a process, characterized by its own Thread ID, Program Counter, register set, and stack, while sharing code, static data, and operating system resources (open files/sockets) with sibling threads of the same parent process. Multithreading is the concurrent execution of multiple threads within a single process address space.

2Simple Explanation

A traditional process is like a worker running a single-threaded job. If the process wants to perform two tasks at once (like typing in a word processor while performing background spell-checking), creating a whole new process is heavy and expensive because the OS has to allocate a whole new memory space.

Instead, the process spawns Threads. Threads are lightweight sub-processes. All threads of a process share the same memory (heap and global variables), making communication blazingly fast. However, because they share memory, if one thread mutates a shared variable without coordination, data corruption occurs.

3Real-Life Example / Analogy

Workers in a Bakery: A process is the entire bakery building. The threads are individual bakers working inside the same kitchen. All bakers share the central mixing bowls, ovens, and flour sacks (Shared Heap & Data Segment). Each baker has their own personal notepad in their pocket to track their specific recipe steps (Private Stack & Program Counter).

4Diagram / Visual Representation

MULTI-THREADED PROCESS

Shared: Code · Data · Files

Thread 1
Registers · Stack

Thread 2
Registers · Stack

Thread 3
Registers · Stack

SINGLE-THREADED PROCESS

Code · Data · Files

Thread 1
Registers · Stack

5Working / How It Works

  1. Thread Creation: Process invokes thread creation function (pthread_create).
  2. Resource Allocation: OS allocates private stack space and Thread Control Block (TCB). Code and Heap segments are shared.
  3. Concurrent Execution: OS thread scheduler assigns CPU time slices across active threads.
  4. Fast Context Switch: CPU switches between threads of the same process without reloading memory page tables or flushing cache lines.

6Types / Components / Characteristics

  • Process vs. Thread Context Switching Comparison:
Feature / Metric Process Context Switching Thread Context Switching
Memory Address Space Switches entire virtual memory space Preserves shared address space
CPU Cache State Flushed / Invalidated Preserved across sibling threads
Execution Speed Slow, high overhead Extremely Fast, lightweight
Saved Data Complete PCB, Page Tables, Registers Registers, Program Counter, Stack Pointer
Isolation & Protection Complete memory isolation Zero memory isolation between threads
  • Core Benefits of Multithreading:
    1. High Responsiveness: User interface thread remains interactive while background thread processes file I/O.
    2. Resource Sharing: Threads share memory and file descriptors by default.
    3. Economy: Creating and switching threads is up to 10-100x faster than process creation.
    4. Multiprocessor Utilization: Scales across multi-core CPU architectures.

7Solved Examples / Problems

Problem: Will a single-core CPU system experience performance speedups by converting a purely CPU-bound algorithm into a multithreaded application? Solution: No. On a single-core CPU, running multiple CPU-bound threads requires the OS to context-switch back and forth on the single processor. The context-switching overhead actually makes the multithreaded program run slower than the single-threaded equivalent. Multithreading on a single CPU core only yields speedups for I/O-bound applications where one thread waits for I/O while another uses the CPU.

8Advantages, Disadvantages & Applications

  • Multithreading Advantages: Highly economical, rapid inter-thread communication, responsive UI.
  • Disadvantages: Lack of memory protection means a crash in one thread can terminate the whole process; requires strict thread synchronization mechanisms.
  • Applications: Web browsers (separate tabs/renderers), text editors (auto-save, spell-check, typing), web servers (handling concurrent HTTP client requests).

9Important Points to Remember

  • Threads of the same process share Code, Data, and OS Files, but maintain private Registers, Program Counters, and Stacks.
  • Thread context switching preserves CPU cache contents.

10Test Your Understanding

  1. MCQ: What components are privately owned by an individual thread within a multi-threaded process?
    • A) Heap segment and global variables
    • B) Open file descriptors and static data
    • C) Stack pointer, CPU registers, and Program Counter
    • D) Text segment instructions
    Show answer
    C.
  2. Conceptual Question: Detail four major performance and architectural advantages of multithreading over multitasking.
  3. Application Question: Explain why a race condition can occur between two threads incrementing a shared global variable counter++.

Finished this topic?

0 of 27 topics read in Operating Systems

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic