0

I am trying to send a value from child to parent but am not getting the desired output. Can anyone figure out what I am doing wrong?

   int main() {
    printf("I am the parent with process ID:%d\r\n", (int) getpid());
    int rc = fork();
    int sum = 0;
    int array[6] = {2, 3, 7, -1, 10, 6};
    int pipeNum[2];

    pipe(pipeNum);

    if (rc < 0) {
        fprintf(stderr, "fork failed\r\n");
        exit(1);
    } else if (rc == 0) {
        for (int i = 0; i < 6; i++) {
            sum += array[i];
        }
        close(pipeNum[0]);
        printf("I am the child with process ID:%d and I am sending %d to my parent.\r\n", (int) getpid(), sum);
        write(pipeNum[1], &sum, sizeof(sum));
        close(pipeNum[1]);      
    } else {
        wait(NULL);
        close(pipeNum[1]);
        read(pipeNum[0], &sum, sizeof(sum));
        printf("I am the parent with process ID:%d with a final sum of %d\r\n", (int) getpid(), sum);
        close(pipeNum[0]);
        exit(0);
    }
    return 0;
}

Here is the output I am getting
I am the parent with process ID:XXX
I am the child with process ID:XXX and I am sending 27 to my parent.
I am the parent with process ID:XXX with a final sum of 0

Any help would be appreciated

Asuu
  • 121
  • 1
  • 11

2 Answers2

1

You are passing the sum to read() and the pointer to sum to write(); you're supposed to pass the pointer for both

read(pipeNum[0], &newSum, sizeof(newSum));

write(pipeNum[1], &sum, sizeof(sum));
J. Brown
  • 124
  • 7
  • When I try this I end up with an error -- note: expected 'const void *' but argument is of type 'int' _READ_WRITE_RETURN_TYPE _EXFUN(write, (int __fd, const void *__buf, size_t __nbyte )); – Asuu Sep 02 '17 at 19:47
  • Mmm sorry now I'm not sure anymore, I'm thinking maybe write() as well needs the pointer. Also it may be that: wherever a pointer it's needed, you should cast it to (void *). Try and let me know – J. Brown Sep 02 '17 at 19:53
  • I found a post and have tried this - I have updated what I am getting which still isn't correct -- https://stackoverflow.com/questions/12864265/using-pipe-to-pass-integer-values-between-parent-and-child – Asuu Sep 02 '17 at 20:09
  • 1
    Try defining and initialising the pipe before the fork! – J. Brown Sep 02 '17 at 20:28
  • 1
    Yep I think it's that because initialising it with pipe() after the fork means that father and child have opened different pipes, which are unaccessible from one to the other – J. Brown Sep 02 '17 at 20:30
  • OMG That was it, Since I called the pipe after forking there was 2 different pipes. Thank you @J.Brown !!!! I spent 2-3 hours trying to find this. – Asuu Sep 02 '17 at 20:39
  • Glad we found it! =D Imma edit the answer to make it more useful! – J. Brown Sep 02 '17 at 21:07
1

The problem was that I was creating a copy of the process and creating a pipe which caused them to not be able to communicate since there was 2 pipes now.

int rc = fork();
int pipeNum[2];
pipe(pipeNum);

What needed to happen is listed below

int pipeNum[2];
pipe(pipeNum);
int rc = fork();

Problem was fixed after creating pipe first

Asuu
  • 121
  • 1
  • 11