Homework Two
For this assignment you are to write two programs in either C or C++ using the pthreads thread library. Your programs must compile without warnings and execute correctly on linux.csce.uark.edu for full credit. You must turn in both programs for full credit. Also, for full credit use good programming style, including the use of an appropriate amount of comments. In addition to the source code, your submission must also include the answers, in a plain text file, to the questions found at the end of the page. Submit the source code and answers to the questions to Hai Nguyen, hqn01@uark.edu.

Pthread Program One

The goal of this assignment is learn basic thread programming and to develop a program using the master/slave programming model. You will use mutex locks to avoid race conditions on data shared by several threads. Using the master/slave "Second model" pseudocode as as a starting point, write a C or C++ program using the pthread library as follows:

Test input data:

  1. ParallelPrimes
    Output: Usage: ParallelPrimes number numthreads

  2. ParallelPrimes 20 3
    Output: Primes found: 2, 3, 5, 7, 11, 13, 17, 19

  3. ParallelPrimes 20 20
    Output: Primes found: 2, 3, 5, 7, 11, 13, 17, 19

  4. ParallelPrimes 20 30
    Output: Primes found: 2, 3, 5, 7, 11, 13, 17, 19

  5. ParallelPrimes 1000 4

  6. ParallelPrimes 1 2
    Output: Error: number must be at least 2.

  7. ParallelPrimes abc
    Output: Error: number must be an integer.

Pthread Program Two

The goal of this program is to make the multithreaded prime calculation more efficient.

Using your code from the previous program, write a program efficientPrimes that starts a fixed number of threads, as in the previous assignment, to determine the primality of all numbers in a range, but is more efficient. For example, when a thread enters the critical section to mark off that x is prime, the thread may also mark off 2x, 3x, 5x, 7x, 11x, etc., as ``taken'' and not prime. You may want to add other optimizations as well.

Have main echo the command line argument information to the screen. When each thread starts, print out a message, and also at each time that a number is taken, have the thread print out its name and the number it has just taken.

Be careful about race conditions! Make your code as efficient as you can, that is, try to have threads duplicate work as little as possible. But don't have a nervous breakdown trying to squeeze out every last microsecond. Also, try to keep the parallelism as great as possible. Since only one thread at a time can be in the synchronized block, make sure that a thread is in there for as little time as necessary.

To check your efficiency, add a call to time your code to determine how long your program ran.

Use the same test input data as in the previous assignment. You may add other test data, if you wish.

For this program we may give a prize to the program that executes in the shortest amount of time. We will definitely do this if there is a clear winner!

For both programs answer the following summary questions in a plain text file:

  1. Which features of the assignment did you not successfully implement?
  2. How did the input you used test your program thoroughly for its correct operation?
  3. How did you analyze the output of your program to prove to yourself that the output of your program shows your program is working correctly?
  4. Examine you code and determine what could go wrong in the program if there were mutex locks in the program. In other words, find and describe all possible race conditions.
  5. How many hours did you spend designing, coding, and debugging this assignment?
Enjoy!