0% found this document useful (0 votes)
43 views33 pages

CS 405 Lect 23 Semaphores in O.S.

Uploaded by

sgttcgwalior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views33 pages

CS 405 Lect 23 Semaphores in O.S.

Uploaded by

sgttcgwalior
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Program: B.

Tech , CS, 4TH Sem , 2nd year

CS 405 Operating System


Unit 4 Input / Output and Inter Process Communication
Topic: - Semaphores in O.S.
July-Dec 2020
Lecture 23

Shirish Mohan Dubey


Assistant Professor
Institute of Technology & Management CSE
Content
• Prerequisite of topic

• Semaphores

• Usage

• Implementation

• Types of Semaphore

• Exercise and Learning Outcomes

• 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.

• The student should have general idea about Process Synchronization.

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.

CS 405 <SELO: 1,4,8,9> Reference –R1,R5 03


Semaphores
The solutions to the critical-section problem are not easy to generalize to more
complex problems. To overcome this difficulty, we can use a synchronization tool,
called a semaphore. A semaphore S is an integer variable that, apart from
initialization, is accessed only through two standard atomic operations: wait and
signal.

CS 405 <SELO: 1,4,8,9> Reference –R1,R5 04


Semaphores
1. Wait:-
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed.

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++;
}

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 06


Semaphores

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.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 07


Semaphores

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

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 08


Properties of Semaphores

• It's simple and always have a non-negative Integer value.


• Works with many processes.
• Can have many different critical sections with different semaphores.
• Each critical section has unique access semaphores.
• Can permit multiple processes into the critical section at once, if desirable.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 09


Usage

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 <SELO: 1,4,8,9> Reference –R1,R4 10


Usage

We can also use semaphores to solve various synchronization problems. For


example, consider two concurrently running processes: PI with a statement S1, and
P2 with a statement P2- Suppose that we require that S2 be executed only after S1
has completed. We can implement this scheme readily by letting PI and P2 share a
common semaphore synch, initialized to 0, and by inserting the statements

CS 405 11
<SELO: 1,4,8,9> Reference –R1,R4
Mutual-exclusion implementation with semaphores

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 12


Solution of the critical-section problem

in process P2. Because synch is initialized to 0, P2 will execute S2 only after P1


has invoked signal(synch), which is after S1.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 13


Implementation

The main disadvantage of the mutual-exclusion solutions of the semaphore


definition given here, is that they all require busy waiting. While a process is in its
critical section, any other process that tries to enter its critical section must loop
continuously in the entry code. This continues lopping is clearly a problem in a real
multiprogramming system, where a single CPU is shared among many processes.

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

The advantage of a spinlock is that no context switch is required when a process


must wait on a lock, and a context switch may take considerable time. Thus,
when locks are expected to be held for short times, spinlocks are useful.

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. .

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 17


Implementation

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

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 18


Implementation

A process that is blocked, waiting on a semaphore S, should be restarted when


some other process executes a signal operation. The process is restarted by a
wakeup operation, which changes the process from the waiting state to the ready
state. The process is then placed in the ready queue.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 19


Types of Semaphore
The two common kinds of semaphores are

1. Counting semaphore

2. Binary semaphore

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 20


1. Counting 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 <SELO: 1,4,8,9> Reference –R1,R4 21


wait operation on the counting semaphore

The wait operation on the counting semaphore S can be implemented as follows:


wait(Sl);
C:=C- 1;
if C<0 then
begin signal(Sl);
wait(S2);
end
signal(Sl);

CS 405 22
<SELO: 1,4,8,9> Reference –R1,R4
signal operation on the counting semaphore

The signal operation on the counting semaphore S can be implemented as follows:

wait(Sl);
C:=C+1;
if C<0 then
signal(S2)
else
signal(Sl);

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 23


Binary Semaphores

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.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 24


Binary Semaphores

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

• Semaphores allow only one process into the critical section.


• 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.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 26


Disadvantages of Semaphores

• Semaphores are complicated so the wait and signal operations must be


implemented in the correct order to prevent deadlocks.
• Semaphores may lead to a priority inversion where low priority processes may
access the critical section first and high priority processes later.

CS 405 <SELO: 1,4,8,9> Reference –R1,R4 27


Exercise

1. Explain the Semaphore Operation.

2. Define types of Semaphore .

3. Discuss the advantage and disadvantage of Semaphore.

CS 405 28
Learning Outcome
• Learn about the Semaphore.

• Learn about the usage of Semaphore.

• Learn about Implementation.

• Learn about the Types of Semaphore.

• Learn about the Advantage and Disadvantage of Semaphore.

CS 405 29
SELO

1. Ability to solve problems through application of theoretical & practical


concept.

4. Lifelong learning ability.

8. Ability to understand subject related concepts clearly along with contemporary

issues

9. Application of concepts of topic & it’s technological application.

CS 405 30
References

R1 :E-BOOK -Operating System Concepts” by Silberschatz and Peter Galvin.

R2:Modern Operating Systems (Hardcover) by Andrew S. Tanenbaum.

R3:Stuart,”Operating System Principles, Design &Applications", engage Learning

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy