[What about Linux] About FIFO

Source: Internet
Author: User

Recently, we have been dealing with data communication between multiple processes (basically one server and multiple clients), and the communication data is video data, which is large in size and has been weighed repeatedly, decide to use FIFO for processing.

An exclusive FIFO queue is used between the server and each client, which causes blocking. However, you can add O_NONBLOCK to set it to a non-blocking status.

The following is a test program written by me, including the client and server, and a separate fifo famous pipeline between the server and each client.

Client:

 
 
  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <unistd.h> 
  4. #include <errno.h> 
  5. #include <stdio.h> 
  6. #include <stdlib.h> 
  7. #include <fcntl.h> 
  8. #include <limits.h> 
  9. #include <time.h> 
  10.  
  11. int main(int argc,char** argv){ 
  12.     int fd; 
  13.     int len; 
  14.     char buf[PIPE_BUF]; 
  15.     time_t tp; 
  16.     if(argc!=2){ 
  17.         printf("Usage:client [Name]"); 
  18.     } 
  19.     if((fd = open(argv[1],O_WRONLY))<0){ 
  20.         perror("open"); 
  21.         exit(EXIT_FAILURE); 
  22.     } 
  23.     while(1){ 
  24.         time(&tp); 
  25.         len = sprintf(buf,"wrfifo: %d sends %s",getpid(),ctime(&tp)); 
  26.         if((write(fd,buf,len+1))<0){ 
  27.             perror("write"); 
  28.             close(fd); 
  29.             exit(EXIT_FAILURE); 
  30.         } 
  31.     } 
  32.     close(fd); 
  33.     exit(EXIT_SUCCESS); 

Server:

 
 
  1. #include <sys/types.h> 
  2. #include <sys/stat.h> 
  3. #include <unistd.h> 
  4. #include <errno.h> 
  5. #include <stdio.h> 
  6. #include <stdlib.h> 
  7. #include <fcntl.h> 
  8. #include <limits.h> 
  9. //#define PIPE_BUF 8192 
  10. int main(void){ 
  11.     int fd1,fd2; 
  12.     int dumy1,dumy2; 
  13.     int len1,len2; 
  14.     char buf1[PIPE_BUF],buf2[PIPE_BUF]; 
  15.     mode_t mode = 0666; 
  16.     unlink("fifo1"); 
  17.     unlink("fifo2"); 
  18.     if((mkfifo("fifo1",mode))<0){ 
  19.         perror("mkfifo1"); 
  20.         exit(EXIT_FAILURE); 
  21.     } 
  22.     if((mkfifo("fifo2",mode))<0){ 
  23.         perror("mkfifo2"); 
  24.         exit(EXIT_FAILURE); 
  25.     } 
  26.     printf("open fifo1\n"); 
  27.     if((fd1=open("fifo1",O_RDONLY|O_NONBLOCK))<0){ 
  28. //  if((fd1=open("fifo1",O_RDONLY,0))<0){ 
  29.         perror("open1"); 
  30.         exit(EXIT_FAILURE); 
  31.     } 
  32.     printf("open fifo2\n"); 
  33.     if((fd2=open("fifo2",O_RDONLY|O_NONBLOCK))<0){ 
  34. //  if((fd2=open("fifo2",O_RDONLY,0))<0){ 
  35.         perror("open2"); 
  36.         exit(EXIT_FAILURE); 
  37.     } 
  38.     printf("loop\n"); 
  39.     while(1){ 
  40.         len1 = read(fd1,buf1,PIPE_BUF-1); 
  41.         if(len1>0) 
  42.             printf("rdfifo1 read: %s",buf1); 
  43.         len2 = read(fd2,buf2,PIPE_BUF-1); 
  44.         if(len2>0) 
  45.             printf("rdfifo2 read: %s",buf2);     
  46.     } 
  47.     close(fd1); 
  48.     close(fd2); 
  49.     printf("exit\n"); 
  50.     exit(EXIT_SUCCESS); 

Compile the above program

Run./server./client ikeo1./client ikeo2 on the three Super terminals respectively.

If the O_NONBLOCK parameter in the open parameter in server. c is removed, the server program will be blocked in the first open operation!

One disadvantage of FIFO is that the pipe name must be negotiated between the server and the client.

This article is from the Scalpel00 blog, please be sure to keep this source http://scalpel00.blog.51cto.com/1071749/278019

Related Article

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.