Mp.weixin.qq.com/s?src=11&ti…

Once in an interview, I was asked about the ways of communication between processes, but I didn’t speak well because I hadn’t thought deeply and sorted it out before. Process we all know what are the ways to communicate, but I guess a lot of people are against the “back” to the memory, so this article today, talk to you in detail explain how they communicate, let everybody can understand the difference between them as far as possible, the advantages and disadvantages, etc., in this case, after the interviewer let you for example, you can also conveniently came to.

1, pipes,

Let’s look at a Linux statement

netstat -tulnp | grep 8080Copy the code

Studied Linux named estimate is to understand the meaning of this statement, with “|” means the pipe, its role is to after the output of the previous command as a command input. In this case, the output of netstat -tulnp is used as the input of grep 8080. If two processes want to communicate, we can use this kind of pipe to communicate, and we can know that the vertical line has no name, so we call this kind of communication method anonymous pipe.

This communication mode is one-way, with the output of the first command as the input of the second command. If processes want to communicate with each other, two pipes need to be created.

There is an anonymous pipe, which means there is a named pipe, so let’s create a named pipe.

mkfifo  testCopy the code

This command creates a named pipe named test.

We then use one process to write data into the pipe, and another process to read the data out.

echo "this is a pipe" > test/ / write dataCopy the code

If the contents of the pipe are not read, the command will remain there until another process reads the contents of test. Then we use another process to read it

cat < test/ / read the dataCopy the code

We can see that the data in test is being read out. The last command has also been executed.

As you can see from the above example, the notification mechanism of a pipe is similar to that of a cache, as one process puts data in a cache area and waits for another process to pick it up, and the pipe is one-way.

What are the disadvantages of this method of communication? Obviously, this method of communication is inefficient. You see, process A transmits data to process B, and only waits for process B to fetch the data before process A returns.

So pipes are not suitable for processes that communicate frequently. Of course, it has its advantages, such as simplicity and the ability to ensure that our data has actually been taken by another process. When we use Linux, we use it a lot.

2. Message queues

Can we put the process’s data in some memory and let the process return immediately? And return without waiting for another process to fetch it?

A: Yes, we can use the communication mode of message queue to solve this problem. For example, if process A wants to send a message to process B, it only needs to put the message in the corresponding message queue, and process B can retrieve it from the corresponding message queue when it needs it. Similarly, process B asks process A to send messages. This communication is similar to caching.

Are there disadvantages to this method of communication? A: Yes, the message queue model is not suitable if the data sent by process A occupies a large amount of memory and the communication between the two processes is very frequent. Because a sends a large amount of data, it means that the process of sending a message (copy) takes a lot of time to read memory.

Is there a solution? Answer yes, please continue to read.

3. Shared memory

Shared memory communication is a good way to eliminate the time spent copying.

Doesn’t each process have its own separate memory? How can two processes share the same memory?

As we all know, 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.

4. 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.

5, the Socket

Shared memory, pipes, semaphores, message queues, they are all communication between multiple processes in a host, can two processes separated by thousands of miles communicate?

Answer is necessary, this time Socket this guy will come in handy, for example, we usually through the browser to initiate an HTTP request, and then the server to return the corresponding data to you, this is the use of Socket communication.

conclusion

Therefore, processes communicate with each other in the following ways:

1, pipes,

2. Message queues

3. Shared memory

4. Semaphore

5, the Socket

This is the end of the story. When I looked at the way processes communicate with each other, I just memorized it by rote. I didn’t understand the relationship between them, their strengths and weaknesses, and why they communicate this way. So recently I spent some time to study and sort out this article. I believe that after reading this article, you can better understand the origin of various communication methods.