Classical Problems of Synchronization - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Classical Problems of Synchronization

Description:

The Bounded-Buffer Problem. Shared data. semaphore full, empty, mutex; full counts the number of full buffers, initialized to 0 ... – PowerPoint PPT presentation

Number of Views:1107
Avg rating:3.0/5.0
Slides: 21
Provided by: marily214
Category:

less

Transcript and Presenter's Notes

Title: Classical Problems of Synchronization


1
Classical Problems of Synchronization
  • The Bounded-Buffer Problem
  • The Readers-Writers Problem
  • The Dining-Philosophers Problem

2
The Bounded-Buffer Problem
  • Shared datasemaphore full, empty, mutex
  • full counts the number of full buffers,
    initialized to 0
  • empty counts the number of empty buffers,
    initialized to n
  • mutex initialized to 1

3
Bounded-Buffer Problem Producer Process
  • do
  • produce an item in nextp
  • wait(empty)
  • wait(mutex)
  • add nextp to buffer
  • signal(mutex)
  • signal(full)
  • while (1)

4
Bounded-Buffer Problem Consumer Process
  • do
  • wait(full)
  • wait(mutex)
  • remove an item from buffer to nextc
  • signal(mutex)
  • signal(empty)
  • consume the item in nextc
  • while (1)

5
The Readers-Writers Problem
  • A data object is shared among several processes
  • Readers - processes that only want to read the
    the shared object
  • Writers - processes that want to update (i.e.,
    read and write) the shared object
  • More than one readers are allowed to access the
    shared object simultaneously
  • Writers must have exclusive access to the shared
    object
  • No reader will be kept waiting unless a writer
    has already obtained permission to use the shared
    object
  • Writers may starve

6
The Readers-Writers Problem
  • Shared datasemaphore mutex, wrtint readcount
  • Initially, mutex 1, wrt 1, readcount 0

7
Readers-Writers Problem Writer Process
  • wait(wrt)
  • writing is performed
  • signal(wrt)

8
Readers-Writers Problem Reader Process
  • wait(mutex)
  • readcount
  • if (readcount 1)
  • wait(wrt)
  • signal(mutex)
  • reading is performed
  • wait(mutex)
  • readcount --
  • if (readcount 0)
  • signal(wrt)
  • signal(mutex)
  • If a writer is in the critical section
  • and n readers are waiting, then
  • One reader is queued on wrt
  • N-1 readers are queued on mutex
  • When a writer executes signal(wrt),
  • the scheduler resumes
  • either all waiting readers
  • or one waiting writer

9
The Dining-Philosophers Problem
  • When a philosopher gets hungry, she tries to pick
    up the left and right chopsticks
  • A philosopher may pick up only one chopstick at a
    time
  • When a philosopher is finished eating, she puts
    down both chopsticks

10
The Dining-Philosophers Problem
  • Shared data
  • semaphore chopstick5 // Initially all
    values are 1
  • Philosopher i
  • do
  • wait(chopstick i )
  • wait(chopstick (i1)5 )
  • eat
  • signal(chopstick i )
  • signal(chopstick (i1)5 )
  • think
  • while (1)

11
The Dining-Philosophers Problem
  • The solution guarantees no two neighbors are
    eating simultaneously, but may create a deadlock
  • Possible remedies
  • 1. Allow at most 4 philosophers to be sitting at
    the table
  • 2. Allow a philosopher to pick up his chopsticks
    only if both chopsticks are available
  • 3. An odd philosopher picks up first her left
    chopstick and then her right chopstick, an even
    philosopher picks up first her right chopstick
    and then her left chopstick
  • A deadlock-free solution may not be
    starvation-free

12
Critical Regions
  • Critical region is a high-level language
    synchronization construct
  • A shared variable v of type T is declared as
  • v shared T
  • Variable v can be accessed only inside a region
    statement
  • region v when (B) Swhere B is a boolean
    expression and S is a statement.
  • Only one of the critical regions associated with
    v can be executed at a time (enforced by
    compiler).
  • Statement S is only executed when B is true.
  • If B is false, the process is delayed until B
    becomes true and no other process is in the
    critical region associated with v.

13
Critical Region Example
  • Process 1
  • region v when (true) S1
  • Process 2
  • region v when (true) S2
  • If Process 1 and Process 2 execute concurrently,
    then either S1 is executed completely before S2
    or S2 is executed completely before S1

14
Example Bounded Buffer
  • Shared data struct buffer
  • item pooln
  • int count, in, out
  • Producer process
  • region buffer when (count lt n) poolin
    nextp in (in1) n count
  • Consumer process
  • region buffer when (count gt 0) nextc
    poolout out (out1) n count- -

15
Monitors
  • Another high-level synchronization construct.
  • Contain local variables and local procedures
    implementing operations on the variables
  • Local variables can only be accessed by the local
    procedures
  • At most one process at a time can be active
    within the monitor (enforced by compiler)

16
The Syntax of a Monitor
  • monitor monitor-name
  • shared variable declarations
  • procedure body P1 ()
  • . . .
  • procedure body P2 ()
  • . . .
  • procedure body Pn ()
  • . . .
  • initialization code

17
Monitors with Condition Variable
  • condition variables allow a process to wait for
    some condition to become true within the monitor
  • condition x, y
  • Operations that can be invoked on a condition
    variable
  • x.wait() calling process is suspended until
    another process invokes x.signal()
  • x.signal() wake up one suspended process. No
    effect if no process is suspended.
  • When a process does a signal for a condition,
  • either signaling process waits until awakened
    process exits monitor or waits for another
    condition
  • or awakened process waits until signaling process
    exits monitor or waits for another condition
  • The process that calls wait must use
  • while not B do
  • x.wait()

18
Dining Philosophers
  • A philosopher is allowed to pickup her chopsticks
    only if both of them are available
  • monitor dp
  • enum thinking, hungry, eating state5
  • condition self5
  • void pickup(int i)
  • void putdown(int i)
  • void test(int i)
  • void init()
  • for (int i 0 i lt 5 i)
  • state i thinking

19
Dining Philosophers
void test(int i) if ( (state (i4)5 !
eating) (state i
hungry) (state (i1)5 ! eating))
state i eating self i
.signal()
  • void pickup(int i)
  • state i hungry
  • test i
  • if (state i ! eating)
  • self i .wait()
  • void putdown(int i)
  • state i thinking
  • // test left right neighbors
  • test((i4) 5)
  • test((i1) 5)

20
Dining Philosophers
  • Philosopher i
  • while (1)
  • think
  • dp.pickup(i)
  • eat
  • dp.putdown(i)
  • Ensure no two neighbors are eating simultaneously
  • Deadlock free
  • NOT starvation free
  • One solution maintain a queue of philosophers.
    When a philosopher is hungry, she is put onto the
    tail of the queue. A philosopher may eat only if
    she is at the head of the queue and both
    chopsticks are free.
Write a Comment
User Comments (0)
About PowerShow.com