explain
Anonymous pipeline, which can only be used between processes with kinship, greatly limits the use of pipeline. The emergence of well-known pipeline breaks through this limitation. It can enable two unrelated processes to communicate with each other.
- The pipeline can be indicated by a pathname and is visible in the file system. After the pipeline is established, the two processes can read and write it as an ordinary file, which is very convenient to use.
- However, FIFO strictly follows the first in first out rule. Reading pipes and FIFO always returns data from the beginning, and writing them adds data to the end. They do not support file positioning operations such as lseek().
- The function mkfifo() can be used to create a famous pipeline, which is similar to the open() operation in the file. You can specify the path and open mode of the pipeline
- After the pipeline is created successfully, you can use the open (), read() and write() functions. Like the development settings of ordinary files, O can be set in open() for pipes opened for reading_ RDONLY,
- For pipes opened for writing, O can be set in open()_ Wronly, different from ordinary files here is the blocking problem. Because there is no blocking problem in the reading and writing of ordinary files, there is a possibility of blocking in the reading and writing of pipes,
- The non blocking flag here can be set to o in the open() function_ NONBLOCK. The following discusses the read and write of blocking open and non blocking open respectively.
- (1) For read processes.
-
If the pipe is blocked open and currently FIFO If there is no data in the read process, it will be blocked until there is data written.
-
If the pipe is non blocking open, regardless of FIFO Whether there is data in the, the read process will immediately execute the read operation. That is, if FIFO If there is no data in the, the read function will immediately return 0.
- (2) For write processes.
-
If the pipeline is blocked open, the write operation will be blocked until the data can be written.
-
If the pipeline is opened non blocking and cannot write all data, the read operation fails to write partially or the call fails
function
Function prototype int mkfifo(const char *filename,mode_t mode)
- The function passes in the value filename: the name of the pipeline to be created (including the path)
- The function passes in the value mode:
-
O_RDONLY: Read pipeline
-
O_WRONLY: Write pipeline
-
O_RDWR: Read write pipeline
-
O_NONBLOCK: Non blocking
-
O_CREAT: If the file does not exist, create a new file and set permissions for it with the third parameter
-
O_EXCL: If used O_CREAT If the file exists, an error message can be returned. This parameter tests whether the file exists
- Function return value:
-
0: success
-
EACCESS: parameter filename The specified directory path does not have executable permissions
-
EEXIST: parameter filename The specified file already exists
-
ENAMETOOLONG: parameter filename The path name for is too long
-
ENOENT: parameter filename The included directory does not exist
-
ENOSPC: The file system does not have enough free space
-
ENOTDIR: parameter filename The directory in the path exists but is not a real directory
-
EROFS: parameter filename The specified file exists in a read-only file system
technological process
realization
fifi_read.c
#include <sys/types.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/wait.h> #include <errno.h> #include <limits.h> #define MYFIFO "/tmp/myfifo" #define MAX_BUFFER_SIZE PIPE_BUF int main(int argc,char * argv[]) { char buff[MAX_BUFFER_SIZE]; int fd; int nread; /* Judge whether the named pipeline already exists. If it has not been created, it will be created with corresponding permissions*/ if (access(MYFIFO, F_OK) == -1) { if ((mkfifo(MYFIFO, 0666) < 0) && (errno != EEXIST)) { printf("Cannot create fifo file\n"); exit(1); } } /* Open a named pipe as a read-only block */ fd = open(MYFIFO, O_RDONLY); if (fd == -1) { printf("Open fifo file error\n"); exit(1); } /*Loop reading named pipeline data*/ while(1) { memset(buff, 0, sizeof(buff)); if ((nread = read(fd, buff, MAX_BUFFER_SIZE)) > 0) { printf("Read '%s' from FIFO\n", buff); } } close(fd); exit(0); }
fifi_write.c
#include <sys/types.h> #include <unistd.h> #include <string.h> #include <stdlib.h> #include <stdio.h> #include <sys/stat.h> #include <fcntl.h> #include <sys/wait.h> #include <errno.h> #include <limits.h> #define MYFIFO "/tmp/myfifo" #define MAX_BUFFER_SIZE PIPE_BUF int main(int argc,char * argv[]) { char buff[MAX_BUFFER_SIZE]; int fd; int nwrite; if(argc<=1) { printf("Usage: ./fifo_write string\n"); exit(1); } sscanf(argv[1],"%s",buff); /* Open FIFO pipeline in write only blocking mode */ fd = open(MYFIFO, O_WRONLY); if (fd == -1) { printf("Open fifo file error\n"); exit(1); } /*Write string to pipe*/ if((nwrite = write(fd, buff, MAX_BUFFER_SIZE))>0) { printf("Write '%s' to FIFI\n",buff); } close(fd); exit(0); }
phenomenon
Open the read application before the write application