开发者

Pipe() fork() and exec

I try to add pipe in a mini-shell. I'm confused, when I type ls | sort, nothing is displayed, I don't understand why :

int fd[2];
if (tube == 1){

    int pipeling = pipe(fd);
    if (pipeling == -1){
        perror("pipe") ;
    }
}


    tmp = fork();               //FORK A

    if (tmp < 0){
        perror("fork");
        continue;
    }

    if (tmp != 0) {                 //parent
                while(wait(0) != tmp) ;
                continue ;
    }

    if (tube == 1) {                //there is a pipe

    if (tmp != 0){                  //parent A
        close(fd[1]);
    }

        if (tmp == 0){              //Child A                     
            close(fd[0]);
            dup2(fd[1], 1);
            close(fd[1]);
            execv(mot[0], mot);
    }

    int tmp2 = fork() ;       开发者_Python百科      //FORK B

    if (tmp2 != 0) {                //Parent B
        close(fd[0]);
        while(wait(0) != tmp2) ;
        continue ;
    }

    if (tmp2 == 0){                 //Child B
        close(fd[1]);
        dup2(fd[0], 0);
        close(fd[0]);
        execvp(mot[1], mot);

    }
}

I've read all topics about that but it doesn't work. Can you help me ?

Edit : the second code, I try to change the structure.

thanks.


The second fork is not going to be reached in case execvp succeeds, because the latter should replace the image of the process and will stop executing the current code.

You have to restructure your program.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜