COMP310 Lecture 6 Semaphores - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

COMP310 Lecture 6 Semaphores

Description:

Assume buffer ops are atomic. 11/3/08. COMP310 Lecture 6. 5. Resource Problems ... Only one process ever uses each fork at one time. ( Mutual exclusion. ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 25
Provided by: mcsV
Category:

less

Transcript and Presenter's Notes

Title: COMP310 Lecture 6 Semaphores


1
COMP310 Lecture 6Semaphores
Ben-Ari Chapter 6.
  • Concurrency Problems
  • Semaphores
  • CPs With Semaphores
  • Mutual Exclusion with Semaphores

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

3
Producers 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

4
Producers 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
5
Resource 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. (!)?

6
Dining Philosophers
BA, 6.9
7
Semaphores
  • 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.
8
Semaphores
  • 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.

9
Process States
Scheduling
Ready
Running
Blocked
10
Process States
Scheduling
Ready
Running
Blocked
11
Process States
Scheduling
Ready
Running
Blocked
12
Process 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.

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

14
Semaphores
Scheduling
Ready
signal(s)?
Running
Blocked
wait(s)?
15
Semaphores (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.
16
Semaphores (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.
17
Critical 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.

18
Mutual Exclusion with Semaphores
Generalises easily to N processes (6.5).
19
Prod/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.

20
Producers 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)?
21
Mutual Exclusion
  • Does this prod/con solution provide mutual
    exclusion (In the sense that -(p3 /\ q2))?
  • a) Yes.
  • b) No.
  • c) Depends.

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

23
Dining Philosophers
  • process Philosopher loop n1 think
    n2 preprotocol n3 eat n4
    postprotocol

24
Correctness
  • 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.)?
Write a Comment
User Comments (0)
About PowerShow.com