1
我有一個父母和一個孩子prozess,並希望通過管道從父母寫一個EOF到孩子......這是如何工作的?寫一個EOF到管道
這裏是我的attampt:
---父代碼---
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不停止,我該如何解決這個問題? 由於事先
'如果(寫(parent_pipe [1],泡殼,1 + strlen的(泡殼) )== -1){'請注意,blub []沒有'\ n',因此fgets將阻塞,直到管道實際上關閉。 – wildplasser