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.
gcc adder.c -lpthread -lm -std=c99 -WallThe -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.
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
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.
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.