You are on page 1of 18

Unit No.

IV
Synchronization Variable
Semaphore and its types
LectureOutline
• Prerequisites
• Problem Objective
• Semaphore
• Operations of
Semaphore
• Binary Semaphore
• Count Semaphore
• Example
• Learning Outcomes

1
Prerequisites

• Basic knowledge of Processes and Process Execution


• Basics of Inter process Communication
• Critical Section and Mutual Exclusion
Session Objectives

• Understand about the need of semaphore


• Operations on semaphore
• Types of Semaphore
SELO
1. Ability to solve problems through application of
theoretical & practical concept.
2. Ability to observe and develop sense making, logical
skills for abstract concepts.
3. Application of concepts of topic & it’s technological
application.
Semaphore - Basics
• A semaphore is a variable or abstract data type used to
control access to a common resource by multiple
processes and avoid critical section problems in a
concurrent system such as a multitasking operating system
• Semaphore is simply a variable that is non-negative and
shared between threads. A semaphore is a signaling
mechanism, and a thread that is waiting on
a semaphore can be signaled by another thread. It uses
two atomic operations, 1)wait, and 2) signal for the process
synchronization.
• It is generally denoted by "S". You can use any other
variable name of your choice.
<SELO:1,2,3>
Define Semaphore
• A semaphore uses two functions:
-wait() This type of semaphore operation helps you to control
the entry of a task into the critical section. However, If the
value of wait is positive, then the value of the wait argument X
is decremented. In the case of negative or zero value, no
operation is executed. It is also called P(S) operation.
-signal() This type of Semaphore operation is used to control
the exit of a task from a critical section. It helps to increase the
value of the argument by 1, which is denoted as V(S).

<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

• In Counting semaphores, firstly, the semaphore variable


is initialized with the number of resources available.
• After that, whenever a process needs some resource, then
the wait() function is called and the value of the
semaphore variable is decreased by one.
• The process then uses the resource and after using
the resource, the signal() function is called and the
value of the semaphore variable is increased by one.

<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

After this Lecture students are able to:


• Semaphore and its operations
• Importance of Semaphore
• Types of Semaphore
References:
Text Books
1. A. Silberschatz, P. B. Galvin, G. Gagne; Operating System
Concepts; Wiley Publishing.
2. D. M. Dhamdhere; Operating Systems: A Concept-based
Approach; Tata McGraw-Hill Publishing.

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.

You might also like