Process Sychronisation
Process Sychronisation
Threads
IPC refers to the mechanisms that allow processes to communicate with each other. This
is crucial in a multi-process system where processes need to share data and synchronize
their actions.
IPC can be implemented using various techniques:
o Shared Memory: A region of memory shared between processes where they can
read and write data.
o Message Passing: Processes communicate by sending messages to each other.
o Sockets: Network-based IPC, used for communication between processes on
different machines.
Clock Synchronization
Mutual Exclusion
Mutual Exclusion (Mutex) refers to ensuring that multiple threads or processes do not
simultaneously execute a critical section of code that accesses shared resources, which
could lead to inconsistent data.
Key requirements for mutual exclusion:
1. Mutual Exclusion: Only one process or thread can execute in the critical section
at a time.
2. Progress: If no thread is in the critical section, one should be allowed to enter.
3. Bounded Waiting: Each thread should be able to enter the critical section within
a bounded time.
Critical Section
The critical section is a part of the code where shared resources are accessed or
modified. Proper synchronization must be used to avoid conflicts between processes or
threads.
4. Race Conditions
Race Condition
A race condition occurs when the behavior of a program depends on the relative timing
of uncontrollable events, such as thread execution order, leading to unpredictable
outcomes.
A race condition typically occurs when multiple threads/processes access shared data
concurrently, and at least one thread modifies the data.
Example: If two threads increment a shared counter, without synchronization, the result
may be incorrect due to interleaving of operations.
Using synchronization mechanisms like mutexes, semaphores, and locks ensures that
only one thread can access shared data at a time, preventing race conditions.
5. Semaphores
Semaphores
Types of Semaphores
1. Counting Semaphores: These semaphores can take any integer value and are used for
managing a pool of resources.
2. Binary Semaphores (Mutex): These are a special case of semaphores with values
restricted to 0 and 1, typically used for mutual exclusion.
Uses of Semaphores
Mutual Exclusion: Ensuring that only one process/thread accesses a critical section at a
time.
Synchronization: Coordinating the order of execution between processes/threads.
Resource Allocation: Managing a limited number of resources (e.g., file handles or
database connections).
6. IPC Problems
Problems in IPC
1. Deadlock: A situation where two or more processes are unable to proceed because each
is waiting for the other to release resources.
o Conditions for Deadlock:
Mutual Exclusion
Hold and Wait
No Preemption
Circular Wait
o Solutions: Prevention, detection, and recovery strategies.
2. Starvation: A process may never get access to resources if other processes with higher
priority keep requesting resources.
o Solution: Fair scheduling algorithms, priority adjustments.
3. Race Conditions: As discussed earlier, race conditions occur when two or more
threads/processes access shared data concurrently and the final outcome depends on the
timing of execution.
4. Synchronization Overhead: The time spent on managing synchronization can reduce
the overall efficiency of a system, especially if excessive locking is involved.
5. Communication Latency: In message-passing IPC systems, the time taken for messages
to travel between processes can impact performance.
Summary
13. Which of the following techniques is used to implement mutual exclusion in a system?
a) Semaphore.
b) Message passing.
c) Shared memory.
d) Context switching.
Race Conditions
19. In which scenario would you need to use a lock or semaphore to avoid a race condition?
Semaphores
a) Mutex.
b) Counting semaphore.
c) Process semaphore.
d) Thread semaphore.
24. Which of the following is true about semaphores?
a) Between 0 and 1.
b) Between -1 and 1.
c) Any integer value.
d) Only positive integers.
IPC Problems
a) Mutual Exclusion.
b) No Preemption.
c) Bounded Waiting.
d) Circular Wait.
a) A process waiting for resources that are being held by another process.
b) A process continuously running without any delay.
c) A process releasing resources once it finishes execution.
d) A process allocating resources to other processes.