Hello
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);
}
else{
//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.