在下面的程序(不是我的程序,但是我修改过的程序)中,子进程对管道进行了两次写入.当我运行该程序时,我得到以下输出:
Received string: Hello, world!
This is the child process.
Run Code Online (Sandbox Code Playgroud)
如何由父进程执行的读取从管道缓冲区中捕获这两个字符串?什么(如果有的话)阻止父进程在读取第一个字符串(或第一个字符串的第一个字符串)之后假设没有其他内容可以从缓冲区中读取并退出?
有问题的计划:
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char string2[] = "This is the child process.\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
return 1;
}
if(childpid == 0)
{
/* Child process closes pipe's input file descriptor */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)));
write(fd[1], string2, (strlen(string2)+1));
return 1; …Run Code Online (Sandbox Code Playgroud)