• Home
  • Textbooks
  • The JR programming language : concurrent programming in an extended Java
  • ASYNCHRONOUS MESSAGE PASSING

The JR programming language : concurrent programming in an extended Java

Ronald A Olsson; Aaron W Keen

Chapter 7

ASYNCHRONOUS MESSAGE PASSING - all with Video Answers

Educators


Chapter Questions

Problem 1

Consider the code in StreamMerge (see Section 7.1). Explain the effect, if any, on the program's execution if process two's last send is changed to send stream1 (BOS) .
.

Check back soon!

Problem 2

The code in StreamMerge (see Section 7.1) assumes that EOS is larger than the other values in the stream. This exercise explores removing that assumption.
(a) Run the original program. To make the program concrete, have process one send the stream $1,3,5,7,9$, EOS and process two send the stream 4,8 , EOS.
(b) Run the program from part (a) with the following values of EOS: 99, -99 , and 6. Compare each of the outputs from these runs with the output from the program in part (a); explain any differences
(c) The program in part (b) implicitly assumes that EOS appears only at the end of the stream. Or, if EOS does appear midstream, it is still interpreted as EOS, and thus terminates the stream; subsequent numbers are just ignored. Confirm this behavior by running the program with EOS set to 5 .
(d) Rewrite the code in StreamMerge so EOS can be an arbitrary integer value that is not necessarily larger than the other values in the stream. (Assume that EOS appears only as stated in part (c).) This program's behavior should be identical to the original program's behavior except for the value of EOS it outputs. Run the modified program for the values of EOS given in part (b).

Check back soon!

Problem 3

Rewrite the code in StreamMerge (see Section 7.1) so it uses a family of two processes to represent the processes that produce the streams and an array of operations to represent the streams.

To make the program concrete, have the first process send the stream 1 , $3,5,7,9$, EOS and the second process send the stream 4, 6, 8, 10, 12, 14, 16 , EOS.

Check back soon!

Problem 4

Generalize the previous exercise to have $\mathrm{N}$ processes producing streams.

Check back soon!

Problem 5

Consider the code in class Cap3 (see Section 7.2).
(a) Rewrite the code so that the two processes are in different classes and $\mathrm{f}$ is still declared local to $\mathrm{p}$.
(b) Rewrite the code so that the two processes are in different classes and $\mathrm{f}$ is now declared private to the class containing $\mathrm{p}$.
(c) Would either of the above two programs or the original program work if getcap were declared private? Explain.

Check back soon!
01:03

Problem 6

Show the output from the following program on input of 1 and on input of 87 .

public static void main(String [] args) \{
cap void (int) $c$;
int $z$;
$/ /$ read in a value for $z$
$\ldots$
if $(z<3)\{c=f ;\}$
else $\{c=g ;\}$
$c(22) ; / /$ invokes either $f$ or $g$
\}
\}
public static void main(String [] args) \{
cap void (int) c;
int $\mathrm{z}$;
$/ /$ read in a value for $z$
...
if $(z<3)\{c=f ;\}$
else $\{c=g ;\}$
$c(22) ; / /$ invokes either $f$ or $g$
\}
\}

Mohamed Mohamed
Mohamed Mohamed
Numerade Educator
03:29

Problem 7

Rewrite the code in program Model4 (see Section 7.3) so it uses arrays of result operations as in program Model2.

Shelby Mohamed
Shelby Mohamed
Numerade Educator

Problem 8

The ResourceAllocatorMultiple program in Section 7.4 allows clients to request or release resources only one unit at a time. Generalize the program to allow clients to request or release resources multiple units at a time. For simplicity, assume that clients release only those units it currently possesses, as was assumed implicitly in the ResourceAllocatorMultiple program. However, clients need not release units in the same groups as they acquired them.

Check back soon!

Problem 9

The ResourceAllocatorMultiple program in Section 7.4 could use a more object-oriented style in how it uses messages.
(a) Change action's second parameter from an integer to an instance of a new MsgKind class, which encapsulates the different kinds of messages. (That is, simulate an enumeration type using MsgKind.) Define the new class and change ResourceAllocatorMultiple as needed.
(b) Replace action's second and third parameters with an instance of a new, abstract Msg class. Define a subclass, derived from Msg, for each kind of message. (The subclass corresponding to the release message will contain the unit identifier.) Change ResourceAllocatorMultiple as needed.

Check back soon!

Problem 10

Atomic Broadcast Problem (based roughly on Exercise 6.16 in Reference [7]). A single slot integer buffer is shared by one producer process and $N$ consumer processes. The producer repeatedly deposits messages into the buffer; consumers repeatedly fetch messages from the buffer.
(a) A given message is fetched by any $N$ processes, not necessarily all $N$ processes. That is, a given message might be fetched multiple times by some processes and not at all by other processes.

Does your solution also work if there are multiple producers? Explain. (Address multiple producers only in this particular part; do not use multiple producers in your solutions to any other parts of this question.)
(b) Modify your solution to the previous part so that each consumer process fetches exactly once each message the producer deposits.
(c) Modify your solution to the previous part so that only the consumer processes waiting (if any) when the producer deposits a message get to fetch that message.

Your solution must be structurally similar to the ResourceAllocatorMultiple code in Section 7.4. This problem requires three kinds of processes that interact with only the indicated interfaces:
- producer - similar to a client. It interfaces to the coordinator by send request(DEPOSIT, UNUSED, somevalue); receive preply();
- consumer, with identity i between 0 and $N-1-$ similar to a client. It interfaces to the coordinator by
send request (FETCH, $i$, UNUSED);
receive creply[i] (somevariable);
- coordinator - similar to the server; it holds the buffer and synchronizes access to it.

Use only send and receive (no inni, call, reply, forward, etc.).

Check back soon!
04:20

Problem 11

Savings Account Problem. A savings account is shared by several customers of a bank. Each customer may deposit or withdraw funds from the account. Assume that the amount of each deposit or withdrawal is positive. The current balance in the account is the sum of all deposits to date minus the sum of all withdrawals to date. The balance must never become negative, which implies that some withdrawals might need to be delayed.

Your solution must be structurally similar to the ResourceAllocatorMultiple code in Section 7.4. Each customer is a client and the bank is the server. On a deposit, your solution must service any waiting withdrawal request(s) that can be serviced. (I.e., it is non-FCFS in part.)

Jennifer Stoner
Jennifer Stoner
Numerade Educator
02:10

Problem 12

Copy the outline for the adaptive quadrature program (see Section 7.7 ) into a file.
(a) Modify the program so that $f(x)$ is $x^2+3 x$, Epsilon is 0.0001 , and the interval is $[1,10]$. Execute the program and compare the computed area with the exact mathematical result.
(b) Modify the original program so that the number of workers, $\mathrm{N}$, is a command-line argument. Execute your program using different numbers of workers. How does the performance differ?
(c) Repeat part (b) but for the program from part (a).

Madi Sousa
Madi Sousa
Numerade Educator

Problem 13

Copy the outline for the adaptive quadrature program (see Section 7.7) into a file.
(a) Modify the program sothat $f(x)$ is $x^3+2 x+1$, Epsilon is 0.0001 , and the interval is $[0,10]$. Execute the program and compare the computed area with the exact mathematical result.
(b) Modify the above program so that $\mathrm{f}$ is declared as a public static method in a different class.
(c) Modify the program again so that $\mathrm{f}$ is declared as a public non-static method in a different class, named fun.
(d) Modify the part (c) program so that the main invokes $\mathrm{f}(0.5) T$ times (instead of computing the area). Do not have the program perform any I/O. Time the results ten times using the UNIX csh command "repeat 10 time jrrun AQ" (or the equivalent in another shell). ${ }^1$ (Also see Exercise 10.10.)
Pick $T$ so that the program runs at least 10 seconds but less than 30 seconds. The specific value for $T$ will depend on the speed of the processor on which the program is run. Start with 1,000 invocations and increase that until a suitable value for $T$ is determined.
Report the value of $T$ and the ten execution times.

Check back soon!

Problem 14

Recall how the adaptive quadrature program (see Section 7.7 ) depends on JR's automatic program termination detection for its termination and to output its result. Suppose JR did not provide automatic program termination detection. Modify the program so it terminates (and still outputs its result) using (a) The technique suggested earlier in which the administrator keeps track of which parts still need to be computed.
(b) A different technique where the administrator counts how many outstanding tasks need to be completed. Your solution might need to use additional messages between the administrator and the workers.

Check back soon!

Problem 15

Write a program that implements the quicksort method of sorting using a shared bag of tasks. The tasks are slices of the data that need to be sorted. The administrator should put the initial data in the bag. A worker should remove a task, partition it into smaller tasks and put them back in the bag. If a task is small enough, say eight data elements, a worker should instead sort the data and send it to the administrator.

Execute your program using different input data and different numbers of workers. How does the performance differ?

Check back soon!

Problem 16

Develop a distributed program to implement a compare/exchange sorting algorithm. Use W worker processes laid out in a line; each worker should communicate only with its one or two neighbors. If there are $\mathrm{N}$ items to sort, initially each worker should be given N/W items. Each worker should sort its items, exchange high and low elements with its neighbors, and repeat until all $\mathrm{N}$ items are sorted.
(a) Execute your program using different input data and different numbers of workers. How does the performance differ?
(b) Compare the performance of this program to your program for Exercise 7.15.

Check back soon!

Problem 17

Develop a program to generate prime numbers using a shared bag of tasks. The tasks are odd numbers that should be checked for primality. The workers check different candidates. Each worker should have a local table of primes that it uses to check candidates. When a worker finds a new prime, it should send it to all the other workers.

Execute your program for different ranges of primes and different numbers of workers. How does the performance differ?

Check back soon!