The assignment is as follows:
The goal of this homework is to get familiar with system calls related to processes in the UNIX operating system. You will write a program that uses multiple processes to compute the sum of a set of (small) positive integers. This is a very strange method of computing the sum of a set of numbers, but we will use it to learn about processes.To be honest, I feel like a fish out of water, like many of the others in the class. We haven't been exposed to any Unix and the professor hasn't, yet, said much more than the book, which is no help at this point.
There are two kinds of processes for this assignment:
A set of ``worker'' processes. Each worker process reads two small integers from its argv, computes its sum and returns the result using exit system call. So, for every sum a worker process is created.
A ``coordinator'' process. It is responsible for creating the ``worker'' processes, and coordinating the computation. Note that all the computation is done by the ``worker'' processes. All the numbers are provided in the command line (argv).
The coordinator process also sets a timer at the start of computation to a reasonable limit (say 10 seconds). If computation has not finished by that time, the coordinator process kills all the workers and then terminates. Make sure that you print appropriate message(s) in such case.
In addition, the coordinator process should print a message when an interrupt signal (^C) is received. However, it should not be terminated. The worker processes just ignore the interrupt signals. Note that the processes must handle multiple interrupts correctly. As a precaution, add this feature only after your program is well debugged.
Note that the coordinator may have to create multiple sets of processes. For example, if there are 8 numbers, the coordinator will first create 4 workers and get the results from them. At this point there are 4 numbers, and it creates 2 workers. Finally one worker is created to compute the overall sum. To make it simpler, if the number of integers to add is odd, the coordinator adds a 0 to the list of numbers. Note that this may happen at any step during the computation.
The command line would look something like:
coordinator 1 2 3 4 5 6 7
Since the results are passed around by exit keep the numbers small (single digit). Note that this is not a good way for communication between processes. Each worker process should print its process id, its operands and their sum. Each time the coordinator gets a result from a worker, it must print the pid of the worker, and the result received from that worker. Achieve maximum ``parallelism'' in your implementation. If you are not using makefile, please include the name of the compiler you are using and any special options needed as comments (along with other traditional comments) at the beginning of your source code.
From what I've figured out and what I've blindly seen on the net and hoped would work, this is what I've come up with so far.
coordinator:
Expand|Select|Wrap|Line Numbers
- //COORDINATOR TEST
- #include <signal.h>
- #include <iostream.h>
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <stdlib.h>
- int main( int argc, char* argv[] )
- {
- int status = 0;
- int numCount = argc - 1;
- int* numbers = new int[numCount];
- int numIndex = 0;
- //Place numbers into an array of their own
- for ( int i = 0; i < numCount; i++ )
- {
- numbers[i] = strtol( argv[i+1], NULL, 10 );
- }
- //If length of numbers array is odd, add a 0 to make it even
- if ( ( numCount % 2 ) != 0 )
- {
- numCount++;
- int* tempNumbers = new int[numCount];
- for ( int i = 0; i < numCount; i++ )
- {
- tempNumbers[i] = numbers[i];
- }
- tempNumbers[numCount] = 0;
- delete [] numbers;
- numbers = tempNumbers;
- }
- /*
- for (int i = 0; i < numCount; i++)
- {
- cout << numbers[i] << endl;
- }
- */
- int result = fork();
- cout << "result: " << result << endl;
- switch(result)
- {
- case -1: {
- cout << "Unable to fork" << endl;
- exit(-1);
- }
- case 0: {
- execlp("worker", numbers[numIndex], numbers[++numIndex]);
- numIndex++;
- break;
- }
- default: {
- int childPID = wait(&status);
- cout << "The PID of the child process is " << childPID << endl;
- cout << "The partial sum is " << ?? << endl;
- break;
- }
- }
- return 0;
- }
I realize the current line:
Expand|Select|Wrap|Line Numbers
- execlp("worker", numbers[numIndex], numbers[++numIndex]);
Even if that problem is solved, I really have no idea how to proceed from there. I'm 99% sure the above isn't going to get me the solution I need. Does anyone have any suggestions?
Any help is greatly appreciated.