Race Condition Shared Data: 5 6 4 1 8 5 6 20 9 ? InterProcess Communication tail A[] Enqueue(): A[tail] = 20; A[tail] = 9; tail++; tail++; process switch Process A Process B Critical Regions Goals • No two processes (threads) can be in their critical region at the same time A enters A leaves • No assumptions about # of CPUs or their critical region critical region speed Process A B leaves B enters B tries to enter critical region • No process outside of its critical region may critical region critical region block another process Process B B blocked B blocked Time • No process should have to wait forever to enter its critical region Strict Alternation Busy Waiting #define FALSE 0 #define TRUE 1 #define N 2 // # of processes int interested[N]; // Set to 1 if process j is interested int last_request; // Who requested entry last? Process A Process B void enter_region(int process) { while (TRUE) { while (TRUE) { int other = 1 ‐ process; // # of the other process while (turn != 0) while (turn != 1) interested[process] = TRUE; // show interest ; /* loop */ ; /* loop */ last_request = process; // Set it to my turn while (interested[other]==TRUE && last_request == process ) critical_region (); critical_region (); ; // Wait while the other process runs turn = 1; turn = 0; } noncritical_region (); noncritical_region (); } } void leave_region (int process) { interested[process] = FALSE; // I’m no longer interested }
Hardware Support Producer/Consumer Problem Shared variables Atomic statements: int lock = 0; const int n; counter += 1; counter += 1; typedef … Item; counter ‐ = 1; counter ‐ = 1; Item buffer[n]; Code for process P i Code for process P i int in = 0, out = 0, counter = 0; while (1) { while (1) { while (TestAndSet(lock)) while (Swap(lock,1) == 1) Producer Consumer ; ; Item pitm; Item citm; // critical section // critical section while (1) { while (1) { … if (counter == 0) lock = 0; lock = 0; produce an item into pitm sleep(); // remainder of code // remainder of code … citm = buffer[out]; } } if (counter == n) out = (out+1) % n; sleep(); counter ‐ = 1; buffer[in] = pitm; if (count == n ‐ 1) in = (in+1) % n; wakeup(producer); counter += 1; consume the item in citm if (counter==1) … wakeup(consumer); } } Semaphore with Blocking Producer/Consumer with Semaphores class Semaphore { const int n; int value; Semaphore empty(n),full(0),mutex(1); ProcessList pl; Item buffer[n]; void down () { value ‐ = 1; Producer Consumer if (value < 0) { // add this process to pl int in = 0; int out = 0; pl.enqueue(currentProcess); Item pitem; Item citem; Sleep(); while (1) { while (1) { } // produce an item full.down(); } // into pitem mutex.down(); empty.down(); citem = buffer[out]; void up () { Process P; mutex.down(); out = (out+1) % n; value += 1; buffer[in] = pitem; mutex.up(); if (value <= 0) { in = (in+1) % n; empty.up(); // remove a process P from pl mutex.up(); // consume item from P = pl.dequeue(); full.up(); // citem Wakeup(P); } } } } } Binary Semaphore Counting Semaphore Semaphore that only takes on the values 0 or 1
Binary Semaphores Shared variables Semaphore mutex; Mutex Code for process P i A simplified version of a Semaphore that can while (1) { only be locked or unlocked down(mutex); // critical section up(mutex); // remainder of code } Monitors class ProducerConsumer { private static final int n; Item buffer[] = new Item[n]; Locks and Condition Variables public synchronized Item consumer() { public synchronized void producer() { while (count == 0) { //produce an item into pItm try { while (count == n) { wait(); try { } wait(); catch (InterruptedException e) { } catch (InterruptedException e) { System.err.println("interrupted"); System.err.println("interrupted"); } } } } cItm = buffer[out]; buffer[in] = pItm; out = (out + 1) % n; in = (in + 1) % n; count ‐ =1; count+=1; if (count == n ‐ 1) { if (count == 1) { // wake up the producer // wake up the consumer notify(); notify(); } } return cItm; } } Barriers A A A A Message Passing B B B B C C C C D D D D Processes approaching B and D at All at Barrier releases barrier barrier barrier all processes
Dining Philosophers
Recommend
More recommend