Mutual Exclusion
Mutual Exclusion
MUTUAL EXCLUSION
1. Critical Section
2. Race Condition
CRITICAL SECTION
Key Features:
Values: 0 (resource is unavailable) or 1 (resource is
available).
Mutual exclusion: Ensures that only one thread or process
can access the critical section at a time.
Simpler to implement than counting semaphores because
they only track two states.
BINARY SEMAPHORES
Operations:
Wait (P operation): The process checks the semaphore value.
If it is 1, the process proceeds and sets the value to 0,
effectively "locking" the resource. If the value is 0, the
process waits.
Signal (V operation): When the process finishes, it sets the
semaphore value back to 1, "unlocking" the resource and
allowing another process to proceed.
BINARY SEMAPHORES
Example Scenario:
Suppose you have a printer shared by multiple processes. A
binary semaphore can be used to control access to the
printer:
When a process needs to print, it waits on the semaphore. If
the printer is free (semaphore value is 1), the process prints
and sets the semaphore to 0, preventing other processes
from printing.
After printing, the process signals the semaphore, setting it
back to 1, so the next process can print.
BINARY SEMAPHORES
Key Features:
Values: Any non-negative integer, representing the number
of available resources.
Resource control: Used when there are multiple instances of
a resource and we want to allow a limited number of
processes to access them concurrently.
COUNTING SEMAPHORES
Operations:
Wait (P operation): The process checks the semaphore value.
If it is greater than 0, the process proceeds and decrements
the value by 1. If the value is 0, the process waits (indicating
no resources are available).
Signal (V operation): When the process finishes using the
resource, it increments the semaphore value by 1, signaling
that a resource has been released.
COUNTING SEMAPHORES
Example Scenario:
Consider a database connection pool with 5 connections:
Initially, the counting semaphore is initialized to 5, meaning
5 database connections are available.
When a process requests a connection, it waits on the
semaphore. If the semaphore value is greater than 0, the
process gets a connection, and the semaphore is
decremented. If all connections are in use (value = 0), the
process waits.
When a process finishes using a connection, it signals the
semaphore, incrementing the value and making the
connection available for another process.
COUNTING SEMAPHORES
Practical Uses:
Binary Semaphore: Used for protecting critical sections
where only one process or thread should enter, such as
writing to a file, accessing a printer, or modifying shared
memory.
Counting Semaphore: Used when multiple instances of a
resource are available, like managing a pool of database
connections, limiting the number of threads accessing a
network socket, or controlling the number of available seats
in a theater booking system.
MONITORS
Mutual Exclusion:
Only one process can be in the critical section at a time.
MUTUAL EXCLUSION REQUIREMENTS
Progress:
If no process is in the critical section, one of the processes
that wish to enter must be allowed to proceed.
The decision about which process enters the critical section
cannot be postponed indefinitely.
MUTUAL EXCLUSION REQUIREMENTS