The background network communication framework generally uses FIFO as the mechanism of event notification: Create a FIFO, then open the FIFO in a non-blocking read and non-blocking write, and then add the FD to the Epoll as the FD that notifies the network event.
There is a vague question that is easy to overlook. FIFO opens in non-blocking mode, you must first open read and then open write. Otherwise it will be error no such device or address. That is, the code below is correct.
#define Fifo_file "/tmp/fifo.xxx"#define fifo_mode File_mode s_irusr | S_IWUSR | S_irgrp | S_iwgrp | S_iroth | S_iwothMkfifo (Fifo_file, fifo_mode); int readfd 0); int 0);
The two open in the above code cannot be reversed, or it will be an error: No such device or address
Note that, even if later we write code is in two processes through FIFO to communicate, do not care to write to open the FIFO, whether the FIFO has been read to open, because we can set a dummy fd, as follows:
int 0 ); int 0);
Interested friends can copy the following code in the past, to see the results of the output:
1/Note L18
2/No comment L18
1#include <unistd.h>2#include <sys/types.h>3#include <sys/stat.h>4#include <fcntl.h>5 6#include <errno.h>7#include <stdio.h>8#include <string.h>9 Ten #defineFile_mode S_IRUSR | S_IWUSR | S_irgrp | S_iwgrp | S_iroth | S_iwoth One #defineFifo_file "/tmp/fifo.test_nonblock_write" A - intMainvoid) - { the Mkfifo (Fifo_file, File_mode); -fprintf (stderr,"Mkfifo, errno=%d, error=%s\n", errno, Strerror (errno)); - - //Open (Fifo_file, o_rdonly| O_nonblock, 0); +fprintf (stderr,"Open (o_rdonly), errno=%d, error=%s\n", errno, Strerror (errno)); - +Open (Fifo_file, o_wronly| O_nonblock,0); Afprintf (stderr,"Open (o_wronly), errno=%d, error=%s\n", errno, Strerror (errno)); at -Unlink (Fifo_file); - - return 0; -}
Considerations for Write FIFO in o_nonblock mode