Critical Section Problem With Algorithm Solution

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Dining-Philosophers Problem

5 4

1 3

2 s Shared data
q q

Bowl of rice (data set) Semaphore chopstick [5] initialized to 1


3.1

Operating System Concepts

Dining-Philosophers Problem (Cont.)


s The structure of Philosopher i:
While (true) { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think }
Operating System Concepts 3.2

Monitors
s A high-level abstraction that provides a convenient

and effective mechanism for process synchronization


s Only one process may be active within the monitor

at a time
monitor monitor-name { // shared variable declarations procedure P1 () { . } procedure Pn () {} Initialization code ( .) { } } }
Operating System Concepts 3.3

Schematic view of a Monitor

Operating System Concepts

3.4

Condition Variables
s condition x, y;
q Allows

programmers to write their own synchronization schemes () a process that invokes the operation is suspended. () resumes one of processes (if any) that invoked x.wait ()

s Two operations on a condition variable:


q x.wait

q x.signal

Operating System Concepts

3.5

Monitor with Condition Variables

Operating System Concepts

3.6

Solution to Dining Philosophers


monitor DP { enum { THINKING; HUNGRY, EATING) state [5] ; condition self [5]; 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 and right neighbors test((i + 4) % 5); test((i + 1) % 5); }

Operating System Concepts

3.7

Solution to Dining Philosophers (cont)


void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; } } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } }

Operating System Concepts

3.8

Solution to Dining Philosophers (cont)


s Each philosopher I invokes the operations

pickup() and putdown() in the following sequence: dp.pickup (i) EAT dp.putdown (i)

Operating System Concepts

3.9

Database Systems Concurrency Control


s Atomic Transactions s Log-based Recovery s Checkpoints s Concurrent Atomic Transactions s Serializability s Two-phase Locking Protocol s Timestamp-based Protocols
Operating System Concepts 3.10

You might also like