Lecture Notes for Concurrent Programming

24 June 2003 - Using Semaphores


Outline


Mutual Exclusion


Mutex Semaphores


Conditional Synchronization


Signaling Semaphores


Split-Binary Semaphores

split-binary semaphores in use


Multi-Buffer Producer-Consumer


Multi-Buffers, Mult-Producer-Consumers


The Dining Philosophers


Asymmetric Solutions

semaphore forks[0:4] = ([5] 1)

process philosopher [ i = 0 to 4 ]
  while true
      think
    forks[(i + ((i + 1) % 2)) % 5].lock()
    forks[i + (i % 2)].lock()
      eat
    forks[(i + ((i + 1) % 2)) % 5].unlock()
    forks[i + (i % 2)].unlock()


The Readers-Writers Problem


A Simple Readers-Writers Solution


A Concurrent Readers-Writers Solution


Safety and Liveness


Adding Fairness


An Incorrec


The Blown Invariant


An Unfair Solution

mtx.lock()        // reader entry
if nw + dw > 0 
  dr++
  mtx.unlock()
  r_go.wait()     { there are nr cs readers }
  mtx.lock()
  if --dr > 0     { there are nr cs readers }
    nr++
    r_go.signal() { there are nr cs readers }
else
  nr++
mtx.unlock


mtx.lock()        // writer exit
nw--
if dr > 0         { there are nr cs readers }
  nr++
  r_go.signal()   { there are nr cs readers }

else if dw > 0    { there are nw cs readers }
  nw++
  w_go.signal()   { there are nw cs readers }
mtx.unlock()


The Thief in the Night

mutex hand-off

the thief in the night


Passing the Baton


The Baton Passing Mechansim


A Correct, Fair Readers-Writers Solution


Baton Passing and Mutual Exclusion


Resource Scheduling


General Resource Scheduling


Passing the Baton


Shortest-Job-Next Scheduling


Points to Remember


This page last modified on 27 June 2003.