CS 3733 Operating Systems, Spring 2007 Assignment 3


Due March 30, 2007

In this assignment you will experiment with two thread packages, POSIX threads and Java threads.

You have been given the problem of finding the average value of sin2 x on the interval [0,1], where x is measured in radians. Since you have forgotten how to integrate this function, you will do it by picking a large number of random values between zero and one and averaging the values corresponding to these.

You have just purchased a new 8-core CPU so you decide to take advantage of this by using a threaded program to do the calculation.

This assignment has several parts. Each part should be done in its own directory. You need to save the source code for each part of the assignment separately.

Part 0: Getting started on a UNIX system
You will use the rand library call to produce the random numbers. To make sure you use different random numbers each time you run the program, you will seed the random number generator with the time using:
srand((int)time(NULL));
Recall that rand returns a random integer between 0 and RAND_MAX, and you are looking to find random numbers between 0 and 1.

Write a function:
double calc(int n);
that calculates the sum of n values of sin2 x in which each value is calculated from a randomly chosen value of x between 0 and 1. Initialize the random number generator before calling calc.
Write a main program that takes one command line parameter, the number of random values to sum, calls calc and prints the resulting average. The function calc should not do any printing. You can put the calc function in the same file as main. Run the program with at least 100,000 steps. You can use more if you are running on a fast machine. The exact answer is .5 - .25sin 2. Print out the percent error. The percent error is 100 times (your_answer-exact_answer)/exact_answer.

Part 1: POSIX threads
Rewrite calc so the it takes a NULL pointer parameter and returns a pointer to NULL. Have calc modify global variables, sum and count which you initialize to 0 before calling calc. Rerun the program to make sure that it still works.

Next modify the program so that it runs calc as a thread. Join the thread and then print the results.

Now modify the program so that it takes a second command line parameter, the number of threads to create. Create that many threads. Each one runs a number of iterations given on the command line. Join all of the threads and print the result. Be sure to print the count as well as the average. Is the count correct?

When implementing the thread, it can update the global variables in two ways. One possibility is to update the count and sum on each iteration of its loop. Call this Part 1a. Alternatively, the thread can store a local sum and only update the global sum and count when it has finished its loop. Call this Part 1b. Save the source code for each of these parts. Compare these two possibilities. Are there any differences in the way they run? Save the output from both runs. Make sure they are clearly labeled.

Part 2: Java Application
Rewrite the program from Part 0 as a Java application. The application should take the same command line parameter as in Part 0. It should do the calculation and print the result with System.out.println. Run the program and compare the answers to those of Part 0.

Part 3: Java Threads Application
Rewrite Part 2 using a Java application that runs multiple threads. The main program now takes two command line parameters. It should create and start the threads, then join the threads and print the results. The threads should not do any printing. Make 2 versions of this that update the static variables differently as in Part 1. Call these two parts, Part 3a and Part 3b. Run the two programs and compare the answers gotten to each other and to those of Part 1.
Note: You can use this template for you implementation.

Part 4: Analysis
In this assignment we are not using any synchronization constructs except for joining the threads. For each of Part 1 and Part 3, think about what could possibly go wrong with the programs. Describe each of the places in your code that might need to be protected.


Implementation notes:

Handing in your assignment
Use this cover sheet.