FIFO file named pipes

Named pipes  ie FIFO files, you can through named pipesbetween unrelated processesto exchange data. FIFO has path name associated therewith, in aspecial file format deviceexists in the file system

FIFO has two purposes:

   (. 1) by the FIFOshellis transferred from a pipe line to another to use the data for thiswithout creating temporary file.
   (2) FIFO forthe client process - server processprogram has been to transfer data between the client and server processes.

FIFO read and write rules:

read data from the FIFO:

1. If there is a process to write open FIFO, and is currently in the FIFO is no data , is set for blocking flag is read , it will remain blocked .
  Blocking flag is not set for a read operation , it returns -1, after the current value EAGAIN errno, alert again.

2. For the set blocking flag is read , the cause reason for blocking two ways: one is the current FIFO data , but there are other processes are reading the data ; the other is the internal FIFO is no data , blocking reasons there is a FIFO write new data, regardless of the amount of data written to the new size, and regardless of how much data read operation request ( writing ).

3. Reading open the blocked mark only this process first read acted upon, if there is more than reading the sequence of operations in this process, then the first wake up and read operations after the completion of the read operation , the other read operation to be performedWill not be blocked even when the read operation is performed, there is no data in the FIFO is the same (in this case, the read operation returns to 0)

4. If there is no process to write open FIFO, then set the blocking flag read operation will block

5. If the FIFO in the data , is set blocking flag read operations will not be in the FIFO is the number of bytes is smaller than the requested number of bytes read is blocked , this time, the read operation will return the FIFO existing amount of data .

Writing data to the FIFO :

to set the blocking flag write operation:

1. When to write data amount not larger than PIPE_BUF time, Linux will guarantee the atomicity of write . If at this time the pipe free buffer is not enough to accommodate the number of bytes to write, then enter sleep , to know when to accommodate the number of bytes in the buffer to be written, a write-once before starting the operation.

2. When to write data is larger than when PIPE_BUF, Linux will not guarantee the atomicity of write . FIFO buffer has a free area, the writing process will attempt to write data to the pipe, writes to return after writing all requests to write data.

Blocking flag is not set for write operations :

1. When the amount of data to be written at the time PIPE_BUF, Linux will not guarantee the atomicity of write. After all again filled with idle FIFO buffer, the write operation returns.

2. When the amount of data to be written is not larger than PIPE_BUF, Linux will ensure atomicity of write. If the current idle write FIFO buffer can not accommodate the number of bytes requested to be written, EAGAIN error is returned, after the alert; if the current idle FIFO buffer can hold the number of bytes requested to be written, after finishing successful return.

Some of the FIFO attention to the problem: 

FIFO treatment (1) of the data pipe 

      Head first into the data pipeline, the first port to be read 

Irreproducibility (2) of the data pipe 

      It has read the data in the pipeline disappear , can not read 

Limitation (3) length of the pipe 

(. 4) SIGPIPE signal  if a process attempts to write to a no read process pipeline, the kernel generates a SIGPIPE 

Example 1:

#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#define FIFO "/tmp/my_fifo"
main(int argc,char ** argv){
   char buf_r[100];
   int fd;
   int nread;
   if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))     //O_EXCL:可执行
     printf("cannot create FIFOserver\n");
   printf("Preparing for reading bytes...\n");
   memset(buf_r,0,sizeof(buf_r));
   fd=open(FIFO,O_RDONLY|O_NONBLOCK,0); // O_RDONLY: read-only, O_NONBLOCK: nonblocking
   if(fd==-1){
     perror("open");
     exit(1);
   }
   while(1){
     memset(buf_r,0,sizeof(buf_r));
     if((nread=read(fd,buf_r,100))==-1){
       if(errno==EAGAIN)
         printf("no data yet\n");
     }
     printf("read %s from FIFO\n",buf_r);
     sleep(1);
   }
   pause();
   unlink(FIFO);
}

Example 2:

Two programs were written, is a server program, the client continuously reads the information sent from the duct;
the other client, and input information sent from the command line in the pipeline: 

Client (write pipeline) 

  1. /*fifo_write.c*/
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. / * The FIFO pipeline path * /
  10. #define FIFO_SERVER "/tmp/myfifo"
  11. main(int argc,char** argv)  
  12. {  
  13. int fd = 0;  
  14. char w_buf[100];  
  15. int nwrite;  
  16. / * Open a FIFO * /
  17.    fd=open(FIFO_SERVER,O_WRONLY|O_NONBLOCK,0);  
  18. if(fd==-1)  
  19. if(errno==ENXIO)  
  20.        printf("open error; no reading process\n");  
  21. / * Parameter is determined there is no input * /
  22. if(argc==1)  
  23.      printf("Please send something\n");  
  24. / * Copy input parameter * /
  25.    strcpy(w_buf,argv[1]);  
  26. / * * Writes to FIFO /
  27. if((nwrite=write(fd,w_buf,100))==-1)  
  28.    {  
  29. if(errno==EAGAIN)  
  30.        printf("The FIFO has not been read yet.Please try later\n");  
  31.    }  
  32. else
  33. / * Write the contents of the output * /
  34.      printf("write %s to the FIFO\n",w_buf);  
  35. }  

Service routine (read pipeline) 

  /*fifo_read.c*/

  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. / * * Path defined FIFO /
  9. #define FIFO "/tmp/myfifo"
  10. main(int argc,char** argv)  
  11. {  
  12. char buf_r[100];  
  13. int  fd;  
  14. int  nread;  
  15. / * Create a FIFO * /
  16. if((mkfifo(FIFO,O_CREAT|O_EXCL)<0)&&(errno!=EEXIST))  
  17.      printf("cannot create fifoserver\n");  
  18. printf("Preparing for reading bytes...\n");  
  19. memset(buf_r,0,sizeof(buf_r));  
  20. / * Open a FIFO, do not block the way * /
  21.    fd=open(FIFO,O_RDONLY|O_NONBLOCK,0);  
  22. if(fd==-1)  
  23.    {  
  24.      perror("open");  
  25.      exit(1);    
  26.    }  
  27. while(1)  
  28.    {  
  29.      memset(buf_r,0,sizeof(buf_r));  
  30. / * Read the pipeline, because the definition of a non-blocking, so in this process does not block * /
  31.      if((nread=read(fd,buf_r,100))==-1){  
  32.         if(errno==EAGAIN)  printf("no data yet\n");  
  33.      }  
  34.      printf("read %s from FIFO\n",buf_r);  
  35.      sleep(1);  
  36.    }    
  37.    pause();  
  38.    unlink(FIFO);  
  39. }  
  40.  

Then compile, compiled after a good run two terminals in Linux, run more than two programs separately, you can see, when you run fifo_read, the program has been read in every second, and then we enter another terminal: 

$ ./fifo_write helloworld

It can be seen fifo_read show " the HelloWorld", explained to accept success 

Under Next compile, compiled after a good run two terminals in Linux, run more than two programs separately, you can see, when you run fifo_read, the program has been read in every second, and then we enter another terminal: 

$ ./fifo_write helloworld

It can be seen fifo_read show " the HelloWorld", explained to accept success

 

Guess you like

Origin blog.csdn.net/qq_38971487/article/details/91493102