Receiving a return value from a child process into a parent process?

I've read a few threads on here for information on this. I understand some but most of them are written in different methods. I just cant find sufficient information on how to return a value from a child process through a return statement. I blame myself for not looking hard enough, but i'm at a muddy puddle stuck. Any information is appreciated thanks.

My goal for this (school assignment) is to have a child process read information from the parent. The child will ten compute mathematically (addition) each value received from the parent, then send the final value back to the parent to print onto the screen.

RULES: 1. Can't use command line args inside of a child process only parent 2. All mathematical computation must be inside of child process. 3. No error checking is needed.

Here is some code of what i have:

int main(int argc, char **argv){
  char buf[256];
  int pipeCommunication[2];
  pipe(pipeCommunication);

  pid_t pid = fork();

  if(pid == 0){
    //inside child process
    int sum = 0;
    read(pipeCommunication[0], buf, sizeof(buf));
    sum += atoi(buf);
    return sum; //WHY DOES IT NOT RETURN THE VALUE OF SUM?
  }else{
   int sum = 0;
   for(int i = 1; i<argc;i++){
     write(pipeCommunication[0], argv[i], sizeof(argv);
     wait(NULL);
   }
   printf("sum = %d\n", sum); //WHY CANT I SEE THE VALUE OF SUM FROM CHILD?
   return 0; 
  }
}

Command line input would be: ./apprun 1 2 3 OUTPUT: 6 (added 1 + 2 + 3 = 6)

EDIT: I've realize that perhaps my write or read func need to cleared to allow another value to be read into the child process. Still trying to figure that out.

EDIT: I've completed the assignment and will post the result here if anyone wants to view the final code. Afterwards, this thread is completed.

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>

#define SIZE argc-1

int main(int argc, char **argv)
{
  int   userInput[SIZE];        
  int       temp;                  
  int     uiSize = sizeof(userInput)/sizeof(userInput[0]);    

  int   pipeCom[2];             
  pipe(pipeCom);                  

  printf("CS201 - Assignment 3 Regular - Jonathan Vazquez\n");

  pid_t pid = fork();

  if (pid == 0) {

      int sum = 0;

      close(pipeCom[1]);                              
      for(int i = 0;i < uiSize; i++){
          read(pipeCom[0], &temp, sizeof(temp));      
          sum += temp;                                
          temp = 0;                                   
      }

      return sum;                                     
  }else {

      int sum = 0;                                                  
      for(int i = 1; i < argc; i++){
        userInput[i] = atoi(argv[i]);               
      }

      close(pipeCom[0]);                              
      for(int i = 1; i < uiSize+1;i++){              
          write(pipeCom[1], &userInput[i], sizeof(userInput));    
      }

      int status;
      wait(&status);                                  
      sum = WEXITSTATUS(status);                      
      printf("sum = %d\n", sum);
      return 0;
  }
 }

1 answer

  • answered 2018-03-13 20:22 Chris Dodd

    The return value of the child's main function (the 'sum' you are returning here) is accessable to the parent as the child's exit status. The exit status is limited to 8 bits (not a full integer), so it is generally only used to return a limited error code rather than a value, but it may be big enough for your purposes.

    To get the child's exit code in the parent, you need to pass a pointer to a status variable to wait instead of NULL. Replace the wait(NULL) line with:

    int status;
    wait(&status);
    sum = WEXITSTATUS(status);
    

    Alternately (and recommended as 8 bits is usually not enough), you can create a second pipe going from the child back to the parent, and have the child write the sum to that pipe and have the parent read it.