Multiprocessing systems in operating systems let several processes run at the same time by using multiple CPU cores or logical processors, with the OS handling scheduling, memory allocation, and communication between them.
Background and Evolution
Early personal computers ran a single program at a time because they had only one CPU. As hardware advanced, manufacturers added more cores, and operating systems evolved to exploit this parallelism. The first major step was Symmetric Multiprocessing (SMP), where all cores share the same memory and I/O resources. Modern operating systems like Linux, Windows, and macOS now support SMP natively, and many also provide support for Non‑Uniform Memory Access (NUMA) architectures where memory access times vary between nodes.
The shift from single‑threaded to multi‑threaded and multi‑process designs required new abstractions: processes became the primary unit of isolation, and threads became lightweight execution contexts that share a process’s address space. Multiprocessing systems build on these abstractions to deliver true concurrency.
Core Concepts: Processes, Threads, and CPUs
A process is an instance of a program with its own memory space, file descriptors, and execution context. A thread is a smaller unit that runs within a process and shares that process’s memory. CPUs (or cores) are the hardware units that execute instructions. In a multiprocessing system, the OS scheduler maps multiple processes or threads onto the available CPUs, often interleaving execution to give the illusion of simultaneous operation.
Process Scheduling and Dispatch
The scheduler is the heart of multiprocessing. It decides which process runs on which CPU and when. Common scheduling policies include:
- Round‑Robin (RR) – each runnable process gets a fixed time slice in a cyclic order.
- Priority‑Based – processes are ordered by a priority value; higher priority processes preempt lower ones.
- Completely Fair Scheduler (CFS) – used in Linux; it models time as a virtual CPU and balances usage across processes.
- Real‑Time Schedulers – such as SCHED_FIFO and SCHED_RR in POSIX, guaranteeing deterministic timing for critical tasks.
The scheduler performs context switching when a process is preempted. It saves the CPU state (registers, program counter) into the process control block and loads the state of the next process. Context switches are expensive, so OS designers aim to minimize them while ensuring responsiveness.
Inter‑Process Communication (IPC) Mechanisms
Because processes have separate address spaces, they need IPC to share data or coordinate. The most common IPC primitives are:
- Pipes and FIFOs – unidirectional byte streams for simple producer‑consumer patterns.
- Message Queues – allow sending discrete messages, optionally with priorities.
- Shared Memory – the fastest method; processes map the same physical memory into their address spaces and use synchronization primitives to coordinate access.
- Semaphores and Mutexes – control access to shared resources; semaphores can count resources, while mutexes enforce exclusive access.
- Sockets – used for communication over a network, but also available for local inter‑process traffic (Unix domain sockets).
Each IPC method has trade‑offs. Pipes are simple but limited to linear data flow. Shared memory offers high throughput but requires careful synchronization to avoid race conditions.
Managing Resources and Synchronization
When multiple processes or threads touch the same resource, the OS must enforce consistency. Key concepts include:
- Mutual Exclusion (Mutex) – only one thread may hold the lock at a time.
- Read‑Write Locks – allow multiple readers but exclusive writers.
- Condition Variables – enable threads to wait for a condition to become true.
- Atomic Operations – single CPU instructions that guarantee indivisibility, useful for counters or flags.
- Deadlock Prevention – strategies such as ordering resource acquisition or using timeout mechanisms.
Operating systems provide system calls (e.g., pthread_mutex_lock, sem_wait) that wrap these primitives. Failure to use them correctly can lead to subtle bugs that are hard to reproduce.
Common Pitfalls and Debugging Strategies
Multiprocessing code is prone to several classes of bugs:
- Race Conditions – two or more threads read/modify a shared variable without proper locking.
- Priority Inversion – a low‑priority process holds a lock needed by a high‑priority one, causing the high‑priority process to block.
- Starvation – a process never gets CPU time because others dominate the scheduler.
- Deadlocks – cyclic waiting where each process holds a resource the other needs.
- Resource Leaks – forgetting to close file descriptors or release memory can exhaust system resources.
Debugging tools help identify these issues:
- Valgrind – detects memory leaks and race conditions in Linux.
- Thread Sanitizer – a compiler instrumentation that reports data races.
- Perf and OProfile – trace CPU usage and identify hotspots.
- System Call Tracers –
straceordtraceshow IPC and file operations. - Logging and Instrumentation – adding timestamps and thread IDs to logs can expose ordering problems.
Good practice is to design with minimal shared state, use immutable data where possible, and test under high concurrency to surface timing‑dependent bugs early.
Choosing the Right Multiprocessing Model
Not all problems benefit from multiprocessing. Consider the following factors:
- Task Granularity – very fine‑grained tasks may incur more overhead than the work they save.
- Data Dependencies – if tasks need frequent communication, the overhead of IPC can outweigh parallelism.
- Hardware Characteristics – NUMA nodes require careful placement of data to avoid remote memory penalties.
- Determinism Requirements – real‑time systems often prefer deterministic single‑threaded designs over complex multiprocessing.
- Development Complexity – multiprocessing adds synchronization complexity; if the performance gain is marginal, a simpler model may be preferable.
Typical models include:
- Parallel Processes – separate executables that communicate via IPC.
- Multithreaded Applications – multiple threads within the same process share memory.
- Thread Pools – a fixed set of worker threads that process a queue of tasks.
- Data‑Parallel Frameworks – e.g., OpenMP or MPI, which abstract the distribution of data across cores or nodes.
Selecting the right model often involves profiling the workload and measuring the cost of context switches, IPC, and synchronization.
Frequently Asked Questions
What is the difference between multiprocessing and multithreading?
Multiprocessing uses separate processes with isolated memory spaces; threads run within a single process and share memory. Processes provide stronger isolation but incur higher overhead for communication, while threads are lighter but require careful synchronization.
How does the OS decide which process runs on which CPU?
The scheduler uses algorithms like CFS or real‑time policies to assign processes to CPUs. It considers process priority, CPU affinity, and load balancing across cores to maximize throughput and responsiveness.
Can I avoid race conditions by using immutable data?
Yes, if data never changes after creation, multiple threads can read it without locks. However, for mutable shared state, you still need synchronization primitives to maintain consistency.
What tools help detect deadlocks?
Thread sanitizers can flag potential deadlocks, and debugging tools like gdb can inspect lock states. Profilers that trace lock acquisition order also help identify cyclic dependencies.
Is it safe to use shared memory across processes on a NUMA system?
Shared memory is safe, but accessing memory located on a remote NUMA node can incur latency penalties. Aligning data structures to local nodes and using NUMA‑aware allocation APIs reduces this overhead.