dining philosophers (1)

20
Dining Philosophers (1) Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock

Upload: gyda

Post on 25-Feb-2016

56 views

Category:

Documents


3 download

DESCRIPTION

Dining Philosophers (1). Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock . Dining Philosophers. A non solution to the dining philosophers problem. semaphore Forks[N] = {1,1,1,1,1} ; void take_fork(int i) { wait (Forks[i]) ; } - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Dining Philosophers (1)

Dining Philosophers (1)

Philosophers eat/think Eating needs 2 forks Pick one fork at a time How to prevent deadlock

Page 2: Dining Philosophers (1)

Dining Philosophers

A nonsolution to the dining philosophers problem

Page 3: Dining Philosophers (1)

semaphore Forks[N] = {1,1,1,1,1} ;void take_fork(int i)

{ wait (Forks[i]) ; }

void put_fork(int i){ signal(Forks[i]) ;}

Problems?

Page 4: Dining Philosophers (1)

Approach 2:Semaphore Forks[N] = {1,1,1,1,1} ;Semaphore mutex = 1 ;

while (1){ think(); wait(mutex) ; take_fork(i) ; take_fork((i+1) % N) ; eat() ; put_fork(i) ; put_fork((i+1) % N) ; signal(mutex) ;}

Will this work?

Page 5: Dining Philosophers (1)

Dining Philosophers

Solution to dining philosophers problem (part 1)

Page 6: Dining Philosophers (1)

Dining Philosophers

Solution to dining philosophers problem (part 2)

Page 7: Dining Philosophers (1)

NULL

Mutex.queue

T T T T T

0 0 0 0 0

State Array

S ArrayMutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

Page 8: Dining Philosophers (1)

NULL

Mutex.queue

E T T T T

2 1 1 1 1

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

1 0 0 0 0

Page 9: Dining Philosophers (1)

NULL

Mutex.queue

E T T T T

1 0 0 0 0

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

up(mutex) ;

down(S[0]) ;

Page 10: Dining Philosophers (1)

NULL

Mutex.queue

E T T T T

0 0 0 0 0

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

up(mutex) ;

down(S[0]) ; // falls through to //critical section

Preempted

Page 11: Dining Philosophers (1)

NULL

Mutex.queue

E T T T T

0 0 0 0 0

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

up(mutex) ;

down(S[0]) ; // falls through to //critical section

Preempted

P2: take_forks(2) ;

down(mutex) ;

test(2) ;

Page 12: Dining Philosophers (1)

NULL

Mutex.queue

E T E T T

1 1 2 1 1

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

Preempted

0 0 1 0 0

P2: take_forks(2) ;

down(mutex) ;

test(2) ;

Page 13: Dining Philosophers (1)

NULL

Mutex.queue

E T E T T

1 1 2 1 1

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

Preempted

0 0 0 0 0

P2: take_forks(2) ;

down(mutex) ;

test(2) ;

up(mutex) ;

down(S[2]) ; //Falls through to critical section

Page 14: Dining Philosophers (1)

NULL

Mutex.queue

E T E T T

1 1 1 1 1

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

Preempted

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

0 0 0 0 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

Page 15: Dining Philosophers (1)

NULL

Mutex.queue

E T E H T

1 1 1 0 1

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

P3

S[3].queue

0 0 0 -1 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

Page 16: Dining Philosophers (1)

NULL

Mutex.queue

E T E H T

1 1 1 0 1

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

P3

S[3].queue

0 0 0 -1 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

P2: put_forks(2) ; down(mutex) ;

State[2] = Thinking ;

test(1) ;

Page 17: Dining Philosophers (1)

NULL

Mutex.queue

E T T H T

1 1 1 0 1

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

P3

S[3].queue

0 0 0 -1 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

P2: put_forks(2) ; down(mutex) ;

State[2] = Thinking ;

test(1) ;

Page 18: Dining Philosophers (1)

NULL

Mutex.queue

E T T H T

1 1 1 0 1

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

P3

S[3].queue

0 0 0 -1 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

P2: put_forks(2) ; down(mutex) ;

State[2] = Thinking ;

test(1) ;

test(3) ;

Page 19: Dining Philosophers (1)

NULL

Mutex.queue

E T T E T

1 1 1 0 1

State Array

S Array

Mutex = 0

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

0 0 0 0 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

P2: put_forks(2) ; down(mutex) ;

State[2] = Thinking ;

test(1) ;

test(3) ; //performs a signal on S[3]

P3Ready Queue

Page 20: Dining Philosophers (1)

NULL

Mutex.queue

E T T E T

1 1 1 0 1

State Array

S Array

Mutex = 1

P0: take_forks(0)

down(mutex) ;

test(0) ;

down(S[0]) ;

P3: take_forks(3) ;

down(mutex) ;

test(3) ;

up(mutex) ;

down(S[3]) ;

0 0 0 0 0

P2: take_forks(2) ; down(mutex) ; test(2) ; up(mutex) ; down(S[2]) ;

eat() !!!

P2: put_forks(2) ; down(mutex) ;

State[2] = Thinking ;

test(1) ;

test(3) ; //performs a signal on S[3]

up(mutex) ;

P3Ready Queue