I have two c files I am working on. One is a stand alone program (I will call it helper) that will take two ints as a command line argument, add them together, prints the operands and the sum, and exits. This program can be run from the command line as well.

The second program, is a larger program that takes single digit ints as command line parameters, then takes those ints, and sends them, two at a time to the first program, lets the helper program add them, then gets the sum of the two ints back from from the helper progam.

Yes, I know there are extremely more efficient ways of doing this, and its an assignment to do it this way, so I really have no choice in changing how the addition is done. I have everything working in the helper program by itself, and use the execlp() function to call on it and run it just fine. The issue I am having is getting the sum of the two numbers back from the helper program. I have tried using the exit() function to pass the sum back, but just keeps giving me 0 back as the exit status. This should work because their are only going to be at most 8 digits given to the second program as parameters and all single digit ints and wont exceed 255.

Please help if you can. I am sure this is something simple that I have just overlooked. Here is a bit of the codes. I cut out the majority of the code, cause that works fine.

helper program

#include <stdio.h>
#include <stdlib.h>

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

          //Adds two numbers together
          //Stores sum to sum variable

	exit(sum);	//returns sum and exits program

main(second) program

int main(int argc, char *argv[]){
                  //All other code here

	int index = 0, result ;
	pid = fork();
	if(pid == 0){
                //Child Process
		result = execlp("./helper", "helper", numArray[index], numArray[index + 1], NULL);
                //Parent process

//	}


So basically, i want the value of the sum to be stored to the result variable in the main program that was calculated in the helper program.

Thanks in advance for any advice or help.

> I have tried using the exit() function to pass the sum back, but just keeps giving me 0 back as the exit status.
That's the result of the exec() call, not the result of the child process.

To get the result from the child, the parent uses one of the wait calls, then examines the resulting status value to get the exit status of the child.

But your spec said the child should also print things as well right?

Yes, the child prints the two integers passed to it, and the resulting sum. I have that part working when the child executes, and the sum is stored in a sum variable. Here is the entire code for the helper program if it helps.

#include <stdio.h>
#include <stdlib.h>

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

	int sum, num1, num2;	
	pid_t pid;
	num1 = atoi(argv[1]);	//variables to store two integers  passed in as parameters as int
	num2 = atoi(argv[2]);
	sum = num1 + num2;	//adds two integers and stores to sum variable
	printf("Process ID: %d\n", pid);	//prints process id
	printf("Operands: %d", num1); printf(" and %d\n", num2);	//prints operands
	printf("Sum: %d\n", sum);	//prints sum

	exit(sum);	//returns sum and exits program

To actually read input from the child, you need to set up a pipe().

One end you make the stdout for the child, and the other end you can read directly from the parent.

I have solved this issue by using the WEXITSTATUS function. I used it in the parent after the wait() function, and it returns the value that was used in exit, so since I used exit(sum); it gives me the value that was in the sum variable.

Thank you all for your advice on this matter.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.