user1472346
user1472346

Reputation: 157

Multiple children reading from/writing to a single parent

Here I have a program where a parent process creates several child processes, passes a distinct integer to each of them. Then each child process writes back the integer read to the parent process, which prints the result to standard output:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define R 0
#define W 1

void run_child(int in, int out){
    int r;
    int it;
    while((r = read(in, &it, sizeof(it))) != 0){
        if(r == -1){
            perror("read");
            exit(1);
        }
        //word[r] = '\0';
        int w = write(out, &it, sizeof(it));
        if(w == -1){
            perror("write");
            exit(1);
        }
        if(close(out) == -1){
            perror("close");
            exit(1);
        }
    }
}
int main(int argc, char **argv) {

    // Process fan

    int i;
    int n;
    int num_kids;
    int from_parent[2];
    int to_parent[2];


    if(argc != 2) {
        fprintf(stderr, "Usage: fan_write <numkids>\n");
        exit(1);
    }

    num_kids = atoi(argv[1]);
    int status;

    char word[32];
    for(i = 0; i < num_kids; i++) {
        if(pipe(from_parent) == -1){
            perror("pipe");
            exit(1);
        }
        if(pipe(to_parent) == -1){
            perror("pipe");
            exit(1);
        }
        int g = i;
        write(from_parent[W], &g, sizeof(int));

        n = fork();
        if(n < 0) {
            perror("fork");
            exit(1);
        }
        if(n == 0){
            if(close(from_parent[W]) == -1){
                perror("close");
                exit(1);
            }
            if(close(to_parent[R]) == -1){
                perror("close");
                exit(1);
            }
            dup2(from_parent[R], STDIN_FILENO);
            if(close(from_parent[R]) == -1){
                perror("close");
                exit(1);
            }
            run_child(STDIN_FILENO, to_parent[W]);
            close(to_parent[W]);
            exit(0);
        }

        if(close(from_parent[R]) == -1){
            perror("close");
            exit(1);
        }
        if(close(to_parent[W]) == -1){
            perror("close");
            exit(1);
        }

        if(close(from_parent[W]) == -1){
            perror("close");
            exit(1);
        }

        for(i=0;i<num_kids;i++){
            int read_int;
            int r = read(to_parent[R], &read_int, sizeof(int));
            printf("read %d bytes\n", r);
            if(r == -1){
                perror("read");
                exit(1);
            }
            printf("%d\n", read_int);
        }
    }
    for(i = 0; i < num_kids; i++){
        wait(&status);
    }

    return 0;
}

With num_kids = 4 I would expect the program to read 4 bytes each time and print the distinct integer. However, when run it reads 4 bytes in one iteration, and then it reads 0 bytes on the following iterations, and prints the same integer over and over. I'm not sure how to fix it.

Edit: Solved! Hint: use a matrix of file descriptors for pipes.

Upvotes: 4

Views: 1765

Answers (3)

Naramsim
Naramsim

Reputation: 8732

I've mixed up the aswers code and this is the final result.. All succesful

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#define R 0
#define W 1

void run_child(int in, int out){
    int r;
    int it;
    while((r = read(in, &it, sizeof(it))) != 0){
        if(r == -1){
            perror("read");
            exit(1);
        }

        int w = write(out, &it, sizeof(it));
        //perror("write:");
        //close(out) ;

        exit(1);

    }
}
int main(int argc, char **argv) {

    // Process fan

    int i;
    int n;
    int num_kids;
    int from_parent[2];
    int to_parent[2];


    if(argc != 2) {
        fprintf(stderr, "Usage: fan_write <numkids>\n");
        exit(1);
    }

    num_kids = atoi(argv[1]);
    int status;

    char word[32];
    for(i = 0; i < num_kids; i++) {
        if(pipe(from_parent) == -1){
            perror("pipe");
            exit(1);
        }
        if(pipe(to_parent) == -1){
            perror("pipe");
            exit(1);
        }
        int g = i;
        write(from_parent[W], &g, sizeof(int));

        n = fork();
        if(n < 0) {
            perror("fork");
            exit(1);
        }

        if(n == 0){//child
            if(close(from_parent[W]) == -1){
                perror("close");
                exit(1);
            }
            if(close(to_parent[R]) == -1){
                perror("close");
                exit(1);
            }
            run_child(from_parent[R], to_parent[W]);

            if(close(from_parent[R]) == -1){
                perror("close");
                exit(1);
            }
            if(close(to_parent[W]) == -1){
                perror("close");
                exit(1);
            }

            exit(0);
        }
        else{ // parent
           write(from_parent[W], &g, sizeof(int)); 
           int read_int;
           int r = read(to_parent[R], &read_int, sizeof(int));
            printf("read %d bytes\n", r);
            if(r == -1){
                perror("read");
                exit(1);
            }
            printf("%d\n", read_int);
        }

    }


    for(i = 0; i < num_kids; i++){
        wait(&status);
    }

    return 0;
}

Upvotes: 0

47dev47null
47dev47null

Reputation: 282

if (n == 0) { //child
    if(close(from_parent[W]) == -1) {
        perror("close");
        exit(1);
    }
    if(close(to_parent[R]) == -1) {
        perror("close");
        exit(1);
    }
    run_child(from_parent[R], to_parent[W]);
    close(from_parent[R]);   // ignore checking return code here!
    close(to_parent[W]);     // ignore checking return code here!
    exit(0);
}

// And this is what run_child looks like
void run_child(int in, int out){
    int r;
    int it;
    while((r = read(in, &it, sizeof(it))) != 0){
        if(r == -1){
            perror("read");
            exit(1);
        }

        int w = write(out, &it, sizeof(it));
        if(w == -1){
            perror("write");
            exit(1);
        }
    }
}

Upvotes: 1

Grijesh Chauhan
Grijesh Chauhan

Reputation: 58271

you have wrong concept to read numbers from all num_kids child process in a loop, your are reading every time from single child and in each child, by loop:

   for(i=0;i<num_kids;i++){ 
        int read_int;
        int r = read(to_parent[R], &read_int, sizeof(int));
        printf("read %d bytes\n", r);
        if(r == -1){
            perror("read");
            exit(1);
        }
        printf("%d\n", read_int);
    }

Is code runs for each child including parent too because you are unconditionally running this loop in child/parent process. But parent can read from single child that the reason you fist time get 4 byte then 0. because child return a number one time. Remove above for loop from your code and do like I am suggesting below:

you should do like(read comments):

if(n == 0){
   //child
}
else{ 
  // read in parent without loop
}

So correct way is:

    if(n == 0){//child
        if(close(from_parent[W]) == -1){
            perror("close");
            exit(1);
        }
        if(close(to_parent[R]) == -1){
            perror("close");
            exit(1);
        }
        dup2(from_parent[R], STDIN_FILENO);
        if(close(from_parent[R]) == -1){
            perror("close");
            exit(1);
        }
        run_child(STDIN_FILENO, to_parent[W]);
        close(to_parent[W]);
        exit(0);
    }
    else{ // parent
       write(from_parent[W], &g, sizeof(int)); 
       int read_int;
       int r = read(to_parent[R], &read_int, sizeof(int));
        printf("read %d bytes\n", r);
        if(r == -1){
            perror("read");
            exit(1);
        }
        printf("%d\n", read_int);
    }

And its working like:

:~$ ./a.out  4
read 4 bytes
0
read 4 bytes
1
read 4 bytes
2
read 4 bytes
3

Upvotes: 2

Related Questions