如何在C中使用多个管道在Linux中运行命令?

时间:2013-10-17 09:41:48

标签: c linux pipe

我正在编写一个C程序,它运行以下带有多个管道的Linux命令:

cat myfile1.txt | egrep计算| wc -l> MYFILE

我的代码如下:

    int p_fd[2];
    pid_t childpid, waitReturn;
    int pid=1;
    int status, i;

    pipe(p_fd);

    for( i = 1 ; i < 3 ; i++ )
        if( childpid = fork() )
            break;
        else
            pid++;

    while( childpid != ( waitReturn = wait( &status ) ) )
        if( ( waitReturn == -1 ) && ( errno != EINTR ) )
            break;

    if ( childpid > 0 && pid == 1 ){
        printf("%d\n", pid);
        int fd;
        if ( ( fd= open("myfile", O_CREAT|O_RDWR|O_TRUNC, 00644)) == -1 )
        {
                printf("Error: Cannot open file in open()\n");
                exit(1);
        }
        close(0);
        dup(p_fd[0]);
        close(1);
        dup(fd);
        close(p_fd[0]);
        close(p_fd[1]);
        close(fd);
        execl("/bin/wc", "wc", "-l", NULL);
    }else if( childpid > 0 && pid == 2 ){
        printf("%d\n", pid);
        close(0);
        dup(p_fd[0]);
        close(1);
        dup(p_fd[1]);
        close(p_fd[0]);
        close(p_fd[1]);

        execl("/bin/egrep", "egrep", "Computing", NULL);

    }else if( childpid == 0 && pid == 3 ){
        printf("%d\n", pid);
        close(1);
        dup(p_fd[1]);
        close(p_fd[0]);
        close(p_fd[1]);

        execl("/bin/cat", "cat", "myfile1.txt", NULL);
    }   

    return 0;

然而,我的程序在达到“execl(”/ bin / egrep“,”egrep“,”Computing“,NULL);”时会挂起,“在带有pid 2的第二个孩子中调用。

我不知道我的程序挂在那里的原因;这是关于管道的僵局吗?

有人可以帮我修改上述程序,以便它能给我想要的结果吗?

1 个答案:

答案 0 :(得分:1)

如下所示的伪代码:

create_pipe_between_cat_and_egrep();
create_pipe_between_egrep_and_wc();
create_destination_file();

if (fork() == 0)
{
    /* Process for cat */
    setup_pipe_stdout_for_cat();
    execl("cat", "cat", "arguments");
}

if (fork() == 0)
{
    /* process for egrep */
    setup_pipe_stdin_stdout_for_egrep();
    execl("egrep", "egrep", "arguments");
}

if (fork() == 0)
{
    /* process for wc */
    setup_pipe_stdin_for_wc();
    setup_file_stdout_for_wc();
    execl("wc", "wc", "arguments");
}

wait_for_all_three_child_processes_to_finish();

使用上面三个不同的块来跟踪流程更容易,而不是循环。

许多代码都可以放在通用函数中,比如为子进程设置stdin / stdout描述符。