Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will write a program that uses multiple processes to compute the sum of a se

ID: 673491 • Letter: Y

Question

You will write a program that uses multiple processes to compute the sum of a set of (small) positive integers. There are two types of processes for this:

I). A set of "slaves" processes: Each slave process gets two small integers from its argv, computes its sum and returns the result using the exit system call. So, a slave process is created for every sum.

II) A "master" process: This process is responsible for creating the slave processes, and coordinating the computation. Note that all the computation is done by the "slave" processes. All the numbers are provided in the command line argv. The master process also set a timer at the start of computation to 3 seconds. If the computation has not been finished by this time, the master process kills all the slaves and then exits. You should print an appropriate message(s) in this case. Note that the master process may have to create multiple sets of slave processes. For example, if there are 8 numbers to be added, then the master process will first create 4 slaves and get the result from them. At this point there are 4 numbers, and it creates 2.

slaves. Finally one slave is created to compute the overall sum. To make it simpler, if the number of integers to add is odd, the master adds a 0 to the list of numbers. This may happen at any step during the computation. The code for master process should be compiled separately and its executable code should be called master. The executable code for the slave process should be called slave. So, to compute the sum of the numbers 1 through 7, the command line will look like:

master 1 2 3 4 5 6 7

Since the results are passed around by exit system call, keep the numbers small (single digit). Each slave process prints its process id, its operands, and their sum. Each time the master gets a result from a slave, it prints the pid of the slave and the partial sum.

Test your programs for the following cases:

master 1

master 1 2 3 4 5

master 1 2 3 4 5 6 7 8

____________________________________________________________________

This is my programming assignment.I got it down so it can handle two arguments(master 1 2) but can't figure out how it have it handle up to 8. Any help would be greatly appreciated.

slave

master

Explanation / Answer

#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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote