c++ - Multiple processes are listening to the same port at the same time. Can different applications listen to the same port?
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-05-31 10:37:23
0
3
1136

nginx’s model knows that it is master-worker

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all(3)
黄舟

This should be multiple processes reusing one socket at the same time

However, in modern

linux, it is possible for multiple sockets to listen to the same port at the same time. This behavior is also supported in Nginx 1.9.1or above

linux 3.9or above kernel supports SO_REUSEPORT option, which allows multiple socket bind/listen on the same port. In this way, multiple processes can each apply for socket to listen to the same port. When data comes, the kernel does load balancing and wakes up one of the listening processes for processing. The usage is similar to setsockopt(listener, SOL_SOCKET, SO_REUSEPORT, &option, sizeof (option))

Using the

SO_REUSEPORT option can effectively solve the epollthundering problem. For specific tests, you can see the example written below: ThunderingHerdTest.cpp

For discussions about the

SO_REUSEPORT option, please refer to The SO_REUSEPORT socket option. Regarding the questioner’s doubts, this sentence in the article can be used as an answer:

To prevent unwanted processes from hijacking a port that has already been bound by a server using SO_REUSEPORT, all of the servers that later bind to that port must have an effective user ID that matches the effective user ID used to perform the first bind on the socket.

That is, not any process can be bound to the same port, only if the

effective user IDis the same

For discussions about

Nginx and SO_REUSEPORT, please see We increase productivity by means of SO_REUSEPORT in NGINX 1.9.1

Ty80

The one with the smallest pid should be the master.

漂亮男人

A single application has multiple processes listening to the same port, and the socket is shared

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template