5.09 overview of famous pipelines
Pipe refers to an unnamed pipe
fifo refers to a well-known pipeline
FIFO will directly create a visible file in the file system
The file types in linux system are divided into seven categories: BCD LSP
Difference between fifo and pipe:
1. fifo belongs to half duplex, and data can only flow in the same direction at the same time
2. Data written into fifo follows the first in, first out rule
3. The data transmitted by fifo is unformatted, and the read-write format is required to be unified
4. fifo exists as a special file in the file system, but the contents of fifo exist in memory
5. The pipeline corresponds to a buffer in memory
6. Reading data from fifo is a one-time operation
7. When the process using fifo exits, the fifo file will continue to be saved in the file system to free up space for writing more data
8. fifo has a name, and unrelated processes can communicate
5.10 creation of famous pipes
There are two ways to create:
1. Command in shell
mkfifo file name
2. Using the function mkfifo
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode)
Function: create a famous pipeline and generate a file pathname of the local file system
Parameters: file name and permission; 0 returned successfully
If the file exists, an error will be reported. We can check errno Error code in H
#include<errno.h>
{ if(mkfifo("fifo_file", 0664) == -1) { if(error != EEXIST) { In this way, the errors reported by the file can be ignored perror("fail to mkfifo"); exit(1); } perror(""); } return 0; }
5.11 basic read-write operation of famous pipeline
The IO called by the system can operate FIFO: open,close,read,write, etc. when FIFO is opened, the non blocking flag (O_NONBLOCK) has the following effects;
Features: 1. Do not specify o_ Nonblock (i.e. no open bit or O_NONBLOCK)
2. When open opens fifo in read-only mode, how many processes should be blocked to open this fifo for writing
3. When open opens fifo in write only mode, how many processes should be blocked to open this fifo for reading
Because the famous pipeline creates a pipeline file locally, the IO functions called by the system can basically operate on the famous pipeline, but lseek cannot be used to modify the offset of the pipeline file
Note: the local file created by the famous pipeline only plays the role of identification. The real famous pipeline realizes process communication or opens up memory in the kernel space, so the locally generated file is only an identification and has no other role. The operation of local pipeline file is essentially the operation of kernel space
#define FIFONAME "fifo_file" main() { if(mkfifo("fifo_file", 0664) == -1) { if(error != EEXIST) { In this way, the errors reported by the file can be ignored perror("fail to mkfifo"); exit(1); } perror(""); } The next step is to operate on the famous pipeline int fd fd = open(FIFONAME, O_RDWR); if(...) Then write data and read data int fd }
5.11 how to realize interprocess communication
Since a famous pipeline creates a pipeline file, communication can be realized between unrelated processes,
Create 2 named pipes
Need a send C documents and recv c
send.c
#include<stdio.h> #include<stdlib.h> #include<unstd.h> ... int main(int argc, char **argv) { //Create pipeline ----- if judgment error, omitted mkfifo("myfifo1", 0664); mkfifo("myfifo2", 0664); int fd_w, fd_r; fd_w = open("myfifo1", O_WRONLY); fd_r = open("myfifo2", O_RDONLY); char buf[128] = ""; ssize_t bytes; while(1) { fgets(buf, sizeof(buf), stdin); buf[strlen(buf) -1] = '\0'; bytes = write(fd_w, buf, sizeof(buf)); bytes = read(fd_r, buf, sizeof(buf)); printf("from recv:%s\n", buf); } return 0; }
recv.c #include<stdio.h> #include<stdlib.h> #include<unstd.h> ... int main(int argc, char **argv) { Create pipe-----if Wrong judgment mkfifo("myfifo1", 0664); mkfifo("myfifo2", 0664); int fd_w, fd_r; //This place is just the opposite fd_r = open("myfifo1", O_WDONLY); fd_w = open("myfifo2", O_RDONLY); char buf[128] = ""; ssize_t bytes; while(1) { bytes = read(fd_r, buf, sizeof(buf)); printf("from send:%s\n", buf); fgets(buf, sizeof(buf), stdin); buf[strlen(buf) -1] = '\0'; bytes = write(fd_w, buf, sizeof(buf)); } return 0; }
5.13 basic read-write operation of famous pipeline
1. Both read and write ends exist, read-only and not write
Blocking will occur
2. Both read and write terminals exist, only write but not read
Blocking will occur
3. In a process, there is only a read side and no write side
Will block at the position of the open function
4. In a process, there is only a write side and no read side
Will block at the position of the open function