我的管道代码有什么问题?

时间:2016-10-09 23:42:15

标签: c shell

我正在尝试为自定义C shell创建一个可以运行cat example.txt | -wc之类的管道。

但是现在当我运行它时,我会在命令行上无限循环shell提示符。

有人可以告诉我,我是如何实施滚边的吗?

#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **args){
    pid_t pid,pid2, wpid;
    int pipefd[2];
    int status;
    pipe(pipefd);
    pid=fork();
    if(pid==0)
    {
        pid2=fork();
        if(pid2==0)
        {
            dup2(pipefd[0],0);
            close(pipefd[1]);
            if(execvp(args[1],args)==-1)
            {
                perror("Shell Error");
            }
        }
        else
        {
            dup2(pipefd[1],1);
            close(pipefd[0]);
            if(execvp(args[4],args)==-1)
            {
                perror("Shell Error");
            }
        }
    }
    else
    {
        //Return to Shell
    }
    close(pipefd[0]);
    close(pipefd[1]);

}

0 个答案:

没有答案