1. FIFO:
FIFO is also a named pipe, can be used for any interprocess communication, and ordinary nameless pipe can only be used to have a common ancestor of the direct communication;
The named pipe is also half-duplex, and the open pipeline should not be opened with read-write mode, this operation is undefined;
2. FIFO creation:
#include <sys/stat.h>int mkfifo (constChar *= successfully returned 0, failed to return-1
FIFO is a file type, the mode parameter is the same as the mode parameter in the Open function, and the operation function of the general file (close, read, write, unlink, etc.) is used for FIFO;
3. Non-blocking flag (O_nonblock):
(1) Blocking mode: read-only open to block to a process for the write opened this FIFO, write-only open to block to a process to read the opening of this FIFO;
(2) Non-blocking mode: read-only returns immediately, if no process is read and open FIFO, then write only open return -1,erron=enxio;
4. Close one end:
(1) If you read a FIFO that has closed the write end, read the data and read the file terminator, and read returns 0;
(2) If you write a FIFO that has closed the read end, the sigpipe is generated;
5. Use:
(1) FIFO is used by the shell command to transfer data from one pipe to another without creating a temporary file;
(2) FIFO for customer process and server process data transfer;
6. Test the code:
FIFO_WRITER.C--Writes a string to the FIFO
1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <limits.h>6#include <sys/stat.h>7#include <sys/types.h>8#include <fcntl.h>9 Ten #defineFifo_name "/var/tmp/fifo_test" One #defineBuf_len Pipe_buf A - - intMainintargcChar*argv[]) the { - intPipeid =-1; - intFifoid =-1; - + CharBuffer[buf_len] = {0 }; - + if(Access (Fifo_name, F_OK) <0){ AFifoid = Mkfifo (Fifo_name,0777); at if(Fifoid <0){ -Perror ("Mkfifo error\n"); - return-1; - } - } - inPipeid =Open (Fifo_name, o_wronly); - if(Pipeid <0){ toPerror ("Open Pipeid error\n"); + return-1; - } the * intRead_bytes =Read (Stdin_fileno, buffer, buf_len); $ if(Read_bytes <0){Panax NotoginsengPerror ("Read error\n"); - Close (Pipeid); the return-1; + } A the Const Char* Buff_send =buffer; + intNo_write_bytes =read_bytes; - while(No_write_bytes >0){ $ intn =Write (Pipeid, buff_send, no_write_bytes); $ if(N <0){ -Perror ("Write error\n"); - Close (Pipeid); the return-1; - }Wuyi theNo_write_bytes-=N; -Buff_send + =N; Wu } - About Close (Pipeid); $ - return 0; -}
FIFO_READER.C--Reads a string from the FIFO
1#include <unistd.h>2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <limits.h>6#include <sys/stat.h>7#include <sys/types.h>8#include <fcntl.h>9 Ten #defineFifo_name "/var/tmp/fifo_test" One #defineBuf_len Pipe_buf A - - intMainintargcChar*argv[]) the { - intPipeid =-1; - - CharBuffer[buf_len] = {0 }; + -Pipeid =Open (Fifo_name, o_rdonly); + A intn = read (Pipeid, buffer, Buf_len-1); at if(N <0){ -Perror ("Read error\n"); - Close (Pipeid); - return-1; - } - in Write (Stdout_fileno, buffer, n); - to Close (Pipeid); + - return 0; the}
linux-interprocess communication (ii): FIFO