CS314-Parallel-Computation

Overview

Step 1

In this lab we will create a multi-process program that uses a mutually exclusive shared variable to perform parallel computation. The usage of the program is

1
./average <fileflame>

where filename is the name of a 1000 line long input file, with each line containing a 9 digit number (plus a newline). There is no leading whitespace before any value. The file is therefore 10001 characters long (10 chars per line times 1000 lines plus an EOF marker). You may assume the file is correctly formatted and do not have to error check for invalid data. An example file is provided on the course web page.

Your program will spawn 5 processes. Each process will (simultaneously) read a different 200 lines from the input file and compute the sum of these 200 lines. The processes then add the sum to a variable in shared memory (as well as printing it to the screen) and terminate. The parent process (of all 5 children) waits until the 5 children have completed and divides the stored value by 1000 to compute an average. The average is then printed to the screen with 2 decimal places of precision.

Here is an example of use:

1
2
3
4
5
6
7
tami@cs:0~]./average testdata.txt
Child 1 sum is 47606451980
Child 2 sum is 48722283695
Child 3 sum is 46980337615
Child 4 sum is 49206486425
Child 5 sum is 47751953548
Average 381648245485.22

Step 2

Please ensure that the following requirements are met:

  • Your code must exist in your home directory on Bash in a file named $HOME/lab7/average.c and must compile to create an executable. The compilation command will be

    1
    gcc -Wall -Wextra -Werror -o average average.c
  • Your program must perform error checking on the file name and command line parameters.

  • You must have a total of 6 processes (not threads). Do not use sleep() to synchronise your processes and instead, use wait(), synchronous system calls, or semaphores.
  • You must have a single variable in shared memory to hold the five sums and you must use the 5 child processes to add to that variable.
  • To function correctly, the shared variable must be a mutually exclusive resource and protected with a semaphore.
  • Your code must be well formatted, attractive, readable, and maintainable. Changing the number of lines in the file or number of processes used should be trivial.
  • The C code you write must adhere to good programming practices. It should be indented, contain suitable comments, use preprocessor directives, and have intuitive variable names. Code that merely works and does not follow good practice DOES NOT meet the requirements. Marking and help during office hours will only be performed on code that executes on Bash.

Hints

Begin by writing a program that reads the command line arguments, forks off the correct number of processes, and has each process print out its PID and exit. Then, add shared memory and a semaphore to make the shared memory a mutually exclusive resource. Have each process print a constant value into the shared memory just to ensure that it is being used correctly. Get the parent process to wait for the five child processes and then divide the value to compute an average. Finish off by adding code to the children to read the correct part of the file and add the values. As you are only storing a sum, you do not need to store the file’s contents in memory.

Test your program on data other than the sample data. To do that, you may have to write a program to generate data files (and compute the average of the values).

Please be aware that the values in the sample output are incorrect. The purpose of the sample output is to demonstrate the format and use of the program, not to give you values that you must duplicate. All source code must be written from scratch.