Title: COMP310 Lecture 6 Semaphores
1COMP310 Lecture 6Semaphores
Ben-Ari Chapter 6.
- Concurrency Problems
- Semaphores
- CPs With Semaphores
- Mutual Exclusion with Semaphores
2Semaphores
- Provide elegant solutions to many synchronisation
problems. - Dijkstra, 1965. (THE operating system.)?
- Used to control access to resources.
- Used to solve order-of-execution problems.
3Producers and Consumers
- A producer and consumer communicate via a buffer
variable. - One producer deposits item in buffer put(d,
buf) - One consumer fetches item from buffer d
get(buf) - Buffer is finite (has fixed capacity C).
- Only allowed to call
- put when the buffer is not full
- get when the buffer is not empty
4Producers and Consumers
Buffer buf
consumer
producer
dataType d loop forever q1 while(buf.empty()) q2
d get(buf)? q4 consume(d)?
dataType d loop forever p1 d produce p2
while(buf.full()) p3 put(d, buf)?
Assume buffer ops are atomic
5Resource Problems
- The Critical Section Problem
- Each process competes for exclusive access to a
shared resource. - The Dining Philosophers Problem
- Each process competes for exclusive access to
multiple shared resources. - Project One
- Each process competes for access to multiple
shared resources. (!)?
6Dining Philosophers
BA, 6.9
7Semaphores
- The semaphore s has two operations
- wait(s) (sometimes called P(s))?
- signal(s) (also called V(s) or notify(s))?
- In Java, obj.wait() and obj.notify() are somewhat
like semaphore operations for obj.
P and V are initials of Dutch words for pass
and release. Or something like that.
8Semaphores
- Semaphores are used to pass permissions between
processes. - Permission is a metaphor. It helps understanding
what semaphores do. - wait(s) waits for a permission.
- signal(s) creates or hands over a permission.
9Process States
Scheduling
Ready
Running
Blocked
10Process States
Scheduling
Ready
Running
Blocked
11Process States
Scheduling
Ready
Running
Blocked
12Process States
- Each process can be in one of several states
- Running the process is executing code.
- Ready the process is not currently executing
code, but its state may be changed to running by
the scheduler. - Blocked the process is not currently executing
code and its state cannot be changed directly to
running. It's state may change to ready.
13Semaphores
- A semaphore s has two fields
- s.value, a non-negative integer
- s.waitset, a set of processes
- s.value counts the available permissions.
- s.waitset is a set of processes waiting to
receive a permission.
14Semaphores
Scheduling
Ready
signal(s)?
Running
Blocked
wait(s)?
15Semaphores (Strong, Blocking)?
wait(s) if (s.value gt 0)? s.value
s.value - 1 else s.waitset s.waitset
p p.state blocked
All happens atomically.
16Semaphores (Strong, Blocking)?
signal(s) if (s.waitset is empty)?
s.value s.value 1 else s.waitset
s.waitset - q p.state ready
All happens atomically.
17Critical Sections with Semaphores
- In the crit. sect. problem, there is one
permission to enter the CS. - This permission can be passed around using a
semaphore.
18Mutual Exclusion with Semaphores
Generalises easily to N processes (6.5).
19Prod/Con with Semaphores
- The producer can take a permission to call put
when the buffer is not full. - The consumer can take a permission to call get
when the buffer is not empty. - Use semaphores toPut and toGet to manage these
permissions.
20Producers and Consumers
semaphore toPut,toGet Buffer buf toPut (C,
) toGet (0, )?
consumer
producer
dataType d loop forever q1 wait(toGet)? q2 d
get(buf)? q3 signal(toPut)? q4 consume(d)?
dataType d loop forever p1 d produce p2
wait(toPut)? p3 put(d, buf)? p4 signal(toGet)?
21Mutual Exclusion
- Does this prod/con solution provide mutual
exclusion (In the sense that -(p3 /\ q2))? - a) Yes.
- b) No.
- c) Depends.
22Dining Philosophers
- There is a set of processes (philosophers).
- Each process (philosopher) does two things
thinking and eating. - Before eating, a philosopher must acquire
exclusive access to two forks. - The forks are arranged in a circle around the
table.
23Dining Philosophers
- process Philosopher loop n1 think
n2 preprotocol n3 eat n4
postprotocol
24Correctness
- Each philosopher eats, only after acquiring two
forks. - Only one process ever uses each fork at one time.
(Mutual exclusion.)? - If any process tries to eat, then eventually some
process succeeds in eating. (Deadlock freedom.)? - If some process tries to eat, that process
eventually eats. (Starvation freedom.)?