父子进程之间管道通信问题
看不懂这个程序输出的结果,求解释:
怎么会输出2个输入字符串(test message!)呢?
源代码:
esperantor@ubuntu:~/source$ cat noname_pipe_example.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv)
{
pid_t pid;
int temp;
int pipedes[2];
char s[13] = "test message!";
char d[13];
if ((pipe(pipedes))== -1)
{
perror("pipe");
exit(EXIT_FAILURE);
}
if ((pid=fork()) == -1)
{
perror("fork");
exit(EXIT_FAILURE);
}
else if(pid==0)
{
printf("now ,write data to pipe\n");
if ((write(pipedes[1],s,13)) == -1)
{
perror("write");
exit(EXIT_FAILURE);
}
else
{
printf("the written data is :%s\n",s);
exit(EXIT_SUCCESS);
}
}
else if (pid >0)
{
sleep(2);
printf("now,read data from pipe\n");
if ((read(pipedes[0],d,13))==-1)
{
perror("read");
exit(EXIT_FAILURE);
}
printf("the data from pipe is :%s\n",d);
}
return 0;
}
结果:
esperantor@ubuntu:~/source$ gcc -o noname_pipe_example noname_pipe_example.c
esperantor@ubuntu:~/source$ ./noname_pipe_example
now ,write data to pipe
the written data is :test message!
now,read data from pipe
the data from pipe is :test message!test message!
怎么会输出2个输入字符串(test message!)呢?
...全文
相关推荐
详解从Node.js的child_process模块来学习父子进程之间的通信
主要介绍了从Node.js的child_process模块来学习父子进程之间的通信,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
Linux进程间通信之管道
Linux进程间通信之管道.适用于亲缘关系间的两个进程间,本程序为基本模型,实现了父子进程间的管道通信.
操作系统实验四 进程的管道通信 实验报告
编写一段程序,实现进程的管道通信。 使用系统调用pipe( )建立一条管道,创建两个子进程P1和P2。让P1和P2分别向管道各写一句话: child process P1 is sending messages! child process P2 is sending messages! 父进程则从管道中读出来自于两个子进程的信息,显示在屏幕上。
父子进程,createprocess
子进程一直在输出“child process is talking at【system time】” 父进程一直在输出“parent process is····” 由两个窗口分别显示
anonymousPipe(实用匿名管道实现进程之间的通信)
实用匿名管道实现进程之间的通信,程序的源代码


