【问题标题】:grep: (standard input): Bad file descriptorgrep:(标准输入):错误的文件描述符
【发布时间】:2014-03-03 05:58:26
【问题描述】:

我正在尝试将标准输入更改为管道出口并将标准输出更改为另一个文件并使用 grep。我收到以下错误:

grep: (standard input): Bad file descriptor

我的代码是:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>

int main()
{
        pid_t childpid;
        int fds[2];
        int stat;
        int fd = open("temp", O_WRONLY | O_APPEND);
        int old_in = dup(STDIN_FILENO);
        int old_out = dup(STDOUT_FILENO);
        if ((childpid = fork()) != 0)
        {
                wait(&stat);
                close(fds[1]);
                dup2(fds[0], STDIN_FILENO);
                dup2(fd, STDOUT_FILENO);
                system("grep hello");
                dup2(old_in, STDIN_FILENO);
                dup2(old_out, STDOUT_FILENO);
                close(fds[0]);
                close(fd);
        }

        else if (childpid == 0)
        {
                close(fds[0]);
                write(fds[1], "hello how are you", 100);
                close(fds[1]);
                return 0;

        }

        else
        {
                fprintf(stderr, "ERROR:\n");
                exit(1);
        }
        return 0;
}

所以基本上我正在将一个字符串写入管道的写入出口,并使用管道的另一个出口(读取)作为 grep 的标准输入,并将标准输出输出到另一个文件。我已经从 childpid 中删除了返回,并尝试使用另一个文件作为标准输入,但仍然得到相同的错误。

【问题讨论】:

  • fork 之后的子分支中使用execvp 而不是system。阅读Advanced Linux Programming
  • 您似乎从未设置过fds 的值(例如,通过调用pipe(fds)。)此外,您确实应该检查系统调用的返回码,即使您没有'不要认为他们会失败。

标签: c linux unix fork pipe


【解决方案1】:

有多个问题!其中:

  • 你没有打电话给pipe(fds);,所以你没有管道。

  • 幼稚代码 (write(fds[1], "hello how are you", 100);)

    • 您应该在行尾添加一个换行符。
    • 您应该只写字符串中的尽可能多的字符,并且显示的字符串中没有 100 个字符。
  • 父母代号(if ((childpid = fork()) != 0)之后的块)

    • 您无需在system 之后恢复文件描述符,因为您即将退出。
    • 您可以使用exec*() 函数之一代替system()
    • 一般来说,您不应该等待孩子退出;在父级读取任何内容之前,可能有太多数据被推入管道,而子级无法完成。在这种情况下,应该没问题,但要小心。
    • 在打开额外文件描述符(old_inold_out)的情况下,您不应该真正执行 grep,但是当您在调用 system() 之后不恢复内容时,它们就会消失。
    • 您应该在复制 fds[0] 之后关闭它(在运行 grep 之前)。

工作代码

#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void)
{
    pid_t childpid;
    int fds[2];
    int fd = open("temp", O_WRONLY | O_APPEND);
    if (pipe(fds) != 0)
        return 1;
    if ((childpid = fork()) != 0)
    {
        close(fds[1]);
        dup2(fds[0], STDIN_FILENO);
        dup2(fd, STDOUT_FILENO);
        close(fds[0]);
        close(fd);
        execlp("grep", "grep", "hello", (char *)0);
        fprintf(stderr, "Failed to execute grep\n");
        return 1;
    }
    else if (childpid == 0)
    {
        const char data[] = "hello how are you\nvery well thanks\n";
        close(fds[0]);
        write(fds[1], data, sizeof(data)-1);
        close(fds[1]);
        return 0;
    }
    else
    {
        fprintf(stderr, "ERROR:\n");
        return 1;
    }
}

【讨论】:

  • 太棒了,我没有意识到我的代码有多丑(可能需要一些睡眠 :))。
猜你喜欢
  • 2019-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-05
  • 2020-05-05
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
相关资源
最近更新 更多