Write a note on semaphores
A semaphore is a tool meant for synchronizing multiple processes trying to access a shared variable. That is a semaphore is used to deal with the critical section problem or in other words to evade the race condition. A semaphore S is in fact an integer variable that apart from initialization is accessed only through two standard atomic operations:
*Wait and signal.
The traditional definition of wait in pseudo code is:
wait (S) {
while (S<=0)
; // no-op
S--;
}
The traditional definition of signal in pseudo code is:
signal (S) {
S++;
}
For each process P,
do {
WAIT
Critical Section
SIGNALS
} while (1);
A semaphore is able to be defined as:
typedef struct {
int value;
struct process *L;
} semphore;
Once more, the wait semaphore operation can be defined as:
void wait (semaphore S) {
S.value--;
if (S.value < 0) {
add this process to S.L;
block();
}
}
As well, the signal semaphore operation is able to be defined as:
void signal( semaphore S) {
S.value++;
if (S.value <= 0) {
remove a process P from S.L;
wakeup(P);
}
}