0% found this document useful (0 votes)
58 views

Synchronization: Semaphores

This document discusses semaphores as a tool for synchronization in operating systems. It begins with an example of a race condition that can occur between threads accessing shared data. It then provides an analogy of a library with a limited number of study rooms to demonstrate resource contention. Finally, it introduces semaphores as a synchronization primitive consisting of an integer variable and wait/signal operations that can be used to control access to shared resources and prevent race conditions.

Uploaded by

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

Synchronization: Semaphores

This document discusses semaphores as a tool for synchronization in operating systems. It begins with an example of a race condition that can occur between threads accessing shared data. It then provides an analogy of a library with a limited number of study rooms to demonstrate resource contention. Finally, it introduces semaphores as a synchronization primitive consisting of an integer variable and wait/signal operations that can be used to control access to shared resources and prevent race conditions.

Uploaded by

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

Synchronization

Operating System Concepts

Semaphores

Ahmad Uways Zulkurnain

Contents
Review

of race conditions
Library analogy to demonstrate
resource contention
Introduction to semaphores

Review: Race Conditions


Thread 1:

Thread 2:

a = data;
a = a+1;
data = a;

b = data;
b = b + 1;
data = b;

Resulting value of data?

Review: Race Conditions


Scenario

Thread 1
Thread 2
a =
data;
--a = a+1; --data =
a;
---

dat
a
0
0
1

Review: Race Conditions


Scenario

Thread 1
Thread 2
a =
data;
--a = a+1; b = data;
data =
a;
---

dat
a
0
0
1

Solutions?
Locks

Hardware based atomic operations*


Software based language support

*sequence of one or more machine instructions that are


executed sequentially, without interruption

What

about resources that can


handle multiple, but limited
number of users simultaneously?

Library Analogy
emp
ty

library has x study rooms, used by one


student at a time
Students must request a room from the
front desk
If no rooms are free, students wait at the
desk until one is free.
When finished with a room, the student
must return to the desk and indicate that
one room has become free.
A

Library Analogy
What

is known:

Number of free rooms


What

is not known:

which rooms are occupied


who is using them
If a room is actually being used

Library Analogy
emp
ty

Study

rooms resources
Students processes
Front desk semaphore

Semaphores
By

E.W. Dijkstra (19302002)


Published in paper
On the sequentiality of
process descriptions
1962/63

Semaphores
Synchronization
Semaphore,

tool

integer variable
atomic increment and decrement
waiting list
Two

standard operations modify S

wait()
signal()
S

can only be accessed by wait()


and signal()

Semaphores
typedef struct{
int value;
process *list;
} semaphore;
wait(S) {}
block() {}

signal(S) {}
wakeup(P) {}

wait()
Decrements

S value
If the value is negative, adds
requesting process to waiting list
and blocks the requesting process
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add process to S->list;
block();
}
}

signal()
Increments

S value
If the value is not positive, removes
a process from waiting list and
wakes it up
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove process P from S->list;
wakeup(P);
}
}

Types of Semaphores
Counting

semaphore
Binary semaphore
Value can be only 1 or 0
Known as mutex
Constructed from counting
semaphore by initializing value to 1
Provides mutual exclusion

Binary Semaphore
semaphore mutex(1);
while(TRUE) {
wait(mutex);
// critical section of code to lock
signal(mutex);
// continue code
}

Does

not require busy waiting

Applications of
Semaphores
Bounded-Buffer

Problem
Readers and Writers Problem
Dining-Philosophers Problem

You might also like