c - fork 和管道,我做错了什么?

标签 c unix pipe fork system-calls

我正在尝试了解管道和重定向。为此,我正在做一些小程序来习惯相关的系统调用。在这一个上,我尝试在文件 pipe4.c 上启动 cat,并将其输出通过管道传输到我之后启动的 grep。它不起作用,我不明白结果, 我虽然逻辑很好,但我显然用 fork 遗漏了一些东西。我做错了什么?

代码如下:

    #include <stdio.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #define BUFF_SIZE 4092

    //redirecting the output of a program ('cat' here)
    //to the stdin of another program ('grep' here)
    void    err_handler(char *msg)
    {
        perror(msg);
        exit(EXIT_FAILURE);
    }

    int main(void)
    {
        //creating a pipe to communicate between the child processes
        int p[2];
        if (pipe(p) < 0)
            err_handler("pipe error: ");
        /*
        ** forking for cat
        */
        pid_t cat_pid;
        if ((cat_pid = fork()) < 0)
            err_handler("fork error: ");
        if (cat_pid)
            close(p[1]);
        if (!cat_pid) {
            printf("===CAT===\n");
            dup2(p[1], STDOUT_FILENO);
            close(p[0]);
            close(p[1]);
            execl("/bin/cat", "cat", "pipe4.c", NULL);
            exit(EXIT_SUCCESS);
        }
        /*
        ** forking for grep
        */
        pid_t grep_pid;
        if ((grep_pid = fork()) < 0)
            err_handler("fork_error: ");
        if (grep_pid)
            wait(&grep_pid);
        if (!grep_pid) {
            printf("===GREP===\n");
            dup2(p[0], STDIN_FILENO);
            close(p[0]);
            close(p[1]);
            execl("/bin/grep", "grep", "\"err_handler\"", NULL);
            exit(EXIT_SUCCESS);
        }
        return 0;

}

我只在我的终端上得到这个:

> pom@parrot ~/dev/19/syscall> sudo ./a.out 
> ===GREP===
> ===CAT===
> ===GREP===

每次执行时,这些行的打印顺序都会发生变化。 我期望的显然是我的 pipe4.c 文件中包含“err_handler”的所有行,就像我直接在 shell 中做的那样:

> pom@parrot ~/dev/19/syscall> cat pipe4.c | grep "err_handler"
> void  err_handler(char *msg)      err_handler("pipe error: ");
>       err_handler("fork error: ");            err_handler("creat error: ");
>               err_handler("read error: ");
>               err_handler("write error:");

最佳答案

有(我认为!)3 个主要问题。

1) 您没有正确使用等待。我建议改用 waitpid。

2) 至少在我的系统上,/bin/grep 不存在。它是/usr/bin/grep。但是由于当 exec 失败时您返回 EXIT_SUCCESS,所以您没有看到错误。您应该替换 execl (...);退出(EXIT_SUCCESS),使用execl(...);错误(“执行”);退出(EXIT_FAILURE);

3) 您正在搜索字面引号。就好像你跑了:

grep '"err_handler"'

也就是说,当您生成 grep 时,execl 的参数应该是 "err_handler",而不是 "\"err_handler\""

关于c - fork 和管道,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384292/

相关文章:

c - void 表达式中的未定义行为

c - 返回 float 的指数值

macos - 带有命令行输入重定向的 NSTask

linux - 使用 wget 指定下载文件名而不覆盖

unix - 如何使用argv使用AWK打印文本文件的第N列

c++ - 从 stdin 读取时子进程挂起(fork/dup2 竞争条件)?

c - 使用两个单向管道的双向通信

git - 防止 PowerShell "boxing"程序输出

c - 结构体指针作为参数并返回

c - 内联 asm 代码组织