Pipelines are similar to the way the file reads and writes, and is an older way of communicating between processes. The use of pipelines requires the creation of a pipeline file, through which the process communicates by reading and writing the file. Pipeline files do not take up disk space. You must have information in the pipeline to read within the pipe. Otherwise the IO is blocked. The program is blocked. And both ends of the pipe are opened to read or write, otherwise the IO is blocked. Individuals prefer to use signals and piping in conjunction with When the pipeline has been written to send a signal to the relevant process, when the relevant signal came to the pipeline after the file read. This effectively avoids the IO blockage which prevents the program from doing other operations.
Pipelines are divided into famous pipes (FIFO) and nameless pipes (pipe):
- General steps for piping use:
- Initialize the piping file system
- Registering and loading pipelines
- Create a read and write pipeline
- To read and write
- Close the pipe file after use ends
One, nameless pipe: can only be used between processes that have affinity.
Header Files <unistd.h>
int pipe (int filedes[2]);//Create nameless pipes (communication between parent and child processes)
- int p[2]; Pipe file Descriptor
- Char buf[16];
- Char buf0[16];
- pid_t pid;
- strcpy (buf, "Hello world!");
- Pipe (P);//Register pipe
- PID = fork ()
- if (PID > 0) {
- Write (p[1], buf, strlen (BUF)); Write to a pipeline
- Close the pipe file descriptor
- Close (p[1]);
- Close (p[2]);
- }
- if (PID = = 0) {
- Read (P[0], buf0, sizeof (BUF0));//reading the pipe
- printf ("%s\n", buf0);
- Close (p[0]);
- Close (p[1]);
- }
- if (PID < 0) {
- Exit (1);
- }
Second, the famous pipeline: Any process can communicate in this way
Header files <sys/types.h> <sys/stat.h>//establishing a well-known pipeline (non-process communication)
int Mkfifo (const char * pathname,int mode);
Create a special FIFO file based on pathname, mode is the file's permissions
Return value: 0 success, 1 failure
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9
#define FIFO_PATH "/home/linux/myfifo"//Piping file
11
12
int main (int arge,char *argv[])
14 {
FDW int = 0; Read File descriptor
int FDR = 0;//Write file descriptor
+ Char Cwrite[8] = {0};
18
if (arge! = 2) {
printf ("parameter input error \ n");
return-1;
22}
23
if (Mkfifo (fifo_path,0666) < 0 && errno! = eexist) {//Register pipeline file
printf ("Create error \ n");
Return-2;
}else{
FDW = open (Fifo_path, O_creat | o_wronly,0666);//Open the Write pipeline
FDR = Open (Argv[1],o_creat | o_rdonly,0666);//Open the Read pipeline
30//operation of the read/write pipeline
if (Fdw > 0) {
32
if (FDR > 0) {
while (read (fdr,cwrite,7) > 0) {
Write (fdw,cwrite,7);
memset (cwrite,0,sizeof (cwrite));
37}
38
}else{
("%s File open failed \ n", argv[1]);
return-3;
42}
43
}else{
printf ("Pipe open failed \ n");
return-4;
47}
48}
49//Close File descriptor
Close (FDW);
Wuyi Close (FDR);
52
return 0;
54}
Pipeline of interprocess communication