Lecture 07 Low Level mechanisms for process synchronization - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Lecture 07 Low Level mechanisms for process synchronization

Description:

if a philosopher can not get the forks, he/she blocked himself/herself */ Fall 2000 ... test(LEFT); /* try to get two forks for his/her left neighbor ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 16
Provided by: eam80
Category:

less

Transcript and Presenter's Notes

Title: Lecture 07 Low Level mechanisms for process synchronization


1
Lecture 07 Low Level mechanisms for process
synchronization
  • Semaphores
  • The dinning philosophers problem
  • Readers and Writers problem
  • Limitations of Semaphores

2
The dining philosophers problem
3
First attempt at a solution
  • var fork array0..4 of semaphore / all
    elements 1 /
  • procedure philosopher(iinteger)
  • repeat
  • wait(forki) /
    takes left fork /
  • wait(forki1 mod 5) / takes right
    fork /
  • lteatgt
  • signal (forki) /
    releases left fork /
  • signal (forki1 mod 5) / releases
    right fork /
  • ltthinkgt
  • until false

4
Definitions
  • define N 5
  • define LEFTN (i-1)N
  • define RIGHTN (i1)N
  • define THINKING 0
  • define HUNGRY 1
  • define EATING 2
  • / Number of philosophers /
  • / Left neighbor of philosopher i /
  • /Right neighbor of philosopher i/
  • / State /
  • / State /
  • / State /

5
Semaphores
  • var
  • state integer arrayN
  • mutex semaphore 1
  • s semaphore arrayN
  • /state of each philosopher/
  • / examine the state of only one philosopher each
    time
  • /
  • / if a philosopher can not get the forks, he/she
    blocked himself/herself /

6
Philosopher Actions
  • procedure philosopher(i integer)
  • while (true)
  • statei THINKING
  • get_forks(i)
  • statei EATING
  • release_forks(i)

7
procedure get_forks(i integer)
  • wait(mutex) / Nobody else can access the state
    array /
  • statei HUNGRY / He/she wants the
    resources/
  • test(i) / try
    to get two forks /
  • signal(mutex)
  • wait(si) / it blocks if does not
    get the forks /

8
procedure release_forks(i integer)
  • wait(mutex) / Nobody else can access
    the state array /
  • statei THINKING / He/she does not need
    the resources /
  • test(LEFT) / try to get two forks for
    his/her left neighbor /
  • test(RIGHT)/ try to get two forks for
    his/her right neighbor / signal(mutex)

9
procedure test(i integer)
  • if (statei HUNGRY and
  • stateLEFT ! EATING and
  • stateRIGHT ! EATING )
  • statei EATING
  • signal(si)

10
Readers and Writers
  • A data object is shared among several concurrent
    processes
  • The readers may want only to read the content of
    the shared object
  • The writers may want to update the shared object

11
Ensure mutual exclusion to the shared object
  • var access_db semaphore 1
  • procedure reader
  • procedure writer
  • wait(access_db)
  • ltwriting is performedgt
  • signal(access_db)
  • ...

12
Variables
  • var
  • access_counter semaphore 1
  • access_db semaphore 1
  • n_readers integer 0

13
procedure reader
  • while(true)
  • wait(access_counter)
  • n_readers n_readers 1
  • if (n_readers 1) then
  • wait(access_db)
  • signal(access_counter)
  • wait(access_counter)
  • signal(access_counter)

14
procedure reader
  • while(true)
  • wait(access_counter)
  • n_readers n_readers 1
  • / first reader ? /
  • if (n_readers 1) then
  • wait(access_db)
  • signal(access_counter)
  • lt read data base gt
  • wait(access_counter)
  • n_readers n_readers - 1
  • / last reader ? /
  • if (n_readers 0) then
  • signal(access_db)
  • signal(access_counter)
  • lt use data gt

15
Limitations of Semaphores
  • It is easy to make mistakes in semaphore
    programming.
  • It is not easy to remember which semaphore is
    associated with which data structure.
  • The operations do not allow a test for busy
    without a commitment to blocking.
  • An alternative to waiting on the semaphore might
    be preferable.
  • The time for which a process is blocked on a
    semaphore is not limited.
  • A process blocks indefinitely until released by a
    signal
Write a Comment
User Comments (0)
About PowerShow.com