Semaphores and their applications
Semaphore instructions: special variables used to signal processes. It is a class of variable. Semaphore is a protected variable. They are not accessible through other (normal) instructions.
They are accessible only through special instructions. For eg. Process number : semaphore
Semaphore take on non-negative integer values like(0,1,2,3,….)
A semaphore can be :
- Counting semaphore :- it can take all +ve integer values.
- Binary semaphore:- it can take 0 & 1 (Boolean values True & False)
Binary semaphore is a special case of counting semaphore.
Instruction for counting semaphore
P(s):- if S>0, then S=S-1 S is a semaphore
Else wait on S
Wait on S ---> wait in a queue called S.
V(S) :- If one or more processes are waiting
then allow one of them
P(S) and V(S) are inverses of each other. Whatever done in P(S) can be undone in V(S).
Semaphore initialize(S,X) : This initializes S to X.
These instruction are indivisible. This is exactly like in test and set.
Several problems can be solved through semaphore.
Busy wait: this means that even during wait we are checking some conditions continually.
The processor is busy since we are using the processor for checking.
Blocked wait:Here the processes wait in the queue. When once of the process completes its work, the next process in the queue in the queue (P2) is handed the processor.