将EOF写入管道 [英] Write an EOF to a pipe
本文介绍了将EOF写入管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个父母和一个孩子pro,想通过管道通过父母将EOF写给孩子。
I have a parent and a child prozess and want to write an EOF from the parent to the child via a pipe... how does this work?
在这里是我的附件:
---父代码---
---parent code---
if(dup2(parent_pipe[1], STDOUT_FILENO) == -1) { /*stdout gets closed and parent_pipe is duplicated with id of stdout*/
error("Can't duplicate the write-end of the pipe to stdout!");
}
if(close(parent_pipe[0]) == -1) {
error("Can't close read-end of the pipe!");
}
char blub[] = "EOF";
if(write(parent_pipe[1], blub, strlen(blub)) == -1) {
error("Failed to write the array");
}
-子代码---
if(dup2(parent_pipe[0], STDIN_FILENO) == -1) { /*stdin gets closed and parent_pipe is duplicated with id of stin*/
error("Can't duplicate the read-end of the pipe to stdin!");
}
/* close the write-end of the pipe */
if(close(parent_pipe[1]) == -1) {
error("Can't close write-end of the pipe");
}
while(fgets(readbuffer, ROWLENGTH, stdin) != NULL) {
printf("Received string: %s", readbuffer, nbytes);
}
孩子在等待EOF并没有停止,我该如何解决问题?
预先感谢
the child waits for the EOF and does not stop, how can i fix this problem? thanks in advance
推荐答案
要停止通信时,必须在父进程中关闭管道:
When you want to stop the communication, you must close the pipe in the parent process:
dup2(...);
...
write to child
...
close(parent_pipe[1]);
这篇关于将EOF写入管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文