preface

Interprocess communication, we use it all the time, but we don’t pay attention to it. If an interviewer asks you how many different ways of communicating between processes you know, you’ll probably be a little confused. Today we will summarize the communication methods between processes.

The pipe

Pipe. This is an important concept that we’ll introduce when we learn about the Linux command line. It was invented by Douglas. McIlroy, who was also the inventor of the early shell on UNIX. After he invented shell, he found that when system operation executed commands, it was often necessary to hand over the output of one program to another program for processing, thus the pipeline came into being.

Pipes can be divided into two categories: anonymous pipes and named pipes.

Common Linux command “|” is anonymous pipe, said the output of a process transfer to another process, such as:

echo "Happyjava" | awk -F 'j' '{print $2}'
# output ava
Copy the code

Alternatively, we can create a named pipe with the mkfifo <pipename> command, such as:

mkfifo pipe
Copy the code

If a process enters data into a pipe, it blocks waiting for another process to read data from the pipe:

The left window (echo ‘Happyjava’ > pipe) will always block if I do not execute cat < pipe in the other window.

The message queue

Note that this message queue is not our usual MQ, such as Kafka, RabbitMQ, RocketMQ, etc.

Message queues provide a way to send a block of data from one process to another. Each block of data is considered to contain a type, and the receiving process can receive data structures containing different types independently. We can avoid synchronization and blocking problems with named pipes by sending messages. But message queues, like named pipes, have a maximum length limit for each chunk of data.

The use of message queues for inter-process communication may be constrained by the maximum length of data blocks, which is also the disadvantage of this communication mode. If the communication between processes is frequent, then the processes need to read the data from the queue into memory frequently, which is equivalent to copying from one process to another indirectly, which takes time.

The Shared memory

Shared memory communication is a good way to eliminate the time spent copying. When the system loads a process, the memory allocated to the process is not physical memory, but virtual memory space. So we can have two processes each take a piece of virtual address space and map it to the same physical memory. In this way, the two processes have separate virtual memory space, but part of the map to the same physical memory. This completes the memory sharing mechanism.

A semaphore

What’s the biggest problem with shared memory? Yes, it is the problem of multiple processes competing for memory, similar to the common thread safety problem. How to solve this problem? That’s when our semaphore comes into play.

The semaphore is essentially a counter for mutual exclusion and synchronization between processes. For example, if the initial semaphore value is 1 and process A accesses memory 1, we set the semaphore value to 0, and then process B accesses memory 1 and sees the semaphore value to 0, then process B will not be able to access memory 1. Thus, semaphores are also a means of communication between processes.

Socket

This is the way of interprocess communication that we have been using. For example, our wechat APP communicates with wechat server by using Socket Socket.

conclusion

To summarize, processes (Linux) communicate with each other in the following ways:

1, pipes,

2. Message queues

3. Shared memory

4. Semaphore

5, the Socket