0

我正在尝试创建一个简单的程序来模拟终端中的“ls -l | tail -n 2”调用。为此,我正在使用“fork”和“execvp”。好吧,这里是代码:

int main(int argc, char *argv[])
{
    int pipefd[2];
    pid_t child1;
    pid_t child2;
    char* com1[] = {"ls", "-l",NULL};
    char* com2[] = {"tail", "-n","2",NULL};

    if (!(child1 = fork())) 
    { 
       close(STDOUT);
       dup(pipefd[1]); 
       close(pipefd[1]); 
       execvp (com1[0], com1);
       _exit(EXIT_SUCCESS);
    }
    else
    {
        close(pipefd[1]);
        if (!(child2 = fork())) 
        { 
            close(STDIN);
            dup(pipefd[0]); /* dupWR now holds the lowest fd available, meaning STDOUT's */
            perror("dup 2");
            close(pipefd[0]); /* reader will see EOF */
            execvp (com2[0], com2);
            _exit(EXIT_SUCCESS);
        }
        else
        {
            close(pipefd[0]);
            waitpid(child2,0,0);
        }
        waitpid(child1,0,0);
    }


    return 0;
}

我收到这些错误:

dup 2: Bad file descriptor
tail: cannot fstat `standard input': Bad file descriptor
tail: -: Bad file descriptor

在我看来,同步存在问题。事实上,如果我声明: com2[] = {"ls", "-l",NULL}; 它工作正常(我的意思是在普通外壳中)。此外,我发现第二个“fork”中的第二个“dup”返回错误。这是为什么?我不知道这段代码的问题在哪里。请帮忙!

编辑:我添加了这段代码(忘记创建管道):

if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(EXIT_FAILURE);
}

谢谢,没用!

4

1 回答 1

1
close(STDOUT);
dup(pipefd[1]); 
close(pipefd[1]); 

由于dup 返回新的文件描述符,并且您不使用返回值,因此您将其丢弃。

你想像这样替换标准输出吗?

dup2(pipefd[1], STDOUT_FILENO);

如果是这样,pipefd[]真的应该先初始化。你的意思是打电话给pipe某个地方吗?

于 2013-05-19T22:25:24.997 回答