Semaphores
Semaphores are a synchronization tool used in concurrent programming to manage access to shared resources. They are particularly important in solving problems related to resource sharing among multiple processes or threads in an operating system or a multiprocessing environment.
Binary Semaphore:
A binary semaphore is the simplest form of semaphore, which can take only two values: 0 and 1. It's used to control access to a single shared resource. A binary semaphore is essentially a mutex.
Operations:
Wait (P): If the semaphore value is 1, decrement it to 0 and proceed (grant access to the resource). If it's already 0, the process must wait (block) until it becomes 1.
Signal (V): Increment the semaphore value to 1, indicating the resource is free. If there are other processes waiting, one of them gets access to the resource.
Pseudo Code:
P(semaphore) {
if semaphore == 1 {
semaphore = 0
} else {
// wait or block the process
}
}
V(semaphore) {
semaphore = 1
// wake up a waiting process if any
}
Counting Semaphore:
A counting semaphore can have an integer value greater than or equal to 0 and is used for controlling access to a set of identical resources, not just one.
Operations:
Wait (P): Decrements the semaphore value. If the result is negative, the process blocks until the value is greater than or equal to 0.
Signal (V): Increments the semaphore value. If there are waiting processes, one is unblocked.
Pseudo Code:
P(semaphore) {
semaphore = semaphore - 1
if semaphore < 0 {
// block the process
}
}
V(semaphore) {
semaphore = semaphore + 1
if semaphore <= 0 {
// unblock a waiting process
}
}
Solving the Critical Section Problem:
The critical section problem involves ensuring that when one process is executing in its critical section (a section of code accessing a shared resource), no other process is allowed to execute in its critical section.
Pseudo Code Using Semaphores:
Semaphore mutex = 1 // Binary semaphore for mutual exclusion
// Process A
P(mutex)
// Critical section
V(mutex)
// Process B
P(mutex)
// Critical section
V(mutex)
In this pseudo code, mutex
is a binary semaphore initialized to 1. Before entering the critical section, a process executes the P
operation on mutex
. If mutex
is 0, it means another process is in its critical section, and the current process will wait. After completing the critical section, the process executes the V
operation to release the semaphore, allowing other waiting processes to enter their critical sections.
Atomicity of P and V Operations
Atomicity ensures that the operations are executed as indivisible units, meaning that no other operations can interrupt or be interleaved with the execution of a P or V operation. This is essential for maintaining the consistency and integrity of the semaphore's value, which is critical for correctly synchronizing access to shared resources.
If P and V operations weren't atomic:
Two or more processes might simultaneously find the semaphore value positive in a P operation, leading them all to enter the critical section, violating mutual exclusion.
Simultaneous V operations might not correctly increment the semaphore value, leading to incorrect synchronization logic.
To avoid these issues, P and V operations are designed to be atomic in semaphore implementations.
Origin of P and V
The terms P and V come from the Dutch language, chosen by the semaphore concept's inventor, Edsger Dijkstra, a pioneering computer scientist from the Netherlands.
P (Proberen): Translates to "to test" or "to try". In the context of semaphores, it's used to test if the semaphore is available (non-zero) and then to decrement it, which corresponds to trying to enter the critical section.
V (Verhogen): Means "to increment" or "to increase". This operation increments the semaphore value, signaling the end of using a shared resource or leaving the critical section.
These operations are foundational in concurrent programming, ensuring that processes or threads can safely and efficiently share resources without conflicts or deadlocks, underlining Dijkstra's significant contributions to the field.