【问题标题】:Why does the child process executing uniq -d not print duplicates passed by pipe over stdin?为什么执行 uniq -d 的子进程不打印通过管道通过标准输入传递的重复项?
【发布时间】:2016-11-20 19:46:33
【问题描述】:

我目前正在编写一个使用 fork() 创建子进程的程序。该子进程应执行带有选项“-d”的 shell 命令“uniq”,以便从标准输入读取。在执行命令之后,我想通过“uniq”程序的管道将字符串作为标准输入发送。所以在我发送相同的字符串后,字符串应该打印在标准输出上(参见uniq 手册页:https://linux.die.net/man/1/uniq)。但是什么都没有打印。

到目前为止,这是我的代码:

void execute_uniq_process()
{
    //create pipe
    int pipefd[2];
    if(pipe(pipefd)<0)
    {
        //error handling
    }

    //create argv for command:
    char *argv[] = {"uniq","-d",(char *)0};

    //create child process
    pid_t pid = fork();
    int status;

    switch(pid)
    {
        case -1:
        //error handling
        break;

        case 0:
            //child process
            //close writing end in pipe
            close(pipefd[1]);

            //duplicate stdin
            dup2(pipefd[0],STDIN_FILENO);
            close(pipefd[0]);

            execvp("uniq",argv);
            exit(errno);
        break;

        default:
            //parent process
            //close reading end in pipe
            close(pipefd[0]);

            //write all commands to pipe
            write(pipefd[1],"test1",5);
            write(pipefd[1],"test1",5);
            write(pipefd[1],"test2",5);
            //edited:
            close(pipefd[1]);

            //waits till child is finished
            wait(&status);

            if(WEXITSTATUS(status) != EXIT_SUCCESS)
            {
                //error handling
            }
        break;
    } 
}

所以我希望在 shell 中打印“test1”。我也想知道如何干净地终止uniq 进程。我认为写入可能存在问题,因此我必须在我在管道中写入的每个字符串之后模拟一个“输入”。

【问题讨论】:

  • 您必须在等待uniq 完成之前关闭管道,因为在您关闭管道之前它不会知道它有 EOF。而且您甚至没有在uniq 的标准输入中写一行。 write() 系统调用会写你告诉它写的东西,当然不会添加任何它自己意志的换行符。
  • 谢谢。在等待 uniq - 进程之前,我编辑了我的帖子并关闭了管道。现在它终止了。但直到现在我才明白为什么没有任何东西写入 uniq 的标准输入。孩子的阅读端被定向到标准输入。父母在管道中写入。对不起,我对这个话题不熟悉。
  • 你还没有写一整行输入到uniq;你写的东西没有换行符,所以没有重复(uniq 看到 test1test1test2 没有换行符)。看我的回答。
  • 现在可以了,谢谢。

标签: c linux pipe fork uniq


【解决方案1】:

您必须在等待uniq 完成之前关闭管道,因为在您关闭管道之前它不会知道它有 EOF。而且您甚至没有在uniq 的标准输入中写入一行,因为写入的数据中没有换行符。 write() 系统调用只写你告诉它写的东西;它当然不会自行添加任何换行符。

这些变化和各种琐事导致:

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void)
{
    int pipefd[2];
    if (pipe(pipefd) < 0)
    {
        fprintf(stderr, "Failed to create pipe\n");
        return 1;
    }

    char *argv[] = {"uniq", "-d", (char *)0};

    pid_t pid = fork();

    switch (pid)
    {
    case -1:
        fprintf(stderr, "Failed to fork\n");
        return 1;

    case 0:
        close(pipefd[1]);
        dup2(pipefd[0], STDIN_FILENO);
        close(pipefd[0]);
        execvp(argv[0], argv);
        fprintf(stderr, "Failed to exec %s\n", argv[0]);
        exit(errno);

    default:
        close(pipefd[0]);
        write(pipefd[1], "test1\n", 6);
        write(pipefd[1], "test1\n", 6);
        write(pipefd[1], "test2\n", 6);
        close(pipefd[1]);
        int status;
        int corpse = wait(&status);

        if (WEXITSTATUS(status) != EXIT_SUCCESS)
        {
            fprintf(stderr, "Child (%s) exited (PID %d, status 0x%.4X)\n", argv[0], corpse, status);
        }
        break;
    }
    return 0;
}

这会在运行时输出test1

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-05
    • 2014-07-10
    • 2023-03-23
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多