0
我目前正在编写一个程序,它使用fork()创建一个子进程。这个子进程应该使用选项“-d”执行shell命令“uniq”,以便从stdin读取。在执行命令后,我想通过管道为“uniq”程序发送字符串作为标准输入。因此,在我将相同的字符串发送给对方后,字符串应打印在stdout上(请参阅uniq
手册页:https://linux.die.net/man/1/uniq)。但没有打印。为什么执行uniq -d的子进程不能打印通过管道传递给stdin的重复项?
这是到目前为止我的代码:
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;
}
}
所以我希望有在外壳印有“测试1”。我也想知道如何干净地终止uniq
过程。我认为写入可能会有问题,所以我必须在管道中每写入一个字符串后模拟一个“输入”。
您必须在等待'uniq'完成之前关闭管道,因为在关闭管道之前它不知道它已经获得了EOF。而且你甚至没有写过一行到'uniq'的标准输入。 'write()'系统调用写入了你要写入的内容,并且肯定不会添加任何自己的意愿的新行。 –
谢谢。在等待uniq进程之前,我编辑了我的帖子并关闭了管道。现在它终止。但直到现在我还不明白为什么没有写入uniq的标准输入。孩子的阅读结束被引导到标准输入。父母在管道中写入。对不起,我是这个主题的新手。 – hiaslosch17
你还没有写入'uniq'的整行输入;在你写的内容中没有新行,所以没有重复('uniq'看到'test1test1test2'没有换行符)。看到我的答案。 –