The inter-process communication IPC Linux

  Interprocess communication (IPC) refers to the dissemination and exchange of data between different processes, each of which has a different process for each user address space (virtual address space), a process of any global variable in another process could not see, so to exchange data between processes can be core, open up a buffer in the kernel, the process a copy data from user space to the kernel buffer, then copy data from process B kernel buffer to own user space provided by the kernel this is called inter-process communication mechanism; under the Linux IPC mechanism is divided into: pipes, semaphores, shared memory, message queues, sockets;

First, the pipeline

1 thought: Calling pipe function in the kernel will open up a pipe called a buffer file that contains a read end and a write end, inter-process can communicate through the buffer zone;

2 Features: half form, parent and child, comes with a synchronization mechanism;

Usage 3: Create the parent calls conduit pipe, to obtain two file descriptor fd [2], fd [0]-readable, fd [1] can be written; fork a child process the parent process, the child process is also described in two to give Fu fd [2]; parent closing the read end fd [0], child closes the write end fd [1], so that the parent process to the pipe to write data, the child process to read data from the pipe, the pipe is a circular queue implemented , the write data from the end of the inflow, outflow from the reading end; follows:

(1) read the pipeline, if the pipeline is empty blocks until the end of the write write write data to the pipeline; write side is closed if 0 is returned;

(2) write pipeline, if the pipeline is full, blocking until the read end of the read data pipeline inner removed; end is closed if the read error of -1 is returned, generates a signal SIGPIPE;

 4 code is implemented as follows:

 1 #include <stdio.h>
 2 #include <unistd.h>
 3 #include <string.h>
 4 
 5 #define ABCDE "abcde"
 6 #define FGHIJ "fghij"
 7 
 8 int main(int argc, char *argv[])
 9 {
10     int   fd[2] = {0}, size = 0;
11     pid_t pid = -1;
12     char  buf[256] = {0};
13 
14     if(pipe(fd)) //1 pipe
15     {
16         perror("pipe");
17         return 1;
18     }
19     pid = fork(); //2 fork
20     if(pid < 0)
21     {
22         perror("fork");
23         return 2;
24     }
25     else if (pid == 0)//3 child write fd[1]
26     {
27         printf("child start\n");
28         close(fd[0]);
29         size = write(fd[1], ABCDE, 5);
30         printf("child,msg=%s,size=%d\n", ABCDE, size);
31         //sleep(1);
32         size = write(fd[1], FGHIJ, 5);
33         printf("child,msg=%s,size=%d\n", FGHIJ, size);
34     }
35     else //3 father read fd[0]
36     {
37         printf("father start\n");
38         close(fd[1]); 
39         //sleep(1);
40         size = read(fd[0], buf, 256);  //block until child write to fd[1]
41         printf("father,msg=%s,size=%d\n", buf, size);
42         memset(buf, 0, sizeof(buf));
43         size = read(fd[0], buf, 256);  //block until child write to fd[1]
44         printf("father,msg=%s,size=%d\n", buf, size);
45     }
46     return 0;
47 }
View Code

two,

Guess you like

Origin www.cnblogs.com/bo1990/p/11401717.html