0% found this document useful (0 votes)
65 views28 pages

OS 15 - Semaphores PDF

This document defines semaphores and discusses their use for synchronization in concurrent programs. Semaphores can be used as locks (binary semaphores), to order events, and for the producer-consumer problem. Reader-writer locks and the dining philosophers problem are also addressed. Semaphores can throttle threads to prevent too many from executing the same code simultaneously. Their implementation typically uses wait and signal operations on an integer value.

Uploaded by

Đức Công
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views28 pages

OS 15 - Semaphores PDF

This document defines semaphores and discusses their use for synchronization in concurrent programs. Semaphores can be used as locks (binary semaphores), to order events, and for the producer-consumer problem. Reader-writer locks and the dining philosophers problem are also addressed. Semaphores can throttle threads to prevent too many from executing the same code simultaneously. Their implementation typically uses wait and signal operations on an integer value.

Uploaded by

Đức Công
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Operating Systems

Semaphores

Le Hai Duong, PhD. ([email protected])

1
Outline

● Definition of semaphores
● Binary semaphores (Locks)
● Semaphores for ordering
● The producer/consumer (bounded buffer) problem
● Reader-writer locks
● The dining philosophers
● Thread Throttling
● Implement semaphores

2
Semaphore ( lit. 'apparatus for signalling'; from Ancient Greek σῆμα
(sêma) 'mark, sign, token', and Greek -φόρος (-phóros) 'bearer,
carrier') is the use of an apparatus to create a visual signal transmitted
over distance…
– Wikipedia (https://en.wikipedia.org/wiki/Semaphore )

3
Overview

● Edsger Dijkstra introduced a synchronization primitive


called the semaphore as a single primitive for all things
related to synchronization
○ can use semaphores as both locks and condition variables

4
Semaphore: definition

● A semaphore is an object with an integer value that we


can manipulate with two routines; in the POSIX standard,
these routines are sem_wait() and sem_post()
● The initial value of the semaphore determines its behavior
→ must first initialize it to some value
initialize semaphore to 1

0: shared between threads in 1 process


5
Semaphore: definition

● The value of semaphore, when negative, is equal to the


number of waiting threads

6
Binary semaphores

● Using a semaphore as a lock


● Locks only have two states (held and not held) → call a
semaphore used as a lock a binary semaphore

7
Binary semaphores (conti.)

8
Binary semaphores (conti.)

9
Semaphores for ordering

● Semaphores are also useful to order events in a


concurrent program.
○ One thread waits for something to happen, and another thread makes
that something happen and then signals that it has happened, thus
wakes the waiting thread → using the semaphore as an ordering
primitive (similar to the use of condition variables).
● Example is as follows → a thread creates another thread
and then wants to wait for it to complete its execution.

10
Semaphores for ordering (conti.)
What should the initial value of this semaphore be?

11
Semaphores for ordering (conti.)

12
Semaphores for ordering (conti.)

13
Setting the value of a semaphore

● Set the value to


○ 1 to use the semaphore as a lock
○ 0 to use the semaphore for ordering

→ Consider the number of resources to give away


immediately after initialization

14
The producer/consumer (bounded
buffered) problem

15
First attempt
Let MAX = 1

16
First attempt
Let MAX = 10
→ race
condition

17
A solution:
adding mutual
exclusion

→ deadlock

18
Avoiding deadlock
→ move the mutex acquire and release to be just around
the critical section
19
Reader-writer locks

● Classic problem → a more flexible locking primitive in


which different kinds of locking are required for different
data structure accesses
○ For example, imagine a number of concurrent list operations,
including inserts and simple lookups
■ Inserts change the state of the list → a traditional critical section
■ Lookups simply read the data structure → can allow many lookups to
proceed concurrently
■ The special type of lock supports this type of operation is known as a
reader-writer lock

20
21
The dining philosophers

● The problem is famous because it is fun and somewhat


intellectually interesting; however, its practical utility is
low.
● The basic setup for the problem is this: assume there are
five “philosophers” sitting around a table
○ Between each pair of philosophers is a single fork (and thus, five
total).
○ In order to eat, a philosopher needs two forks, both the one on their
left and the one on their right.
○ The contention for these forks, and the synchronization problems that
ensue, are what makes this a problem we study in concurrent
programming.

22
The dining philosophers (conti.)

23
24
Thread throttling
● How can a programmer prevent “too many” threads from doing
something at once and bogging the system down?
Answer: decide upon a threshold for “too many”, and then
use a semaphore to limit the number of threads concurrently
executing the piece of code in question.
→ throttling, and consider it a form of admission control
● A simple semaphore can solve this problem.
○ Initializing the value of the semaphore to the maximum number of threads
○ Put a sem_wait() and sem_post() around the admission controlled
region → a semaphore can naturally throttle the number of threads that are
ever concurrently in the dangerous region of the code

25
Implement semaphores

26
Implement semaphores (conti.)

27
Summary

● Semaphores are a powerful and flexible primitive for


writing concurrent programs → some programmers use
them exclusively, shunning locks and condition variables,
due to their simplicity and utility.

28

You might also like