Classic Coordination Problems Flashcards

1
Q

User Defined Box Sizing

A

– One process reads a number from keyboard and places inside an object of class BoxDimension
– Separate process extracts the number from BoxDimension object, and draws a box of corresponding size

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Coordination Problem

A
  • Getter must extract only dimensions that Putter has placed
  • Getter must not extract any dimension more than once
  • Putter must not place a new dimension until the Getter has extracted the one the
    Putter placed earlier
  • Putter=Producer; put=produce
  • Getter=Consumer; extract=consume
  • Placing values in a unit buffer (buffer of size 1)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Producer-Consumer (Bounded Buffer) Problem

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Producer-Consumer (Bounded Buffer) Problem
Outline of solution

A
  • Uses two semaphores produce and consume
  • How to ensure that consumer does not consume before producer produces?
    • What should the initial values of the semaphores be?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Java Implementation of Producer-Consumer Unit Buffer

A

class Consumer implements Runnable {
Semaphore produce, consume;
StringBuffer buf;
public Consumer(Semaphore produce, Semaphore consume, StringBuffer buf) {this.produce = produce;
this.consume = consume;
this.buf = buf;
}
public void run() {
while(true) {
consume.acquire();
System.out.println(buf);
produce.release();
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Producer-Consumer in General (Several producers, consumers, and bounded length buffers)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Conditions

A
  • A Consumer must extract only values that a Producer has placed
  • No value should be extracted more than once
  • A Producer may place a value only in free buffer locations
    • Initially all buffer locations are free
    • Any location that contains a value that has been extracted is free
    • No other location is free
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Generalized Producer-Consumer in Java

A

class Consumer implements Runnable {
Semaphore produce,Semaphore consume;
Buffer buf;
public Consumer(Semaphore produce, Semaphore consume, Buffer buf){this.produce = produce; this.consume = consume; this.buf = buf;}
public void run() {
while(true) {
consume.acquire();
System.out.println(“Got:” + buf.get());
produce.release();
}
}
}

class Buffer
String[] values; boolean[] availableWrite;
public Buffer(int bufSize)
values = new String[bufSize];
availableWrite = new boolean[bufSize];
for(int i = 0; i<bufSize;i++)
availableWrite[i] = true;
synchronized void put(String s)
for(int i = 0; i < availableWrite.length; i++)
if(availableWrite[i] == true)
values[i] = s;
availableWrite[i] = false;
break;
synchronized String get()
for(int i = 0; i < availableWrite.length; i++)
if(availableWrite[i] == false)
availableWrite[i] = true;
return values[i];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Readers-Writers Problem

A
  • A buffer (“file”) that several threads are accessing simultaneously
    • Buffer holds a single value (the file)
    • Writers write values to the buffer (they may also read the buffer)
    • Readers may only read from the buffer
  • Ensure that any buffer value read by a thread is a value written by some writer!
    • Unlike Producer-Consumer, a value may be overwritten by a Writer without ever having been read by a Reader
    • Unlike Producer-Consumer, the same value can be read by many Readers and more than once by a Reader
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Readers-Writers Problems
Constraints that Follow from the Requirement

A
  • Writer must have exclusive access to the buffer
  • Any number of readers may be concurrent
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Readers-Writers Problems
Solution Outline

A
  • Two semaphores, write and read, initialized to 1
  • A count of the number of Readers numReaders, initialized to 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Readers-Writers
Firing up the Threads

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Writer run() function

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Reader run() function

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code does not meet Requirement as we stated it

A
  1. (As stated earlier) Ensure that any buffer value read by a thread is a value written by some writer!
  2. (Should Be) Ensure that any buffer value read by a thread is either the initial value or a value written by some writer!
  • 2 is normally requirement for Readers-Writers
    • Illustrates the subtlety of implementing multithreaded programs
  • How would you modify code so that 1 is met?
How well did you know this?
1
Not at all
2
3
4
5
Perfectly