ece3055 computer architecture and operating systems lecture 12 process synchronization

56
1 ECE3055 ECE3055 Computer Architecture an Computer Architecture an d Operating Systems d Operating Systems Lecture 12 Process Synchronizati Lecture 12 Process Synchronizati on on Prof. Hsien-Hsin Sean Lee Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering School of Electrical and Computer Engineering Georgia Institute of Technology Georgia Institute of Technology

Upload: adonia

Post on 08-Feb-2016

63 views

Category:

Documents


0 download

DESCRIPTION

ECE3055 Computer Architecture and Operating Systems Lecture 12 Process Synchronization. Prof. Hsien-Hsin Sean Lee School of Electrical and Computer Engineering Georgia Institute of Technology. Process Synchronization. Background The Critical-Section Problem Synchronization Hardware - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

1

ECE3055 ECE3055 Computer Architecture and OComputer Architecture and Operating Systemsperating Systems

Lecture 12 Process SynchronizationLecture 12 Process Synchronization

Prof. Hsien-Hsin Sean LeeProf. Hsien-Hsin Sean LeeSchool of Electrical and Computer EngineeringSchool of Electrical and Computer Engineering

Georgia Institute of TechnologyGeorgia Institute of Technology

Page 2: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

2

Process SynchronizationProcess Synchronization

Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors

Page 3: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

3

BackgroundBackground

Concurrent access to shared data may result in data inconsistency

Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes

Shared-memory solution to bounded-buffer problem has a race conditionrace condition on the class data count.

Page 4: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

4

Race ConditionRace Condition

The ProducerProducer calls while (1) {

while (count == BUFFER_SIZE); // do nothing

// add an item to the buffer++count;buffer[in] = item;in = (in + 1) % BUFFER_SIZE;

}

Page 5: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

5

Race ConditionRace Condition

The ConsumerConsumer calls while (1) {

while (count == 0); // do nothing

// remove an item from the buffer--count;item = buffer[out];out = (out + 1) % BUFFER_SIZE;

}

Page 6: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

6

Race ConditionRace Condition

count++ could be implemented as register1 = count; register1 = register1 + 1; count = register1;

count-- could be implemented as register2 = count; register2 = register2 – 1; count = register2;

Consider this execution interleaving:S0: producer execute register1 = count {register1 = 5}S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}

Page 7: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

7

Critical Section ProblemCritical Section Problem

N processes are competing shared data Each process has a code segment, called critical section, in

which the shared data is accessed Goal: when one process is executing in its critical section, no

other process can execute in its critical section. Each process looks like this

while (true) {entry section

critical sectioncritical section exit section

non critical section }

Page 8: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

8

Solution to Critical-Section ProblemSolution to Critical-Section Problem

1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections

2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely

3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

Page 9: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

9

Two-task SolutionTwo-task Solution

Two tasks, T0 and T1 (Ti and Tj) Three solutions presented. All implement this

MutualExclusion interface: public interface MutualExclusion { public static final int TURN0 = 0; public static final int TURN1 = 1; public abstract void enteringCriticalSection(int turn); public asbtract void leavingCriticalSection(int turn); }

Page 10: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

10

Worker ThreadWorker Threadpublic class Worker implements Runnable{ private String name;private int id;private MutualExclusion mutex;

public Worker(String name, int id, MutualExclusion mutex) { this.name = name;this.id = id;this.mutex = mutex;}public void run() { while (true) { mutex.enteringCriticalSection(id);MutualExclusionUtilities.criticalSection(name);mutex.leavingCriticalSection(id);MutualExclusionUtilities.nonCriticalSection(name);}}}

Page 11: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

11

Algorithm Factory classAlgorithm Factory class

Used to create two threads and to test each algorithmpublic class AlgorithmFactory{

public static void main(String args[]) {MutualExclusion alg = new Algorithm_1();Thread first = new Thread( new Worker("Worker 0", 0, alg));Thread second = new Thread(new Worker("Worker 1", 1, alg));first.start();second.start();

}}

Page 12: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

12

Algorithm 1 (Incomplete)Algorithm 1 (Incomplete)

public class Algorithm_1 implements MutualExclusion{ private volatile int turn;public Algorithm_1() { turn = TURN0;}public void enteringCriticalSection(int t) { while (turn != t)Thread.yield();}public void leavingCriticalSection(int t) { turn = 1 - t;}}

Page 13: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

13

Algorithm 1 (Incomplete)Algorithm 1 (Incomplete)

Threads share a common integer variable turn If turn==i, thread i is allowed to execute Does not satisfy progress requirement

Why? It requires strict alternation of threads in the execution

of the critical section For example, If turn == 0 (at the beginning, or after T1

and exit from critical section) T1 will NOT be able to enter the critical section till T0 enters and exits from critical section again

One thread could be indefinitely postponed

Page 14: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

14

Algorithm 2 (Incomplete)Algorithm 2 (Incomplete)public class Algorithm_2 implements MutualExclusion{ private volatile boolean flag0, flag1;public Algorithm 2() {flag0 = false; flag1 = false;}public void enteringCriticalSection(int t) {if (t == 0) {flag0 = true;while(flag1 == true)Thread.yield();}else { flag1 = true;while (flag0 == true)Thread.yield();}} // Continued On Right Hand Side

public void leavingCriticalSection(int t) { if (t == 0) flag0 = false;

else flag1 = false;

}}

Page 15: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

15

Algorithm 2 (Incomplete)Algorithm 2 (Incomplete)

Add more state information Boolean flags to indicate thread’s interest in

entering critical section Progress requirement still not met

Why? How about swap the while loop and the

flag setting?

Page 16: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

16

Algorithm 3Algorithm 3

Combine ideas from 1 and 2 Does it meet critical section requirements?

Page 17: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

17

Algorithm 3 (Peterson’s)Algorithm 3 (Peterson’s)

public class Algorithm_3 implements MutualExclusion{

private volatile boolean flag0;private volatile boolean flag1;private volatile int turn;public Algorithm_3() {

flag0 = false;flag1 = false;turn = TURN_0;

}// Continued on Next Slide

Page 18: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

18

Algorithm 3 - enteringCriticalSectionAlgorithm 3 - enteringCriticalSection

public void enteringCriticalSection(int t) {int other = 1 - t;turn = other;if (t == 0) { flag0 = true;while(flag1 == true && turn == other)Thread.yield();}else { flag1 = true;while (flag0 == true && turn == other)Thread.yield();}}

// Continued on Next Slide

Page 19: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

19

Algorithm 3 – leavingingCriticalSection()Algorithm 3 – leavingingCriticalSection()

public void leavingCriticalSection(int t) { if (t == 0)

flag0 = false; else

flag1 = false; }}

Page 20: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

20

Algorithm 3 - enteringCriticalSectionAlgorithm 3 - enteringCriticalSection

int other = 1 - t;turn = other = 1;if (t == 0) { flag0 = true; while(flag1 == true &&

turn == other)Thread.yield();

}else { flag1 = true; while (flag0 == true &&

turn == other)Thread.yield();

}

①①

②②③③

④④

t = 0int other = 1 - t;turn = other = 0;if (t == 0) { flag0 = true; while(flag1 == true &&

turn == other)Thread.yield();

}else { flag1 = true; while (flag0 == true &&

turn == other)Thread.yield();

}

t = 1①①②②③③④④

①①③③ ②②④④

①①③③ ④④②②

③③④④①①②②

③③ ①①②②④④

③③ ①①④④②②

Possibleordering

Cannot be true simultaneously

Page 21: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

21

Bakery Algorithm Bakery Algorithm

Generalization for n processes

Each process has an id. Ids are ordered

Before entering the critical section, a process receives a number. The holder of the smallest number enters its critical section.

Tie breaker is done using the process id: if processes Pi and Pj hold the same number, if i<j, then Pi enters first.

Page 22: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

22

Bakery AlgorithmBakery Algorithm

boolean choosing[n]; // shared int number[n]; // shared choosing[i] = true; number[i] = max(number[0], number[1], … , number[n-1]) + 1; choosing[i] = false; while (true ) { for (j=0, j<n-1, j++) { // Note that “i” is itself, “j” are other threadswhile (choosing[j]) ; // do nothing while (number[j] != 0 && (number[j], j) < (number[i], i)) // number[i]==0 means job done ; // do nothing } // end for critical section number[i] = 0; non-critical section }

Page 23: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

23

Synchronization HardwareSynchronization Hardware

Many systems provide hardware support for critical section code

Uniprocessors – could disable interrupts Currently running code would execute without preemp

tion Generally too inefficient on multiprocessor systems

Operating systems using this not broadly scalable Modern machines provide special atomic hardw

are instructionsAtomic = non-interruptable

Either test memory word and set value Or swap contents of two memory words

Page 24: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

24

Data Structure for Hardware SolutionsData Structure for Hardware Solutions

public class HardwareData{ private boolean data;public HardwareData(boolean data) { this.data = data;}public boolean get() { return data;}public void set(boolean data) { this.data = data;}// Continued on Next Slide

Page 25: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

25

Data Structure for Hardware Solutions Data Structure for Hardware Solutions

public boolean getAndSet(boolean data) { boolean oldValue = this.get();this.set(data);return oldValue;

}public void swap(HardwareData other) {

boolean temp = this.get();this.set(other.get());other.set(temp);

}}

Atomic instruction

Atomic instruction(read-modify-write)

Page 26: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

26

Thread Using get-and-set LockThread Using get-and-set Lock

// lock is shared by all threadsHardwareData lock = new HardwareData(false);while (true) {

while (lock.getAndSet(true))Thread.yield();

criticalSection();lock.set(false);nonCriticalSection();

}

Page 27: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

27

Thread Using swap InstructionThread Using swap Instruction

// lock is shared by all threadsHardwareData lock = new HardwareData(false);// each thread has a local copy of keyHardwareData key = new HardwareData(true);

while (true) { key.set(true);do {

lock.swap(key);}while (key.get() == true);criticalSection();lock.set(false);nonCriticalSection();

}

0

1 1 1 1

Page 28: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

28

Thread Using swap InstructionThread Using swap Instruction

// lock is shared by all threadsHardwareData lock = new HardwareData(false);// each thread has a local copy of keyHardwareData key = new HardwareData(true);

while (true) { key.set(true);do {

lock.swap(key);}while (key.get() == true);criticalSection();lock.set(false);nonCriticalSection();

}

1

1 0 1 1

Page 29: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

29

SemaphoreSemaphore

Synchronization tool that does not require busy waiting (spin lock)

Semaphore S – integer variable Two standard operations modify S: acquire() and release()

Originally called P() and V() Less complicated Can only be accessed via two indivisible (atomic) operations

acquire(S) { while S <= 0

; // no-opS--;

}release(S) {

S++;}

Page 30: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

30

Semaphore as General Synchronization ToolSemaphore as General Synchronization Tool Counting semaphore – integer value can range over an unre

stricted domain Binary semaphore – integer value can range only between 0

and 1; can be simpler to implement Also known as mutex locks

Can implement a counting semaphore S as a binary semaphore

Provides mutual exclusion

Semaphore S; // initialized to 1

acquire(S);criticalSection();release(S);

Page 31: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

31

Synchronization using Semaphores Synchronization using Semaphores Implementation - WorkerImplementation - Worker

public class Worker implements Runnable{

private Semaphore sem;private String name;public Worker(Semaphore sem, String name) { this.sem = sem;this.name = name;}public void run() { while (true) { sem.acquire();MutualExclusionUtilities.criticalSection(name);sem.release();MutualExclusionUtilities.nonCriticalSection(name);}}

}

Page 32: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

32

Synchronization using Semaphores Synchronization using Semaphores Implementation – Semaphore FactoryImplementation – Semaphore Factory

public class SemaphoreFactory{

public static void main(String args[]) { Semaphore sem = new Semaphore(1);Thread[] bees = new Thread[5];for (int i = 0; i < 5; i++)

bees[i] = new Thread(new Worker(sem, "Worker " + (new Integer(i)).toString

() ));for (int i = 0; i < 5; i++)

bees[i].start();}

}

Page 33: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

33

Semaphore ImplementationSemaphore Implementation

acquire(S){ value--;if (value < 0) { add this process to listblock;}

}release(S){

value++;if (value <= 0) { remove a process P from listwakeup(P);}

}

Page 34: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

34

Semaphore ImplementationSemaphore Implementation

Must guarantee that no two processes can execute acquire() and release() on the same semaphore at the same time

Thus implementation becomes the critical section problem Could now have busy waiting in critical section

implementation But implementation code is short Little busy waiting if critical section rarely occupied

Applications may spend lots of time in critical sections Performance issues addressed throughout this lecture

Page 35: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

35

Deadlock and StarvationDeadlock and Starvation

Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes

Let S and Q be two semaphores initialized to 1P0 P1

acquire(S); acquire(Q); acquire(Q); acquire(S);. .. .. . release(S); release(Q); release(Q); release(S);

Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

Page 36: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

36

Classical Problems of SynchronizationClassical Problems of Synchronization

Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem

Page 37: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

37

Bounded-Buffer ProblemBounded-Buffer Problem

public class BoundedBuffer implements Buffer{

private static final int BUFFER SIZE = 5;private Object[] buffer;private int in, out;private Semaphore mutex;private Semaphore empty;private Semaphore full;

// Continued on next Slide

Page 38: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

38

Bounded Buffer ConstructorBounded Buffer Constructor

public BoundedBuffer() { // buffer is initially emptyin = 0;out = 0;buffer = new Object[BUFFER SIZE];mutex = new Semaphore(1);empty = new Semaphore(BUFFER SIZE);full = new Semaphore(0);

}public void insert(Object item) { /* next slides */ }

public Object remove() { /* next slides */ }}

Page 39: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

39

Bounded Buffer Problem: insert() MethodBounded Buffer Problem: insert() Method

public void insert(Object item) { empty.acquire();mutex.acquire();// add an item to the bufferbuffer[in] = item;in = (in + 1) % BUFFER SIZE;mutex.release();full.release();

}

Page 40: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

40

Bounded Buffer Problem: remove() MethodBounded Buffer Problem: remove() Method

public Object remove() { full.acquire();mutex.acquire();// remove an item from the bufferObject item = buffer[out];out = (out + 1) % BUFFER SIZE;mutex.release();empty.release();return item;

}

Page 41: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

41

Bounded Buffer Problem: ProducerBounded Buffer Problem: Producer

import java.util.Date;public class Producer implements Runnable{

private Buffer buffer;public Producer(Buffer buffer) { this.buffer = buffer;}public void run() { Date message;while (true) { // nap for awhileSleepUtilities.nap();// produce an item & enter it into the buffermessage = new Date();buffer.insert(message);}}

}

Page 42: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

42

Bounded Buffer Problem: ConsumerBounded Buffer Problem: Consumer

import java.util.Date;public class Consumer implements Runnable{

private Buffer buffer;public Consumer(Buffer buffer) {

this.buffer = buffer;}public void run() {

Date message;while (true) {

// nap for awhileSleepUtilities.nap();// consume an item from the buffermessage = (Date)buffer.remove();

}}

}

Page 43: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

43

Bounded Buffer Problem: FactoryBounded Buffer Problem: Factory

public class Factory{

public static void main(String args[]) { Buffer buffer = new BoundedBuffer();// now create the producer and consumer threadsThread producer = new Thread(new Producer(buffer));Thread consumer = new Thread(new Consumer(buffe

r));producer.start();consumer.start();

}}

Page 44: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

44

Readers-Writers Problem: ReaderReaders-Writers Problem: Reader

public class Reader implements Runnable{

private RWLock db;public Reader(RWLock db) { this.db = db;}public void run() { while (true) { // nap for awhiledb.acquireReadLock();

// you now have access to read from the database// read from the database

db.releaseReadLock();}}

}

Page 45: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

45

Readers-Writers Problem: WriterReaders-Writers Problem: Writer

public class Writer implements Runnable{

private RWLock db;public Writer(RWLock db) {

this.db = db;}public void run() {

while (true) { db.acquireWriteLock();// you have access to write to the database

// write to the database

db.releaseWriteLock();}

}}

Page 46: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

46

Readers-Writers Problem: InterfaceReaders-Writers Problem: Interface

public interface RWLock{

public abstract void acquireReadLock();public abstract void acquireWriteLock();public abstract void releaseReadLock();public abstract void releaseWriteLock();

}

Page 47: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

47

Readers-Writers Problem: DatabaseReaders-Writers Problem: Database

public class Database implements RWLock{

private int readerCount;private Semaphore mutex;private Semaphore db;public Database() { readerCount = 0;mutex = new Semaphore(1);db = new Semaphore(1);}public int acquireReadLock() { /* next slides */ }public int releaseReadLock() {/* next slides */ }public void acquireWriteLock() {/* next slides */ }public void releaseWriteLock() {/* next slides */ }

}

Page 48: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

48

Readers-Writers Problem: Methods called by readersReaders-Writers Problem: Methods called by readers

public void acquireReadLock() { mutex.acquire();++readerCount;// if I am the first reader tell all others// that the database is being readif (readerCount == 1)

db.acquire();mutex.release();

}public void releaseReadLock() {

mutex.acquire();--readerCount;// if I am the last reader tell all others// that the database is no longer being readif (readerCount == 0)

db.release();mutex.release();

}

Page 49: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

49

Readers-Writers Problem: Methods called by writersReaders-Writers Problem: Methods called by writers

public void acquireWriteLock() { db.acquire();

}

public void releaseWriteLock() { db.release();

}

Page 50: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

50

Dining-Philosophers ProblemDining-Philosophers Problem

Shared data Semaphore chopStick[] = new Semaphor

e[5];

Page 51: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

51

Dining-Philosophers Problem (Cont.)Dining-Philosophers Problem (Cont.)

Philosopher i:while (true) {

// get left chopstickchopStick[i].acquire();// get right chopstickchopStick[(i + 1) % 5].acquire();eating();// return left chopstickchopStick[i].release();// return right chopstickchopStick[(i + 1) % 5].release();thinking();

}

Page 52: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

52

MonitorsMonitors

A monitor is a high-level abstraction that provides thread safety

Only one thread may be active within the monitor at a time

A monitor contains Initialization (constructor) Private data (only visible by monitor itself) Monitor procedure (the way to invoke a monitor) Monitor entry queue (contains all threads that called monitor

but have not been granted permissions)

Page 53: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

53

Condition VariablesCondition Variables

(private) condition x, y;

Like an event queue

Only 2 operations can be applied to a condition variable Wait Signal (called notify in Java)

A thread that invokes x.wait is suspended until another thread invokes x.signal

Page 54: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

54

Monitor with condition variablesMonitor with condition variables

Condition variable X

One Entry queue per monitor

Page 55: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

55

Condition Variable Solution to Dining Condition Variable Solution to Dining PhilosophersPhilosophers

monitor DiningPhilosophers { int[] state = new int[5];static final int THINKING = 0;static final int HUNGRY = 1;static final int EATING = 2;condition[] self = new condition[5];public diningPhilosophers {

for (int i = 0; i < 5; i++)state[i] = THINKING;

}public entry pickUp(int i) {

state[i] = HUNGRY;test(i);if (state[i] != EATING)

self[i].wait;}// Continued on Next Slide

Page 56: ECE3055  Computer Architecture and Operating Systems Lecture 12 Process Synchronization

56

Solution to Dining Philosophers (cont)Solution to Dining Philosophers (cont)

public entry putDown(int i) { state[i] = THINKING;// test left and right neighborstest((i + 4) % 5);test((i + 1) % 5);

}

private test(int i) { if ( (state[(i + 4) % 5] != EATING) &&

(state[i] == HUNGRY) &&(state[(i + 1) % 5] != EATING) ) {

state[i] = EATING;self[i].signal;

}}