子进程之间的UNIX管道

Jus*_*kva 7 c shell pipe process exec

我正在尝试编写一个程序,它将在它们之间产生任意数量的子进程和管道,类似于命令行管道.在我的情况下,我正在尝试"ls -l | more"并将其输出到stdout,然后让父级继续执行更多命令.

我将以下代码作为最小示例:

int main (int argc, const char * argv[]) {
    int fd[2];
    pipe(fd); 
    chdir("/directory/with/lots/of/files");

    // Create one child process for more
    int pid = fork();
    if (pid == 0) {
        close(fd[1]);
        int ret = dup2(fd[0],0);
        if (ret < 0) perror("dup2");
        char *argv[10];
        argv[0] = "more";  argv[1] = NULL;
        execvp("more", argv);
    } 
    // Create another child process for ls
    int pid2 = fork();
    if (pid2 == 0) {
        int ret = dup2(fd[1],1);
        if (ret < 0) perror("dup2");
        char *argv[10];
        argv[0] = "ls";    argv[1] = "-l";   
        argv[2] = NULL;
        execvp("ls", argv);
    }

    // wait for the more process to finish
    int status;
    waitpid(pid, &status, 0);

    printf("Done!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我执行程序时(当然包含在main()函数中)我最终得到的更多,这是预期的.我会点击"d"来减少更多的输出和"你"上升,它似乎工作正常.但是当我到达底部,而不是像更多的那样退出时,它只留下一个空白.Ctrl-C用于退出它,但退出整个程序,意味着"完成!" 线从未打印过.这里有一部电影可以说明会发生什么(请注意,最后我按Ctrl-C返回bash).

有什么想法吗?我只想弄清楚如何将其更改为位置,而不是在更多到达底部之后转到空行,更多退出并返回到父进程,以便它可以继续执行.

Rob*_*sak 9

你需要close()至少管道的写入端,否则more永远不会看到EOF.例如:

    ...

    // close parent's pipes
    close(fd[0]);
    close(fd[1]);

    // wait for the more process to finish
    int status;
    waitpid(pid, &status, 0);

    printf("Done!\n");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)