Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Threads, Context Switching & IPC
OS

Threads, Context Switching & IPC

Understand the lightweight version of processes (Threads) and how processes communicate (IPC).

A thread is the smallest unit of CPU utilization. A process provides the environment (address space, file descriptors, etc.), and threads execute within that environment sharing the same resources.

Process vs Thread

AspectProcessThread
Address spaceSeparateShared within process
Creation timeSlowFast (10-100x faster)
Context switchExpensive (TLB flush, address space switch)Cheap (registers + stack only)
IPCRequires kernel mediation (pipes, shared memory)Reads/writes same memory directly
ProtectionFully isolated from other processesNo isolation between threads (can corrupt each other)

User-Level vs Kernel-Level Threads

User-LevelKernel-Level
Managed byThread library at user levelOS kernel
Context switchNo system call — very fastSystem call — slower
BlockingOne blocking thread blocks all threads in processOne thread can block independently
ParallelismNo — only one thread runs at a timeYes — multiple threads across CPU cores
ExampleGreen threads (old Java)POSIX threads (pthreads on Linux)

Modern Model: N:M (Hybrid)

Many user-level threads mapped to fewer kernel-level threads. Best of both worlds — fast creation + true parallelism.

Inter-Process Communication (IPC)

MechanismSpeedDirectionUse case
Shared memoryFastest (no copy)BidirectionalProducer-consumer, large data
Message passingModerate (kernel copy)AnyDistributed systems, small messages
PipesModerateUnidirectionalShell pipelines (ls | grep)
SocketsSlow (network stack)AnyNetwork communication
SignalsFastUnidirectionalNotifications, interrupts

Shared Memory

Two or more processes map the same physical memory region into their address spaces. No kernel involvement after setup — just reads and writes. Must use synchronization (mutex, semaphore) to prevent race conditions.

Message Passing (Queues)

Processes send and receive messages through a queue managed by the kernel. The sender writes a message, the receiver reads it. No shared memory needed — works across network nodes.

Pipes

A unidirectional byte stream connecting stdout of one process to stdin of another. pipe() creates two file descriptors: one for reading, one for writing. Anonymous pipes work between related processes; named pipes (FIFOs) work between any processes.

Q: Why are threads called “lightweight processes”?

A: Threads share the same address space (code, data, heap) as their parent. Creating a thread doesn’t require allocating a new virtual address space or setting up new page tables. Context switching between threads is much faster than between processes.

Q: What is the difference between user-level and kernel-level threads?

A: User-level threads are managed by a library without kernel awareness — fast but if one blocks, all block. Kernel-level threads are managed by the OS — slower but independent blocking. Modern systems (Linux, Windows) use kernel-level threads.

Q: What is the fastest IPC mechanism?

A: Shared memory — data is not copied between processes. Once mapped, both processes read/write the same physical RAM directly. No kernel involvement during data transfer. The trade-off: you need synchronization (mutex, semaphore) to prevent race conditions.

Q: What is a pipe in Linux?

A: A unidirectional communication channel. pipe() returns two file descriptors — write to one end, read from the other. Used in shell pipelines: ls | grep .txt sends ls output directly to grep input.

Q: What are the multithreading models?

A: 1:1 (each user thread = one kernel thread — Linux, Windows), N:1 (many user threads to one kernel thread — obsolete), N:M (many user threads to many kernel threads — most flexible).

My Private Notes

Notes are auto-saved locally to this device.