Inter-process Communication mode
Inter-process communication methods include pipelines, shared memory, signals, semaphores, message queues, sockets.
Purpose of inter-process communication
The main purpose of inter-process communication is: data transmission, information sharing, event notification, resource sharing, process control and so on.
Pipeline of interprocess communication
Pipe: This type of communication has two restrictions, one is half-duplex communication, the data can only flow one way, and the other is only in the relationship between the processes used. A process's affinity usually refers to a parent-child process relationship.
Flow Pipeline (S_pipe): To remove the first restriction, it can be transmitted in both directions.
Named Pipes (name_pipe): Overcomes the limitations of pipes without names, and allows communication between unrelated processes.
Linux inter-process communication mechanism
Creation of pipelines
The pipe function creates a communication buffer that the program can access through the file descriptor Fildes[0] and fildes[1]. Return value: Successfully returned 0, error returned-1.
#include <unistd.h>int pipe (int fd[2]);
Reading and writing of pipelines
The parameter FD is the pipe descriptor and the same file descriptor that can be manipulated using the file I/O function (Close, read, write). FD[0] is the pipe read end, fd[1] is the pipe write end. If you do not need to, you can close the corresponding end of the descriptor. Data written to fd[1] can be read in FIFO order from fildes[0].
ssize_t Read (fd[0void *buf, size_t count); ssize_t Write (fd[1void *buf, size_t count);
Implementation of Pipelines
Call fork () to create two sub-processes, use the system call pipe () to establish a pipeline, and two sub-processes to each write a sentence to the pipeline:
Child Process 1 is sending a message!
Child Process 2 is sending a message!
The parent process reads from the pipeline information from two child processes, which is displayed on the screen. The two sub-processes are then ended separately.
Note: The parent process and the two child processes need to be synchronized, using the Waitpid () function to implement the parent process to wait for the child process to finish running and to read the data from the pipeline and print.
Because the fork function allows the child process to completely copy the entire address space of the parent process, the child processes have read and write ends of the pipeline. So it's best to turn off the unused end in the relevant process.
Ask the parent process to receive a message from the child process P1, and then receive a message from the child process P2. "There are two synchronization issues, between two child processes and the parent process (the sub-write stepfather read), the child process 1, and the child Process 2 (first 1 write, then 2 write). The flowchart is as follows:
Pipeline Implementation interprocess communication flowchart
#include <stdio.h>#include<stdlib.h>#include<unistd.h>#include<string.h>#include<errno.h>#include<sys/types.h>#include<sys/wait.h>intMain () {intstatus; pid_t P1; pid_t P2; intfd[2]; Charrevbuf[ -]; intret = pipe (FD);//Creating Pipelines if(ret = =-1)//pipe return value of-1 fails{perror ("Pipe error!\n"); Exit (1); } if((P1 = fork ()) = =0)//Child process P1 { Char*child1 ="Child Process 1 is sending a message!"; Close (fd[0]);//close the pipe read endWrite (fd[1], Child1, strlen (child1));//Child Process 1 writes data to the pipeline } Else if(P1 >0)//Parent Process{waitpid (P1,&status,0); if((P2 = fork ()) = =0)//Child process P2 { Char*child2 ="Child Process 2 is sending a message!"; Close (fd[0]);//close the pipe read endWrite (fd[1], Child2, strlen (child2));//Child Process 2 writes data to the pipeline } Else if(P2 >0)//Parent Process{Close (fd[1]);//Close the pipe write end//Waitpid (P1, &status, 0);Read (fd[0], Revbuf, -); printf ("The MSG1:%s\n", REVBUF); Waitpid (P2,&status,0); Read (fd[0], Revbuf, -); printf ("The msg2:%s\n", REVBUF); } Else //failed to create process 2{perror ("Fork p2 error!\n"); Exit (1); } } Else //failed to create process 1{perror ("Fork p1 error!\n"); Exit (1); } return 0;}
Anonymous pipeline for Linux process communication