首页 > 系统服务 > 详细

pipe实现兄弟进程通信

时间:2015-06-08 19:30:30      阅读:143      评论:0      收藏:0      [点我收藏+]

pipe实现进程间通信,首先关闭第一个子进程的读入端,然后关闭第二个子进程的写入端

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<unistd.h>
  4. int main()
  5. {
  6. int fd[2];
  7. pipe(fd);
  8. pid_t pid = fork();
  9. if(pid==0)
  10. {
  11. close(fd[0]);
  12. write(fd[1],"Hello",6);
  13. exit(0);
  14. close(fd[1]);
  15. }
  16. pid_t pid2 = fork();
  17. char buf[6]={0};
  18. if(pid2==0)
  19. {
  20. close(fd[1]);
  21. read(fd[0],buf,6);
  22. printf(buf);
  23. close(fd[0]);
  24. exit(0);
  25. }
  26. else if(pid2>0)
  27. {
  28. close(fd[0]);
  29. close(fd[1]);
  30. exit(0);
  31. }
  32. }





pipe实现兄弟进程通信

原文:http://www.cnblogs.com/ZhangJinkun/p/4561661.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!