Professional Documents
Culture Documents
IV
Synchronization Variable
Semaphore and its types
LectureOutline
• Prerequisites
• Problem Objective
• Semaphore
• Operations of
Semaphore
• Binary Semaphore
• Count Semaphore
• Example
• Learning Outcomes
1
Prerequisites
<SELO:1,2,3>
• Both wait() and signal() functions are used to change the
value of the semaphore.
• The value can be changed by only one process at a
particular time and no other process can change the value
simultaneously.
wait(S) { signal(S) {
while (S<=0); C.S.
S--; S++;
C.S. }
}
<SELO:1,2,3>
Wait () Function
• The wait() function is used to decrement the value of the semaphore
variable "S" by one if the value of the semaphore variable is positive.
If the value of the semaphore variable is 0, then no operation will be
performed.
wait(S) {
while (S == 0); //there is ";" sign here
S--;
}
<SELO:1,2,3>
Signal () Function
The signal() function is used to increment the value of the semaphore
variable by one.
signal(S)
{
S++;
}
<SELO:1,2,3>
Types of Semaphore
• There are two types of semaphores:
• Binary Semaphore
• Counting Semaphore
<SELO:1,2,3>
Binary Semaphores
• In Binary semaphores, the value of the semaphore variable will be 0
or 1.
• Initially, the value of semaphore variable is set to 1.
• If some process wants to use some resource then he wait() function is
called and the value of the semaphore is changed to 0 from 1.
• The process then uses the resource and when it releases the resource
then the signal() function is called.
<SELO:1,2,3>
Counting Semaphores
<SELO:1,2,3>
Advantages of semaphore
• Semaphores allow only one process into the critical section.
They follow the mutual exclusion principle strictly and are
much more efficient than some other methods of
synchronization.
• There is no resource wastage because of busy waiting in
semaphores as processor time is not wasted unnecessarily to
check if a condition is fulfilled to allow a process to access the
critical section.
• Semaphores are implemented in the machine independent
code of the microkernel. So they are machine independent.
<SELO:1,2,3>
Disadvantages of semaphore
• Sometimes the higher priority process has to wait for
the complete execution of the lower priority process.
• The wait() and signal() functions need to be
implemented in the correct order.
<SELO:1,2,3>
Learning Outcomes
References
1. William Stallings; Operating Systems; Pearson Publication.
2. P. K. Sinha; Distributed Operating Systems: Concepts and
Design; PHI Publication.
3. A. S. Tanenbaum; Modern Operating System; PHI
Publication.