子进程之间的 UNIX 管道 [英] UNIX Pipes Between Child Processes

查看:25
本文介绍了子进程之间的 UNIX 管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

int main (int argc, const char * argv[]) {int fd[2];管道(FD);chdir("/directory/with/lots/of/files");//为多个创建一个子进程int pid = fork();如果(pid == 0){关闭(FD [1]);int ret = dup2(fd[0],0);if (ret <0) perror("dup2");字符 *argv[10];argv[0] = "更多";argv[1] = NULL;execvp("更多", argv);}//为ls创建另一个子进程int pid2 = fork();如果(pid2 == 0){int ret = dup2(fd[1],1);if (ret <0) perror("dup2");字符 *argv[10];argv[0] = "ls";argv[1] = "-l";argv[2] = NULL;execvp("ls", argv);}//等待更多进程完成内部状态;waitpid(pid, &status, 0);printf("完成!
");返回0;}

现在,当我执行程序(当然包含在 main() 函数中)时,我得到的结果更多,这是意料之中的.我会点击d"向下翻页更多的输出,u"向上翻,它似乎工作正常.但是当我到达底部时,它不会像 more 那样退出,而是只留下一个空行.Ctrl-C 可以退出它,但它会退出整个程序,意思是完成!"线永远不会被打印.此处 提供了一部影片,说明了所发生的情况(请注意,最后我按 Ctrl-C 返回 bash).>

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

解决方案

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

<代码> ...//关闭父管道关闭(FD [0]);关闭(FD [1]);//等待更多进程完成内部状态;waitpid(pid, &status, 0);printf("完成!
");返回0;}

I'm trying to write a program that will spawn an arbitrary number of child processes and pipe between them, similar to a command line pipeline. In my case I'm trying to do "ls -l | more" and output that to stdout, then have the parent continue executing more commands.

I have the following code as a minimal example:

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!
");
    return 0;
}

Now, when I execute the program (enclosed in a main() function of course) what I end up with is more, which is expected. I'll hit "d" to page down more's output and "u" to go up, and it seems to work fine. But when I reach the bottom, instead of exiting like more does, it just leaves a blank line. Ctrl-C works to exit it but it exits the entire program, meaning the "Done!" line never gets printed. A movie is available here that illustrates what happens (note that at the very end I press Ctrl-C to get back to bash).

Any thoughts on this? I'm just trying to figure out how to change it to where instead of going to a blank line after more reaches the bottom, more quits and returns to the parent process so it can continue executing.

解决方案

You need to close() at least the writing end of your pipe, otherwise more will never see EOF. For example:

    ...

    // 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!
");
    return 0;
}

这篇关于子进程之间的 UNIX 管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆