samedi 25 avril 2015

Pipeline idea not working due to pointers (C)

I was working on a little program I had to make some time ago, and I wanted to short it up.

This little program was about one thread creating several child who would redirect their standard output/input one to another with pipes in order, except for the last child, who won't redirect it's standard output, like this.

Parent pipe  child1   pipe   child2  pipe   last child
       __             __             __
O-----|__|-----O-----|__|-----O-----|__|-----O -> Stdout

First time I face this code, I made a matrix with a dimension of [n_child][2], and made a pipe from every position of that matrix, so it was very easy to connect every pipe to every child when needed. But now I want to try it only with 2 pipes, and "playing" with inheritance.

Maybe I'm not explaining myself really well, so I think everything would be better understood with my code,so here we go.

piping_function(int number_of_child){

    int i;
    int olddesc[2];
    int newdesc[2]; //Here I create the descriptors of the pipes I'll use
    int *olddir;
    int *newdir;
    if(pipe(olddesc)<0 || pipe(newdesc) < 0){  //I create the pipes
        //Error 
    }
    olddir = olddesc;
    newdir = newdesc; //And attach the directions to the array's direction (we will see later why)
    for(i = 0; i < number_of_child; i++){
        chaddr[i] = fork();
        switch(chaddr[i]){
            case -1:
                //Error trace
            case 0:
                dup2(olddesc[0],0); //Here I redirect the pipe who connect the previous child's pipe to the standard input
                if(i != number_of_child - 1)
                    dup2(newdesc[1],1); //And here, except from the last child, I redirect the standard output to the pipe who will connect to the standard input of the next child
                close(olddesc[0]);
                close(newdesc[1]); //I close the descriptors I don't need
                //Several child operations with standard input-output (end up returning 0/1 after the pipeline is connected, so no child will create any child)
            default:
                if(i == 0)
                    dup2(olddesc[1], 1); //I want the standard output of the principal proccess only on the first pipe
                olddir = newdir; //Here I would want the direction of the "old" pipe to be the direction of the "new" pipe, in order to achieve the pipeline
                if(pipe(newdesc)<0)
                    //Error
                break;
        }//End of switch
    }//End of for
    close(olddesc[0]);close(olddesc[1]);close(newdesc[0]);close(newdesc[1]); //I don't need these descriptors anymore, as they must be redirected to the standard's input/output of the process they need.
}//End of function

Well, there is my code. I think I can see the mistake I'm doing, when I make olddir be newdir, and create the pipe, I'm doing olddir to be also that new pipe right? So here comes my question:

Is there any way to achieve that change? I mean, the thing I want is to equal olddir (who is the address of olddesc, right? so if I change that address olddesc's address will be also changed, right?) to newdir, in order to continue with the pipe I created before to redirect the standard output of the "next" child, but I also want newdir to be a NEW pipe.

I don't really know if I explained myself right, I'm not a native speaker and it's a bit difficult to explain these kind of ideas in other language. Feel free to correct any grammar mistake, I'd appreciate it, and to ask any question about the code, as maybe I'm not giving the point I wanted to.

Thanks.

Aucun commentaire:

Enregistrer un commentaire