Laboratory Exercises For Computer Science 295

Lab 9: Message Passing Examples

Lab 9: Message Passing Examples

Goals: This lab provides additional practice involving process creation, message passing, and process synchronization in SR.

Program prime-finder.sr (in directory ~walker/parallel/sr/examples) determines the first N prime numbers. The approach uses the Sieve of Eratosthenes and represents a variant of Program 6.3 in the text, as described on page 135. Here, a generator thread sends the numbers 2, 3, 4, ... to the first filter thread. Each filter contains its own in_stream mailbox to receive numbers. Each time a filter thread is created, the first number it receives is its prime. Since the program also is to count primes, the second number a filter thread receives is a sequence number for that prime. The filter prints this prime and sequence number. As the filter receives subsequent prime candidates from its mailbox, it filters out those candidates which are divisible by its prime, as those candidates cannot themselves be primes. When the first of these non-divisible candidates is found, a new filter thread is created. This new filter then receives all remaining non-divisible candidates.

Overall, this outline follows Program 6.3, except that each filter is started only as needed, and the new threads are spread over various machines.

Steps for this Lab:

  1. Copy prime-finder.sr to your account, compile it, and run it to check that it works as expected.

  2. Explain how the program decides when to stop.

  3. Write a paragraph explaining when and where threads are created.

  4. The generator resource contains a statement nap(5) to periodically slow down the initial stream of numbers.

  • In program prime-finder.sr, each filter thread sends output to the screen. While this normally produces the desired output, at least one trial by the instructor resulted in primes being printed out of order. How could it happen that the primes were printed in an order other than the order they were computed?

  • To resolve any possible ordering problem, each filter could send its result back to the main resource, which could tabulate the results before printing.

    Create a mailbox within main resource to receive all computed primes and store those in an array. Then, modify the filter resource as needed so it sends its computed prime to main, rather than printing directly to the screen. When main receives the desired number of primes, it should print the results and stop the program.


    Lab work to be turned in: Due Thursday, May 1)