The following are in reverse chronological order.
on a computer running Ubuntu. In the meantime, you can try them. The source code for these applets is available at http://elvis.rowan.edu/~hartley/ConcProgJava/Applets/index.html.appletviewer http://elvis.rowan.edu/~hartley/ConcProgJava/Applets/bubbleSort.html appletviewer http://elvis.rowan.edu/~hartley/ConcProgJava/Applets/diningPhilosophers.html appletviewer http://elvis.rowan.edu/~hartley/ConcProgJava/Applets/distributedDP.html appletviewer http://elvis.rowan.edu/~hartley/ConcProgJava/Applets/parallelQuickSort.html
A student checked out one of the reserve books, ``Modern Operating Systems,'' that is on reserve for your class. We have two copies on reserve. I was wondering if you could please ask the students in your class to return this copy so that it will be on reserve for other students.
If we had more time in the semester, a good next programming assignment would be to rework the block matrix multiplication program to be more like the quick sort queue (sometimes called a bag) of tasks: instead of starting a thread to calculate each block of the product matrix, create a queue of tasks, one for each block of the product matrix; then create a fixed number of threads equal to the number of cores being used; then have each thread take a task from the queue, fill in the block of the product matrix represented by the task, then take another task from the queue, ..., until all tasks are done.
prompt% time taskset -c 0 java QuickSort num range 1 prompt% time taskset -c 0-1 java QuickSort num range 2 prompt% time taskset -c 0-2 java QuickSort num range 3 prompt% time taskset -c 0-3 java QuickSort num range 4
0612 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
0727 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
0964 90: -10 women must go first at the beginning of the simulation, an artificial restriction
1926 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
1bxgi 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
2070 100: code and output look good
6967 0: nothing submitted
915916420 100: code and output look good
916012483 95: -5 not necessary to signal own gender
916024501 95: -5 not necessary to signal own gender
916025375 100: code and output look good
Chessmaster 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
J9137 00: code and output look good but turned in late
Keelah 0: nothing submitted
Shads 70: -30 need signalAll() instead of signal(); output clearly shows that when one gender leaves, not all of other waiting gender are signaled into bathroom
Torque 0: nothing submitted
WayneBrody 0: nothing submitted
ZnyvxNuzrq 100: code and output look good
thepizza 100: code and output look good
utley 100: code and output look good
class Bathroom {
...
private void manEnter() throws InterruptedException {
mutex.lock();
try {
while (numWomenIn > 0) { men.await(); }
numMenIn++;
} finally { mutex.unlock(); }
}
private void manExit() throws InterruptedException {
mutex.lock();
try {
numMenIn--;
if (numMenIn == 0 && mutex.hasWaiters(women)) women.signalAll();
} finally { mutex.unlock(); }
}
private void womanEnter() throws InterruptedException {
mutex.lock();
try {
while (numMenIn > 0) { women.await(); }
numWomenIn++;
} finally { mutex.unlock(); }
}
private void womanExit() throws InterruptedException {
mutex.lock();
try {
numWomenIn--;
if (numWomenIn == 0 && mutex.hasWaiters(men)) men.signalAll();
} finally { mutex.unlock(); }
}
...
}
shared: volatile int x = 0, y = 0;Now let's change the problem slightly.T0: { x = 3; y = 4; }
T1: { z = y; z = z + x; }
shared: volatile int w = 0;Whether volatile or not, the JVM memory model guarantees the other threads will see the updates to the single variable w done in the order shown. All threads see w first change to 3, then change to 4. 10 is not possible for the final value of z whether the variables are volatile or not.T0: { w = 3; w = 4; }
T1: { z = w; z = z + 2*w; }
class BufferItem {
private double value = 0;
private boolean occupied = false;
public synchronized double getValue() { return value; }
public synchronized boolean getOccupied() { return occupied; }
public synchronized void setValue(double v) { value = v; }
public synchronized void setOccupied(boolean o) { occupied = o; }
}
class BoundedBuffer {
public void deposit(double value) throws InterruptedException {
while (buffer[putIn].getOccupied()) { ...
...
buffer[putIn].setValue(value);
buffer[putIn].setOccupied(true);
...
public double fetch() throws InterruptedException {
while (!buffer[takeOut].getOccupied()) { ...
...
value = buffer[takeOut].getValue();
buffer[takeOut].setOccupied(false);
...
}
With a minute left in the class meeting, Jon said he ran the program
with five producers and one consumer (I think).
If you look at the beginning of the original busy-waiting bounded buffer,
the one using volatile, you will see the comment,
``Designed for a single producer thread and a single consumer thread.''
I think we discussed the reason for this in class when we looked
at the original version.
Does the same comment apply to Jon's synchronized version?
Why or why not?
The volatile modifier is used to remove visibility races, also called data races in the Goetz book the graduate students are reading. But volatile does not eliminate race conditions, such as the lost update. The way synchronized is used in the busy-waiting bounded buffer code above also ensures visibility but does not necessarily remove race conditions. Can you think of a race condition that can occur in the busy-waiting bounded buffer code above when there are five producers? Sometimes the term check then act or put if absent is used to describe the sequence of operations vulnerable to a race condition.
while (true) do {
println(age() reader x sleeping for m milliseconds);
Thread.sleep(random number of milliseconds);
println(age() reader x wants to read);
wantToReadFromDatabase();
}
The lifetime of a writer thread is similar
while (true) do {
println(age() writer x sleeping for m milliseconds);
Thread.sleep(random number of milliseconds);
println(age() writer x wants to read);
wantToWriteIntoDatabase();
}
Reading and writing in the database, once permission
is granted, are simulated like this.
readFromDatabase() {
println(age() reader x reading for m milliseconds);
Thread.sleep(random number of milliseconds);
println(age() reader x done reading);
}
...
writeIntoDatabase() {
println(age() writer x writing for m milliseconds);
Thread.sleep(random number of milliseconds);
println(age() writer x done writing);
}
Set up input parameters for you program
class Arbitrator {
private volatile boolean lock_flag = false; // shared boolean flag
public void wantToEnterCS(int i) { // pre-protocol
while (lock_flag) { // busy wait
}
lock_flag = true;
}
public void finishedInCS(int i) { // post-protocol
lock_flag = false;
}
}
class Arbitrator {
private volatile int turn = 0; // strict alternation
public void wantToEnterCS(int i) { // pre-protocol
while (turn != i) { // busy wait
}
}
public void finishedInCS(int i) { // post-protocol
turn = other(i);
}
}
private void multiply(int[][] a, int[][] b, int[][] c, int p, int q) {
LinkedList workers = new LinkedList();
for (int aStart = 0; aStart < a.length; aStart += p) {
for (int bStart = 0; bStart < b[0].length; bStart += q) {
Thread multiply = new Thread(
new MultiplyWorker(a, b, c, p, q, aStart, bStart)
);
workers.add(multiply);
multiply.start();
}
}
}
public class MultiplyWorker implements Runnable {
public void run() {
for (int i = aStart; i < aStart + p; i++) {
for (int j = bStart; j < bStart + q; j++) {
for (int k = 0; k < b.length; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
}
Suppose that one thread executes the statementsint x = 0, y = 0, z = 0;
and the other thread executes the statementy = 1; z = 2;
What are ALL possible resulting values for the variable x? Assume that each thread is executing on its own CPU. Consider all possible interleavings and assume that we are dealing with a RISC-like load-store architecture: the only machine instructions that reference memory are register load and register store. Explain your answers!x = y + z;
Suppose that one thread executes the loopint sum = 0;
for (int i = 0; i < 100; i++) {
sum = sum + 1;
}
and the other thread executes an identical loop.
shared boolean lock_flag = false;
mutex_begin() { mutex_end() {
while (lock_flag) { lock_flag = false;
/* continue */ ; }
}
lock_flag = true;
}
Rob, on your quad core machine, can you make some runs with various block sizes using 1, 2, 3, and 4 CPUs? I think the commands are like this.
prompt% taskset -c 0 java Driver prompt% taskset -c 0-1 java Driver prompt% taskset -c 0-2 java Driver prompt% taskset -c 0-3 java Driver
export JAVA_HOME=$HOME/jdk1.6.0_18
export PATH=$JAVA_HOME/bin:$PATH
Everybody must be ready to turn in a USB drive containing their program and a text file containing the output of several test runs, although it is not certain that I will collect the USB drives. Guaranteed, though, is that I will call on one or two undergrads at random and one or two grad students at random to come to the front of the room and demo their program. A detailed explanation and running commentary will be sought from those students so you better know your program inside and out!
% java -version
java version "1.6.0_16"
Java(TM) SE Runtime Environment (build 1.6.0_16-b01)
Java HotSpot(TM) Server VM (build 14.2-b01, mixed mode)
On a fully up-to-date Ubuntu 8.04 LTS system the Java version is
% java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Server VM (build 14.3-b01, mixed mode)
If your computer differs, run the Synaptic Package Manager to
Mark All Upgrades as described below.
sun-java6-bin sun-java6-demo sun-java6-fonts sun-java6-jdk sun-java6-jre sun-java6-plugin
% java -version java version "1.6.0_17" Java(TM) SE Runtime Environment (build 1.6.0_17-b04) Java HotSpot(TM) Server VM (build 14.3-b01, mixed mode)
home page:
http://elvis.rowan.edu/~hartley/index.html
e-mail:
hartley@elvis.rowan.edu