Pipe realizes brother Process communication

Source: Internet
Author: User


Pipe implements interprocess communication, first closes the read-in end of the first child process, and then closes the write end of the second child process
  
 
  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. }



From for notes (Wiz)

Pipe realizes brother Process communication

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.