Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the web inter-process communication mechanism?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what is the communication mechanism between web processes". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "what is the communication mechanism between web processes"!

1. Pipeline

Let's look at a Linux statement.

ounter(line

netstat -tulnp | grep 8888

Anyone who has learned Linux names probably understands the meaning of this statement, which| A pipe takes the output of the previous command as the input to the next command. This is where the output of netstat -tulnp is taken as input to grep 8888. If two processes want to communicate, they can use this pipe to communicate, and we can know that this vertical line has no name, so we call this communication method anonymous pipe.

And this communication is one-way, only the output of the first command can be used as the input of the second command, if the process wants to communicate with each other, then you need to create two pipes.

There are anonymous pipes, which means there are named pipes. Let's create a named pipe.

ounter(line

mkfifo test

This command creates a named pipe named test.

Next, we use a process to write data into this pipe, and then another process reads the data out.

ounter(line

echo "sowhat a pipe">

If the contents of the pipe are not read at this time, then the command will stay there until another process reads the contents of test. So we're going to use another process to read

ounter(line

cat < test //read data

We can see that the data in the test has been read out. The last order is over.

As you can see from the above example, the notification mechanism of pipes is similar to caching, just like one process places data in a cache area and then waits for another process to get it, and pipes are unidirectional.

What are the disadvantages of this method of communication? Obviously, this communication method is inefficient, process a transfers data to process b, and process a can only return after process b fetches the data. So pipes are not suitable for processes that communicate frequently. Of course, it also has its advantages, such as being relatively simple and ensuring that our data has actually been taken by other processes. When we use Linux, we often use it.

2. Message queue

Can we put the process data in some memory and then have the process return immediately? Without waiting for another process to fetch it?

A yes, we can use the message queue communication mode to solve this problem, for example, a process to send a message to b process, only need to put the message in the corresponding message queue on the line, b process needs to go to the corresponding time. From the message queue. Similarly, process b sends messages to process a. This type of communication is similar to caching. RabbitMQ, Kafka, etc.

Is there a disadvantage to this method of communication? Yes, if the data sent by the a process occupies a large amount of memory, and the communication between the two processes is particularly frequent, the message queue model is not suitable. Because a sends large amounts of data, it means sending messages (copies)-a process that takes a lot of time to read memory.

Is there any solution? Yes, please continue reading.

3. Shared memory

Shared memory is a good way to solve the problem of copying time. Java JVM is based on shared memory. Some people may ask, does not each process have its own independent memory? How can two processes share a block of memory?

We all know that when the system loads a process, the memory allocated to the process is not physical memory, but virtual memory space. Then we can let the two processes each take out a virtual address space, and then map to the same physical memory, so that although the two processes have independent virtual memory space, but part of it is mapped to the same physical memory, which completes the memory sharing mechanism.

4. Semaphore

What is the biggest problem with shared memory? Yes, it is a multi-process memory competition problem, similar to what we usually say about thread safety. How to solve this problem? This is when our semaphore comes on.

A semaphore is essentially a counter used to achieve mutual exclusion and synchronization between processes. For example, the initial value of semaphore is 0, and then when process a accesses memory X, we set the semaphore value to 1, and then when process b also accesses memory X, we know that there is already a process accessing memory X when the semaphore value is 1. At this time, process b will not be able to access memory X. So semaphores are also a way of communicating between processes. See Concurrent Programming for details.

5、Socket

The shared memory, pipes, semaphores, and message queues we mentioned above are all communication between multiple processes on a host. Can two processes thousands of miles apart communicate?

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

At this point, I believe that everyone has a deeper understanding of "what is the communication mechanism between web processes", so it is advisable to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report