Author:Zeng Hongan, a lecturer at Huaqing vision embedded College.
A well-known pipe, also known as FIFO, is a way of inter-process communication. FIFO has the following features:
1. Full Duplex Communication Mode, data first-in-first-out;
2. It can be used for communication between any process by specifying the same pipeline file;
3. The file name is stored in the file system, while the content in the MPs queue is stored in the memory. You can operate on it through open, read, and write;
To use FIFO, follow these steps:
1. Create/open a FIFO
FIFO is a file type. in Linux, the FIFO type is represented by p. As follows:
-Rwxr-xr-x 1 root 7368 create_fifo
-Rw-r -- 1 root 380 create_fifo.c
Prw-r -- 1 root 0 myfifo
-Rwxr-xr-x 1 root 8178 read_fifo
-Rw-r -- 1 root 1185 read_fifo.c
-Rwxr-xr-x 1 root 8333 write_fifo
-Rw-r -- 1 root 1139 write_polico.c
As you can see, although the FIFO file exists in the file system (which can be opened by different processes), the content in the FIFO file is stored in the memory, so the file size is always 0.
Because FIFO is not a common file, it can only be accessed using file IO.
# Include <sys/STat. h>
Int mkfifo (cONst char * path, mode_t mode );
The mkfifo function is used to create a famous pipe. The path parameter specifies the path of the FIFO to be created. mode indicates the access permission of the pipe file, which is generally expressed by the number of octal bytes.
# Include <sys/stat. h>
# Include <fcntl. h>
Int open (const char * path, int oflag ,...);
The function open Opens a file through a specified path. Different processes can call open to open the same FIFO for communication. Refer to the following code (the header file is omitted)
# Define BUF_SIZE 51
Int main (int argc, char * argv [])
{
Int fd;
Ssize_t n;
Char buf [BUF_SIZE];
If (argc <2)
{
Fprintf (stdout, "Usage: % s <export o_path> \ n", argv [0]);
Exit (1 );
}
If (mkfifo (argv [1], 0666) <0) // An error occurred while creating the FIFO
{
If (errno! = EEXIST) // The error is not because the MPs queue already exists.
{
Fprintf (stderr, "mkfifo () failed % s \ n", strerror (errno ));
Exit (-1 );
}
}
If (fd = open (argv [1], O_RDWR) <0) // An error occurred while enabling FIFO.
{// Note: <priority is higher than =
Fprintf (stderr, "open () failed % s \ n", strerror (errno ));
Exit (-1 );
}
...
Return 0;
}
Ii. read/write FIFO
After a process enables FIFO, it can perform corresponding read/write operations based on the options specified during open (refer to the option description in the open help document ).
# Include <unistd. h>
Ssize_t read (int fildes, void * buf, size_t nbyte );
Ssize_t write (int fildes, const void * buf, size_t nbyte );
......
If (n = read (fd, buf, BUF_SIZE) <0)
{
Fprintf (stderr, "read () failed % s \ n", strerror (errno ));
Exit (-1 );
}
Else if (n = 0)
{
Fprintf (stdout, "all write sides are closed... \ N ");
Exit (-1 );
}
Else
{
Fprintf (stdout, "read % d bytes from FIFO: % s \ n", n, buf );
}
......
Write operations on the FIFO, you can follow the above Code.
At last, we will summarize the issues that should be paid attention to when using FIFO:
1. open FIFO may be blocked because only the read or write end exists. In other words, if the read-only/Write-only mode is specified when the program opens the FIFO
The process is a read/write end for the Enable FIFO. If the read/write mode is specified, the process is both the read end and the write end.
2. When reading data from the first-in-first-out (using the read function), if there is no data, the wait is blocked by default until data is written to the first-in-first-out. If the read function returns 0, all write ends of the FIFO are off.
Closed, the program needs to be processed accordingly.
When writing data to the FIFO (using the write function), if the FIFO has enough space, the write function will return the number of bytes written. If the space is insufficient, the write function will be blocked until the data is written. When
When some read ends are closed, writing data to the FIFO will fail. The kernel sends a signal (SIGPIPE) to the writing process to terminate the process. There are two solutions: the program is opened in read/write mode.
FIFO or capture the SIGPIPE signal in the program, which is processed by the user.
"This article is written by Hua Qing vision http://www.embedu.org/index.htm"