search

Home  >  Q&A  >  body text

c++ - Multiple processes are listening to the same port at the same time. Can different applications listen to the same port?

nginx’s model knows that it is master-worker

曾经蜡笔没有小新曾经蜡笔没有小新2759 days ago1117

reply all(3)I'll reply

  • 黄舟

    黄舟2017-05-31 10:39:23

    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

    reply
    0
  • PHP中文网

    PHP中文网2017-05-31 10:39:23

    The one with the smallest pid should be the master.

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-31 10:39:23

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

    reply
    0
  • Cancelreply