FIFO for client server-side communication

Source: Internet
Author: User


FIFO solves the problem of generating a large number of temporary files when the process communicates, and can achieve communication between non-consanguinity processes, and can be reserved for later process use.
The read and write rules for FIFO are similar to those for anonymous pipelines, butFIFO is saved on disk, while anonymous pipes are stored in memory.
When the FIFO write process is closed, a file terminator is sent to the FIFO read process.
Client:

  
 
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/stat.h>
  4. #include<sys/types.h>
  5. #include<fcntl.h>
  6. #include<string.h>
  7. int main()
  8. {
  9. char* msg = "Hello, i am client!\n";
  10. int fd = open("./fifo1",O_WRONLY);
  11. int i = 0;
  12. for(;i<10;i++)
  13. {
  14. printf(msg);
  15. if(write(fd,msg,strlen(msg)+1)<0)
  16. {
  17. _exit(-1);
  18. }
  19. sleep(2);
  20. }
  21. close(fd);
  22. return 0;
  23. }


Server-side:
  
 
  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<sys/stat.h>
  4. #include<sys/types.h>
  5. #include<fcntl.h>
  6. #include<string.h>
  7. int main()
  8. {
  9. int fd = open("./fifo1",O_RDONLY);
  10. int i = 0;
  11. char buf[24]={0};
  12. while(read(fd,buf,24)>0)
  13. {
  14. printf(buf);
  15. }
  16. close(fd);
  17. return 0;
  18. }




From for notes (Wiz)

FIFO for client server-side 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.