CS 405 Lect 23 Semaphores in O.S.
CS 405 Lect 23 Semaphores in O.S.
• Semaphores
• Usage
• Implementation
• Types of Semaphore
• References.
CS 405 01
Prerequisite of topic
• The Students should have general idea about Concepts of Memory
Management.
• The Students should have general idea about Paging and Segmentation in O.S.
• The students should have general idea about Dynamic Linking and Loading in
O.S.
• The student should have general idea about Inter Process Communication.
CS 405 02
Semaphores
Semaphore was proposed by Dijkstra in 1965 which is a very significant technique
to manage concurrent processes by using a simple integer value, which is known as
a semaphore. Semaphore is simply a variable which is non-negative and shared
between threads. This variable is used to solve the critical section problem and to
achieve process synchronization in the multiprocessing environment.
wait(S)
while (S<=0);
S--;
}
CS 405 <SELO: 1,4,8,9> Reference –R1,R4 05
Semaphores
2.Signal :-The signal operation increments the value of its argument S.
Signal
(S)
{
S++;
}
Modifications to the integer value of the semaphore in the wait and signal
operations must be executed indivisibly. That is, when one process modifies the
semaphore value, no other process can simultaneously modify that same semaphore
value.
In addition, in the case of the wait(S), the testing of the integer value of S (S < 0),
and its possible modification (S := S — 1), must also be executed without
interruption
We can use semaphores to deal with the n-process critical-section problem. The
n processes share a semaphore, mutex (standing for mutual exclusion),
initialized to 1. Each process Pi is organized as shown in Figure 1.
CS 405 11
<SELO: 1,4,8,9> Reference –R1,R4
Mutual-exclusion implementation with semaphores
CS 405 14
<SELO: 1,4,8,9> Reference –R1,R4
Implementation
Busy waiting wastes CPU cycles that some other process might be able to use
productively. This type of semaphore is also called a spinlock (because the
process "spins" while waiting for the lock). Spinlocks are useful in
multiprocessor systems.
CS 405 15
<SELO: 1,4,8,9> Reference –R1,R4
Implementation
CS 405 16
<SELO: 1,4,8,9> Reference –R1,R4
Implementation
To overcome the need for busy waiting, we can modify the definition of the wait
and signal semaphore operations. When a process executes the wait operation and
finds that the semaphore value is not positive, it must wait. However, rather than
busy waiting, the process can block itself. .
The block operation places a process into a waiting queue associated with the
semaphore, and the state of the process is switched to the waiting state. Then,
control is transferred to the CPU scheduler, which selects another process to
execute
1. Counting semaphore
2. Binary semaphore
These are integer value semaphores and have an unrestricted value domain. These
semaphores are used to coordinate the resource access, where the semaphore count
is the number of available resources. If the resources are added, semaphore count
automatically incremented and if the resources are removed, the count is
decremented.
CS 405 22
<SELO: 1,4,8,9> Reference –R1,R4
signal operation on the counting semaphore
wait(Sl);
C:=C+1;
if C<0 then
signal(S2)
else
signal(Sl);
The binary semaphores are like counting semaphores but their value is restricted to
0 and 1. A binary semaphore can be simpler to implement than a counting
semaphore, depending on the underlying hardware architecture.
The wait operation only works when the semaphore is 1 and the signal operation
succeeds when semaphore is 0. It is sometimes easier to implement binary
semaphores than counting semaphores.
CS 405 25
<SELO: 1,4,8,9> Reference –R1,R4
Advantages of Semaphores
CS 405 28
Learning Outcome
• Learn about the Semaphore.
CS 405 29
SELO
issues
CS 405 30
References
R4: Operating System : Principle and Design by Pabitra Pal Choudhury, PHI
Learning.
R5: https://www.tutorialspoint.com/semaphores-in-operating-system
CS 405 31
CS 405 32