【发布时间】: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没有换行符)。看我的回答。 -
现在可以了,谢谢。