0

this is my first post. I have a task to use a fork to create multiple processes and then use execlp to run another program to add 2 numbers.

The problem I am having is we are supposed to use the exit() call in the execlp to return the small integer. This is a bad way to communicate but it's what we are supposed to do for the sake of this program.

Here is my "coordinator" program

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h> 
#include <sys/wait.h> 
#include <stdlib.h>

using namespace std;

int main (int argc,char* argv[])

{
const int size = argc-1;

int sizeArray = 0;

int numofProc =0;

int arrayofNum[size];

int status;

int value;



for(int y=1; y<argc; y++)
{

arrayofNum[y-1] = atoi(argv[y]);

sizeArray++;

}


if(sizeArray % 2 !=0)

{

arrayofNum[sizeArray]  = 0;

sizeArray++;



}



numofProc = sizeArray/2;


//declaration of a process id variable

pid_t pid;


//fork a child process is assigned 

//to the process id

pid=fork();



//code to show that the fork failed

//if the process id is less than 0

if(pid<0)

{

    cout<<"Fork Failed";// error occurred

    exit(-1); //exit

}


//code that runs if the process id equals 0

//(a successful for was assigned

else 

if(pid==0)

{

    //this statement creates a specified child process

     execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);//child process



}

//code that exits only once a child 

//process has been completed

else

{

waitpid(pid, &status, 0);

cout<<status;


}

//main

}  

and here is the execlp process

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


using namespace std;



int main(int argc, char* argv[])

{



int arrayofNum[argc-1];



arrayofNum[0] = atoi(argv[1]);

arrayofNum[1] = atoi(argv[2]);



int sum = arrayofNum[0] + arrayofNum[1];

exit(sum);


}  

My problem is NO MATTER WHAT I DO, the status is ALWAYS printing a 0, I do not know how to retrieve the sum that is returned from the worker process.

My professor told me ""Only the higher byte of status will have the value returned by the worker. you need to extract it. It can be done by many ways. ""

In a nut shell, my question is, How do I retrieve that "sum" that is being sent from my worker process.

Please, I am so confused and have been up for 2 nights wondering about this

Thanks,

John

4

1 回答 1

3

First up, you need to pass strings to your program, but you say:

execlp("./worker", "worker", arrayofNum[0], arrayofNum[1]);

And arrayofNum is an array of integers. Also, with execlp you also need to pass a NULL as the last argument. The standard says:

The arguments represented by arg0,... are pointers to null-terminated character strings. These strings shall constitute the argument list available to the new process image. The list is terminated by a null pointer.

Second, after you call to waitpid(2) you need to do:

if (WIFEXITED(status))
    code = WEXITSTATUS(status);
于 2013-02-10T09:45:29.610 回答