Topic: Inter-Process Communication (IPC) & Synchronization
1. INTRODUCTION TO INTER-PROCESS COMMUNICATION (IPC)
Definition
Inter-Process Communication (IPC) is a mechanism provided by the operating system that
allows processes to communicate with each other and synchronize their actions. It is the set of
methods by which independent processes manage shared data.
Why is IPC needed?
In a modern OS, processes are either Independent or Cooperating. IPC is essential for
cooperating processes for the following reasons:
• Information Sharing: Multiple users may need access to the same piece of information
(e.g., a shared database).
• Computation Speedup: A single task can be broken into sub-tasks and run in parallel
on different CPUs.
• Modularity: Dividing system functions into separate processes or threads.
• Convenience: A user may work on many tasks at once (editing, printing, and compiling).
Basic Models of IPC
1. Shared Memory: A region of memory is established that is shared by cooperating
processes. It is very fast but requires manual synchronization.
2. Message Passing: Processes communicate by exchanging messages. This is useful for
smaller amounts of data and easier to implement in distributed systems.
2. THE CONCURRENCY PROBLEM
Race Condition
A Race Condition occurs when multiple processes access and manipulate the same data
concurrently, and the outcome of the execution depends on the particular order in which the
access takes place.
• Example: Two processes trying to increment a shared counter. If both read the value
"10" at the exact same time, both will increment it to "11" and write it back, losing one
increment operation.
Critical Section
The Critical Section is the part of a program where shared resources (variables, files, tables)
are accessed.
Requirements for a Valid Solution:
1. Mutual Exclusion: If process $P_i$ is in its critical section, no other process can be.
2. Progress: If no process is in the critical section, the processes that want to enter must
decide who goes next; this decision cannot be postponed indefinitely.
3. Bounded Waiting: No process should have to wait forever to enter its critical section.
3. SYNCHRONIZATION SOLUTIONS
A. Hardware Solution (Test-and-Set)
Many systems provide hardware instructions that allow you to test and set the value of a
variable atomically (in one uninterruptible step). If a process sets a "lock" variable to true,
others are blocked until the lock is released.
B. Strict Alternation
A software solution where processes use a turn variable to take turns.
• Drawback: It requires Busy Waiting (using CPU cycles just to wait) and fails the
"Progress" requirement if one process is much slower than the other.
C. Peterson’s Solution
A classic software-based solution for two processes. It uses a turn variable and a flag array. It is
a complete solution that satisfies Mutual Exclusion, Progress, and Bounded Waiting.
4. SEMAPHORES & MONITORS
Semaphores
A Semaphore is a synchronization tool that uses an integer variable. It is accessed via two
atomic operations:
1. Wait (P): Decrements the value. If value < 0, the process blocks.
2. Signal (V): Increments the value. If there are blocked processes, one is woken up.
3. Monitors
A Monitor is a high-level synchronization construct (like a class in OOP) that ensures
only one process is active within the monitor at a time. It simplifies coding by handling
the "locking" automatically.
4.
5 CLASSICAL IPC PROBLEMS
A. Producer-Consumer Problem
A producer puts data in a buffer, and a consumer takes it out. IPC ensures the producer doesn't
add to a full buffer and the consumer doesn't take from an empty one.
B. Readers-Writers Problem
Multiple readers can read at once, but only one writer can write at a time. While a writer is
active, no readers are allowed.
C. Dining Philosophers Problem
philosophers share 5 chopsticks. Each needs 2 to eat. This problem illustrates Deadlock and
Starvation in resource allocation.
Summary
• Mutual Exclusion is the "Goal."
• Semaphores are the "Tool."
• Deadlock is the "Risk."