Q: Multiple Childs and fork() in C They asked me to write a program to have n process created by one Parent process. The parent starts
a value from 0. Each child process recovers this value and increments it by its own pid(value = value+getpid()) . Then the child process returns the result to his parent who sends it again to the next child process.at the end  The program will display the sum of all child's pid prom parent process.
What i have done is this :
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main()
{
    int n = 2; //number of process to be forked
    int sumPid = 0;
    int fd1[2]; //
    int fd2[2]; // 

    pid_t p;

    if (pipe(fd2) == -1)
    {
        fprintf(stderr, "Pipe Failed");
        return 1;
    }

    if (pipe(fd1) == -1)
    {
        fprintf(stderr, "Pipe Failed");
        return 1;
    }
    for (int i = 0; i < n; i++)
    {
        if (fork() == 0)
        {
            int sumTemp;
            fprintf(stdout, "[Son] pid %d from [Parent] pid %d \n", getpid(), getppid());
            close(fd2[1]);
            read(fd2[0], &sumTemp, sizeof(int));
            close(fd2[0]);
            sumTemp += getpid();
            close(fd1[0]);
            write(fd1[1], &sumTemp, sizeof(int));
            close(fd1[1]);
            exit(0);
        }
        else
        {
            write(fd2[1], &sumPid, sizeof(int));
            close(fd1[1]);

            read(fd1[0], &sumPid, sizeof(int));
            close(fd1[0]);
        }
    }
    fprintf(stdout, "\nLa sum is %d \n", sumPid);

    return (0);
}

I don't know what's the problem exact, but it seems that my code doesn't give me the write result.
