A cocked 1. Concept of pipeline

Pipes, also known as “anonymous management”, or “anonymous pipes”, pipes are a very basic and frequently used IPC method.

1.1 Pipeline essence

  • The essence of a pipe is also a file, but a pseudo file, which is actually a kernel buffer of 4K size;
  • After the pipe is created, it generates two file descriptors, one for the read side and the other for the write side.
  • Data in a pipe can only be written from the write end and read from the read end;

1.2 Pipeline Principles

A pipe is a buffer in the kernel, or more specifically, a circular queue. Data is written from one end of the queue and read from the other, as shown below:

img

1.3 Advantages of pipelines

simple

1.4 Disadvantages of pipelines

  • Only one-way communication, if the need for two-way communication needs to establish two pipes;
  • Can only be applied to processes that are related by blood, such as father-child processes;
  • The buffer size is limited, typically 1 page, or 4k;

A cocked 2. Pipeline creation

Three steps for creating a pipe:

A. The parent process calls the pipe function to create a pipe.

B. The parent process calls the fork function to create a child process.

C. Parent process closes fd[0] and child process closes fd[1];

The details are shown in the figure below:

img

A cocked 3. Pipe read and write behavior

A. The buffer size of the pipe is fixed at 4K, so if the pipe is full, no more data can be written, and the process’s write call will block until there is enough space to write data again;

B. The pipe reads faster than the pipe writes. Once the data is read, the pipe frees up the corresponding space for subsequent data writes. When all data has been read, the process’s read() call blocks until more data is written.

4 ▋ routines

Communication between father and son:

 1#include <stdio.h>
 2#include <sys/types.h>
 3#include <unistd.h>
 4#include <string.h>
 5
 6int main()
 7{
 8    int fd[2];
 9    pid_t pid;
10    char buf[1024];
11    char *data = "hello world!"; 12 13 /* Create a pipe */ 14if (pipe(fd) == -1) {
15        printf("ERROR: pipe create failed! \n");
16        return- 1; 17 } 18 19 pid = fork(); 20if(pid == 0) {21 /* subprocess */ 22 close(fd[1]); // The child process reads the data and closes the write endread(fd[0], buf, sizeof(buf)); // Read data from pipe 24printf("child process read: %s\n", buf); 25 close(fd[0]); 26}else if(pid > 0) {27 /* Parent process */ 28 close(fd[0]); 29 Write (fd[1], data, strlen(data)); // Write data to pipe 30printf("parent process write: %s\n", data); 31 close(fd[1]); 32} 33 34return 0;
35}
Copy the code

Brotherly correspondence:

 1#include <stdio.h>
 2#include <sys/types.h>
 3#include <unistd.h>
 4#include <string.h>
 5#include <sys/wait.h>
 6
 7int main ()
 8{
 9    int fd[2];
10    int i = 0;
11    pid_t pid;
12    char buf[1024];
13    char *data = "hello world!"; 14 15 /* Create a pipe */ 16if (pipe(fd) == -1) {
17        printf("ERROR: pipe create failed! \n");
18        return -1;
19    }
20
21    for (i = 0; i < 2; i++) {
22        pid = fork();
23        if (pid == -1) {
24            printf("ERROR: fork error! \n");
25            return -1;
26        } else if (pid == 0) {
27            break; 28} 29} 30 31 /* Use I to determine the created child process and parent process */ 32if(I == 0) {33 /* close(fd[0]); 35 Write (fd[1], data, strlen(data)); 36printf("elder brother send: %s\n", data); 37 close(fd[1]); 38}else if(I == 1) {39 /* second subprocess, younger process */ 40 close(fd[1]); 41read(fd[0], buf, sizeof(buf));
42        printf("younger brother receive: %s\n", buf); 43 close(fd[0]); 44}else{45 /* Parent process */ 46 close(fd[0]); 47 close(fd[1]); 48for (i = 0; i < 2; i++) {
49            wait(NULL); 50} 51} 52 53return 0;
54}
Copy the code

5T technical information, including: Linux, C/C++, Python, Raspberry PI, embedded, Java, artificial intelligence, etc. Public number inside reply into group, invite you to enter master such as cloud technology exchange group.