CS520
Fall 2013
Lab 8
Due Wednesday,October 23


Basic Forking with Posix Threads

In this lab, you will be learning the basics how to launch a process in another thread.

Program Description

The program is to take two command line arguments. The first command line argument is the name of a file, and the second command line argument is number of threads to use. For this lab, you may assume that both command line arguments will be valid.

The program is to read through the file one character at a time, and for each character, add the sine of the character to a counter. At the end, the program is to print out the value of the counter to stdout. This is the only output the program should produce on either stdout or stderr.

The reason the program computers the sin is to make the computation part of the program take longer; if you just add up the numbers directly, reading things into main memory from disk takes so long that the amount of time spent doing the calculation is negligible.

Compiling the Program

The program should be completely contained in a single file called adder.c. I will compile it with the following command:
gcc adder.c -lpthread -lm -std=c99 -Wall
The -lpthread links against the pthread library, and the -lm flag links against the math library, allowing you to call the sin function in math.h.

Simple Timing and Expectations

I ran my program on a file that was 232k big, and going from 1 to 2 threads, I saw a speedup of almost 2; your program should demonstrate comparable speedup when run in a similar situation.
cmo66@zelinka:~/cs520/p5/adder$ ls -alh medtext 
-rw-rw-r-- 1 cmo66 cmo66 232K Oct 22 15:31 medtext
cmo66@zelinka:~/cs520/p5/adder$ time ./a.out medtext 1
Sum is 38995.942955

real	0m0.024s
user	0m0.012s
sys	0m0.008s
cmo66@zelinka:~/cs520/p5/adder$ time ./a.out medtext 2
Sum is 38995.942955

real	0m0.014s
user	0m0.016s
sys	0m0.004s

Design Suggestions

You are, of course, free to design this program any way you feel, but here are a few recommendations.

If you want your program to run fast, you should probably copy the entire file into memory all at once, which is faster than reading it in one part at a time. The function stat will help you figure out how big a file is, and fread will allow you to read in a variable amount of the file in at once. Look online for examples of how both these functions work.

You will probably want to have a function that is able to add up a portion of the array, which you can then get to run in a separate thread using pthread_create. This means your function should have the signature void* f (void* p), because if it does not have that signature, you can't call it using pthread_create.

Make sure that each item in the array is summed up properly, and accounted for. It may be worth not putting in the sin until the last minute, instead summing the character values directly to make sure that your method of dividing up the work is valid.

Check the program against itself running in single threaded mode, both should produce the same answers, modulo floating point precision errors.

Grading

In addition, remember, you may lose points if your program is not properly structured or adequately documented. Coding guidelines are given on the course overview webpage.