Process Synchronization
Process Synchronization
1. Introduction
• Multitasking Systems:
In an operating system, multiple processes may run simultaneously. When these processes
need to share resources like memory or CPU, synchronization is necessary to ensure that no
two processes interfere with each other.
• Critical Sections:
The part of a program that accesses shared resources is referred to as the critical section.
Synchronization mechanisms are used to ensure that only one process at a time can execute
in its critical section, thus avoiding conflicts or inconsistency.
• Processes in a system often need to access common resources (like memory, files,
printers, etc.). Without synchronization, multiple processes accessing these
resources simultaneously could lead to inconsistencies or data corruption. For
example, if two processes modify the same data at the same time, the result may be
unpredictable.
• Example: If two processes are updating a bank account balance at the same time,
without synchronization, one process might overwrite the changes made by the
other, leading to incorrect balance calculation.
• A race condition occurs when two or more processes attempt to modify shared data
at the same time, and the outcome depends on the timing of these events.
Synchronization ensures that processes execute in a predictable order.
3. Data Integrity:
• Synchronization prevents data corruption by ensuring that processes do not access
shared data concurrently. It ensures that once a process modifies shared data, no
other process can change that data until it has been properly handled.
• Example: If one process is writing to a file while another is reading from it,
synchronization ensures that the reading process does not access the file before the
writing process is finished.
4. Deadlock Prevention:
• Deadlock occurs when two or more processes are unable to proceed because each
one is waiting for the other to release a resource. Without synchronization, deadlock
can cause processes to wait indefinitely, blocking further execution.
• Example: If Process A holds Resource 1 and waits for Resource 2, while Process B
holds Resource 2 and waits for Resource 1, both processes are deadlocked. Proper
synchronization ensures processes do not get stuck in such situations.
5. Starvation Prevention:
Inter-Process Communication (IPC) is the mechanism that allows processes to communicate with
each other and synchronize their actions in a multitasking system. IPC plays a vital role in process
synchronization by enabling processes to exchange data and coordinate their execution without
conflicts.
• Shared Memory:
• In shared memory systems, processes communicate by accessing a common
area of memory.
• Multiple processes can read from and write to the same memory location.
However, synchronization is necessary to ensure that only one process
accesses the shared memory at a time to avoid corruption.
• Message Passing:
• Pipes:
• Pipes are used for communication between related processes. Data flows in
one direction, and one process writes to the pipe while another reads from
it.
• Sockets:
• IPC mechanisms provide a structured way for processes to share data, but they
require synchronization to prevent data inconsistency.
4. Producer-Consumer Problem
The Producer-Consumer Problem is a classic synchronization problem that involves two types of
processes: the producer and the consumer.
1. Problem Overview:
• The consumer retrieves and processes the data from the buffer.
• The consumer does not try to retrieve data from the buffer when it is empty
(buffer underflow).
2. Synchronization Solution:
• A semaphore is used to count the number of items in the buffer and to manage the
availability of buffer space:
• A mutex is used to ensure mutual exclusion so that only one process (either
producer or consumer) can access the buffer at any given time.
3. Steps in Synchronization:
• Producer process:
3. Adds an item to the buffer and signals that there is one more item in the buffer using
the full semaphore.
• Consumer process:
3. Retrieves an item from the buffer and signals that there is now an empty slot in the buffer
using the empty semaphore.