syncProb - Dante Cannarozzi Washington University...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Dante Cannarozzi Washington University [email protected] www.cse.wustl.edu/~djc2 Classic Synchronization Problems (CSE 422S) 2-Ken Wong, Oct 2008 Producer-Consumer Problem (1) Shared Data int buffer[N]; // N integer buffers int nxtIn = 0; // index to next input slot int nxtOut = 0; // index to next output slot Semaphore freeSlot = N, // # resources notEmpty = 0, // signal event enter = 1; // protect critical section Consumer Producer nxtOut nxtIn N-1 . . . 3-Ken Wong, Oct 2008 Producer-Consumer Problem (2) Process Producer { ... produce newItem ... Wait(freeSlot); Wait(enter); buffer[nxtIn] = newItem; nxtIn = (nxtIn+1) mod N; Signal(enter); Signal(notEmpty); } Process Consumer { Wait(notEmpty); Wait(enter); outItem = buffer[nxtOut]; nxtOut = (nxtOut+1) mod N; Signal(enter); Signal(freeSlot); ... consume outItem ... } int buffer[N]; int nxtIn = 0; int nxtOut = 0; Semaphore enter = 1, // protect CS freeSlot = N, // # resources notEmpty = 0;// signal event 4-Ken Wong, Oct 2008 The Dining Philosopher Problem Philosopher States » Thinking Has no chopsticks » Hungry Wants both chopsticks » Eating Has both chopsticks Example » 4 philosophers 0, 1, 2, 3 » 4 chopsticks 3 2 1 3 2 1 5-Ken Wong, Oct 2008 Bad Dining Philosopher Algorithm Semaphore stick[4] = {1, 1, 1, 1}; Process philosopher(i) { do { Wait (R(i)); Wait(L(i)); . . . Eat . . . Signal (R(i)); Signal (L(i)); . . . Think . . . } until (DONE); where R(i) := stick[i] L(i) := stick[(i+1) mod 4] Algorithm deadlocks » How? Why? Why do we care? Fix: 2-philosopher case » Increase # resources » Larger atomic operation » Resource order (0<2<1<3) Extend to N philosophers? What about starvation ? » Only 1 philosopher starves? “defined to be” Resources 6-Ken Wong, Oct 2008 An Asymmetric Algorithm Semaphore stick[4] = {1, 1, 1, 1}; Process philosopher(i) { do { Wait (A(i)); Wait(B(i)); . . . Eat . . . Signal (B(i)); Signal (A(i)); . . . Think . . . } until (DONE); where A(i) := stick[i], if even(i) A(i) := stick[(i+1) mod 4], otherwise B(i) := stick[(i+1) mod 4], if even(i) B(i) := stick[i], otherwise i.e., A(i) := R(i), if even(i) L(i), otherwise B(i) := L(i), if even(i) R(i), otherwise 3 2 1 3 2 1 7-Ken Wong, Oct 2008 Dining Philosopher Monitor (1) Monitor diningPhilosophers { State state[4]; // HUNGRY, THINKING, EATING Condition phil[4]; // condition variable pickup (i) { state[i] = HUNGRY; test(i); phil[i].Wait; } putdown (i) { state[i] = THINKING; test((i+3) mod 4); // try to wake up right neighbor test((i+1) mod 4); // try to wake up left neighbor } 8-Ken Wong, Oct 2008 Dining Philosopher Monitor (2) test(k) { // wakeup philosopher k if possible if ((state[(k+3) mod 4] != EATING) and (state[(k+1) mod 4] != EATING) and (state[k] == HUNGRY)) { // ASSERT: 1) Right and Left not eating; and // 2) I’m hungry state[k] = EATING; phil[k].Signal; } } begin { // initialization for (i=0; i<4; i++) state[i] = THINKING; } } 9-Ken Wong, Oct 2008 Dining Philosopher Monitor (3)...
View Full Document

This document was uploaded on 11/13/2009.

Page1 / 38

syncProb - Dante Cannarozzi Washington University...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online