Docsity

Homework Assignment for Concurrent Programming (CIS6930) - Week 1, Assignments of Computer Science

Toggle sidebar
bg1
Homework 0
CIS6930 Concurrent Programming
Assigned: 8/30/02
Due: 9/6/02
1. Implement a java class called hw0 that takes three command line arguments: N, the
number of threads; D, the approximate total number of seconds the program should
run; and S, the number of msecs between output of status information.
•The main method of hw0 performs the following
Read the command line arguments N, D, and S
Create and start N threads
Record the current time
Enter the following loop
do{
sleeps for S msecs [
prints the value of x
while{ D seconds have not elapsed }
Print a list of the names of the threads that are still alive
Interrupt remaining alive threads
Terminate
•All the threads and the thread running the main method share a long
variable, x.
•Each thread has a local variable n with a unique value ranging from
[0..N−1] and a name "Tn". Thus the thread 0 has n=0 and a name "T0".
Each thread performs the following loop, where inc = n+1.
while(!interrupted())
{ t = x;
x = x+inc;
assert t+inc==x : thread name;
}
[Hints: You will need the following methods: java.lang.System.currentTimeMillis;
and from java.lang.Thread: sleep, activeCount, isAlive, enumerate, interrupt,
interrupted. The code fragments above are only to indicate what needs to be done.
You will need to correct some details. Also, you should NOT use any kind of
synchronization for this assignment.]
2. Use the assignment axiom and sequential composition rule to prove that
{true} t := x; x := x + inc; {t + inc = x }.
pf2

Partial preview of the text

Download Homework Assignment for Concurrent Programming (CIS6930) - Week 1 and more Assignments Computer Science in PDF only on Docsity!

Homework 0

CIS6930 Concurrent Programming Assigned: 8/30/ Due: 9/6/

  1. Implement a java class called hw0 that takes three command line arguments: N, the number of threads; D, the approximate total number of seconds the program should run; and S, the number of msecs between output of status information. - The main method of hw0 performs the following

Read the command line arguments N, D, and S Create and start N threads Record the current time Enter the following loop do{ sleeps for S msecs [ prints the value of x while{ D seconds have not elapsed } Print a list of the names of the threads that are still alive Interrupt remaining alive threads Terminate

  • All the threads and the thread running the main method share a long variable, x.
  • Each thread has a local variable n with a unique value ranging from [0..N−1] and a name "Tn". Thus the thread 0 has n=0 and a name "T0". Each thread performs the following loop, where inc = n+1.

while(!interrupted()) { t = x; x = x+inc; assert t+inc==x : thread name; }

[Hints: You will need the following methods: java.lang.System.currentTimeMillis; and from java.lang.Thread: sleep, activeCount, isAlive, enumerate, interrupt, interrupted. The code fragments above are only to indicate what needs to be done. You will need to correct some details. Also, you should NOT use any kind of synchronization for this assignment.]

  1. Use the assignment axiom and sequential composition rule to prove that {true} t := x; x := x + inc; {t + inc = x }.
  1. If you run your program from part 1 long enough, you will see the assertion that you just proved in problem 2 violated. Briefly explain what happened.
  2. Consider the following fragment of class

class hw1_ { int r,w;

public hw1_2(){_______________}

void m() { if ___________________ r := r+1; else if __________________w := w + 1; else ; //skip }

}

Suppose that the invariant of this class is (0 ≤ w ≤ 1) ∧ (r = 0 ∨ w = 0)

a. Describe the values of r and w that satisfy the invariant. b. Provide a body for the constructor that explicitly establishes the invariant. c. Fill in the guards of the if statement so that the invariant is preserved. Use the weakest guards possible.