gpt4 book ai didi

c - 为什么执行 uniq -d 的子进程不打印管道通过标准输入传递的重复项?

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:21 25 4
gpt4 key购买 nike

我目前正在编写一个使用 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() 系统调用只写你告诉它写的东西;它当然不会根据自己的意愿添加任何换行符。

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

#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

关于c - 为什么执行 uniq -d 的子进程不打印管道通过标准输入传递的重复项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com