0

So in my program, the user gives three arguments. Then, I pipe it into three children.

Two children all do their own calculations, then exits. The last child displays the results.

The parent waits for all children to finish, then it simply terminates the program.

A summary of what the children do:

inputs are a, b, c.
child0: c0 = a*b
child1: c1 = b*c
overview: printf("%d %d", c0, c1);

I can't figure out how to get the overview to print out correctly. It keeps printing out weird corrupted characters or boxes.

Anyone have any advice on how to do it correctly? I read a book, but it only went over in detail single child-parent pipes. This deals with multiple children, and I guess that's where I got confused. Thanks for your help!

Code below:

int main(int argc, char *argv[]) {
    int fd[4];
    int a, b, c, c0, c1, status;
    char char0[10];
    char char1[10];
    pid_t child0;
    pid_t child1;
    pid_t overview;

    // Set argv to ints
    a = atoi(argv[1]);
    b = atoi(argv[2]);
    c = atoi(argv[3]);

    // Pipe
    pipe(fd);

    // child0
    if((child0 = fork()) == 0) {
        close(fd[2]);
        c0 = a*b;
        sprintf(char0, "%d", c0);
        write(fd[0], char0, 10);
        close(fd[0]);
        exit(0);
    }

    // child1
    if((child1 = fork()) == 0) {
        close(fd[2]);
        c1 = b*c;
        sprintf(char1, "%d", c1);
        write(fd[1], char1, 10);
        close(fd[1]);
        exit(0);
    }

    // overview
    if((overview = fork()) == 0) {
        close(fd[0]);
        close(fd[1]);
        read(fd[2], char0, 10);
        read(fd[2], char1, 10);
        printf("%s %s", char0, char1); //Prints weird stuff??
        close(fd[2]);
        exit(0);
    }

    // Wait for children to finish
    waitpid(child0, &status, 0);
    waitpid(child1, &status, 0);
    waitpid(overview, &status, 0);
    exit(0);
}
user2837858
  • 339
  • 6
  • 18

1 Answers1

1

Your code for declaring pipe is totally wrong, pipes will have only two ends and for declaring three pipes you need to declare as follows

pd1[2];
pd2[2];
pd3[2];

from one end you can write that is pd1[1];

and from the other end you can read pd1[0];

So your code will look like,

int main(int argc, char *argv[]) {
    int fd1[2];
    int fd2[2];
    int fd1[2];        
    int a, b, c, c0, c1, status;
    char char0[10];
    char char1[10];
    pid_t child0;
    pid_t child1;
    pid_t overview;

    // Set argv to ints
    a = atoi(argv[1]);
    b = atoi(argv[2]);
    c = atoi(argv[3]);

    // Pipe
    pipe(fd1);
    pipe(fd2);
    pipe(fd3);
    // child0
    if((child0 = fork()) == 0) {
        close(fd1[0]);
        c0 = a*b;
        sprintf(char0, "%d", c0);
        write(fd1[1], char0, 10);
        close(fd[1]);
        exit(0);
    }

    // child1
    if((child1 = fork()) == 0) {
        close(fd2[0]);
        c1 = b*c;
        sprintf(char1, "%d", c1);
        write(fd2[1], char1, 10);
        close(fd2[1]);
        exit(0);
    }

    // overview
    if((overview = fork()) == 0) {
        close(fd1[1]);
        close(fd2[1]);
        read(fd1[0], char0, 10);
        read(fd2[0], char1, 10);
        printf("%s %s", char0, char1); //Prints weird stuff??
        //close(fd[2]);
        exit(0);
    }

    // Wait for children to finish
    waitpid(child0, &status, 0);
    waitpid(child1, &status, 0);
    waitpid(overview, &status, 0);
    exit(0);
}

This code might also not correct I have just explained how to use pipes, see opening and closing of pipes, that is while writing reading end should be closed and while reading writing end should be closed.

edit

see this post and execute small program then modify your code step by step you will get understand.

How to send a simple string between two programs using pipes?

Community
  • 1
  • 1
smali
  • 4,687
  • 7
  • 38
  • 60
  • Still doesn't print out correctly after modifying my code. Do you know why? – user2837858 Sep 24 '14 at 09:46
  • what values you are passing as arguments and what output you are getting. and as I told cross verify that you are using correct descriptor for reading and writing – smali Sep 24 '14 at 09:48
  • I'm just passing in 1 2 3, so it would be `./a.out 1 2 3`. Expected output should be `2 6` but I am getting a random bunch of characters like `��M^�ēu� u�`. I've verified that the calculations actually work when debugging. It just never prints out correctly. – user2837858 Sep 24 '14 at 09:54