1

This is for a class so I am trying to understand why the variable nChars is not being set when the child process returns. I have read that waitpid() reaps the child process but when I try to print nChars it still shows zero when the childs' nChars is the number of the commandline characters

int main(int argc, char **argv)
{
    // set up pipe
  int      fd[2], status;
  pid_t    childpid;

  pipe(fd);
    // call fork()
  if((childpid = fork()) == -1){
    perror("pipe");
    return -1;
  }
  if (childpid == 0) {
        // -- running in child process --
        int     nChars = 0;
        char    ch;

        close(fd[1]);
        // Receive characters from parent process via pipe
        // one at a time, and count them.

        while(read(fd[0], &ch, 1) == 1)nChars++;

        // Return number of characters counted to parent process.
        printf("child returns %d\n", nChars);
        close(fd[0]);

        return nChars;
    }
    else {
        // -- running in parent process --
        int     nChars = 0;
        close(fd[0]);

        printf("CS201 - Assignment 3 - \n");

        // Send characters from command line arguments starting with
        // argv[1] one at a time through pipe to child process.

        for(int i=1; i < argc; i++)
            write(fd[1], argv[i], strlen(argv[i]));


        // Wait for child process to return. Reap child process.
        // Receive number of characters counted via the value
        // returned when the child process is reaped.

        waitpid(childpid, &status, WNOHANG);
        printf("child counted %d characters\n", nChars);
        close(fd[1]);
  return 0;
  }
thepi
  • 342
  • 2
  • 12
  • according to [this page](http://linux.die.net/man/2/waitpid), `WNOHANG` means it does not wait until the child process has returned. You should check the return value of `waitpid` to check if the wait was successful. Note: see also `WIFEXITED` and `WEXITSTATUS` on the same page. – wimh May 30 '15 at 17:28

1 Answers1

2

Parent and child don't share memory, so they have different variables nChars. Child is a COPY of parent, so when you change some variables in copy they doesn't get changed in original. If you need to have one variable visible from two execution flows use threads.

You're returning nChars from child as process exit code, so it'll be in status variable. Try:

waitpid(childpid, &status, 0);
// removed WNOHANG because with it parent won't wait for child to exit
printf("child counted %d characters\n", status);

But it would better to use come IPC mechanism like pipes or sockets to transfer data between child and parent, because exit codes are for program exit status, exit code 0 means all is okay, and other exit codes mean that something gone wrong, exit code is not for transferring arbitrary data

user1940679
  • 176
  • 1
  • 11
  • I agree it would be better to transfer data differently. the goal of the assignment is to get the parent process to use the exited code as the variable for nChars. status is returning the number of arguments in the command line not the number of characters. – thepi May 30 '15 at 18:21