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

Basic Series of High performance Server Development (1) Division of Labor between main Thread and worker Thread

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In order to smoothly handle multiple client links, the server generally accepts new client connections in a thread A and generates socket fd for new connections, and then sends these socket fd for new connections to several other worker threads B1, B2, B3, B4. These worker threads handle network IO events (i.e., send and receive data) on these new connections, and at the same time, they also handle other transactions in the system. Here we call thread A the main thread, B1, B2, B3, B4, etc. worker threads. The code framework for worker threads is generally as follows:

while (! m_bQuit) { epoll_or_select_func(); handle_io_events(); handle_other_things();}

In epoll_or_select_func() through select() or poll/epoll() to detect io events on socket fd, if there are these events then the next step handle_io_events() to handle these events (send and receive data), after finishing may also do some other tasks of the system, namely call handle_other_things().

This has three advantages:

Thread A only needs to handle incoming connections, not network IO events. Because network IO event processing is generally relatively slow, if thread A processes both new connections and network IO, it may be because the thread is busy processing IO events and cannot process new connections on the client side in time, which is very bad.

Thread A receives a new connection, and new socket fd can be allocated to worker threads according to certain Load Balancer principles. Common algorithms, such as round robin, that is, polling mechanism, that is, assuming that there is no connection disconnection in the middle, a new connection is allocated to B1, another to B2, another to B3, and another to B4. This is repeated, that is, thread A records the number of socket fd on each worker thread, which can maximize the balance of resources and avoid the phenomenon that some worker threads are "busy" and others are "idle."

Even if the worker thread is not fully loaded, you can have the worker thread do something else. For example, there are now four worker threads, but only three connections. Thread B4 can then do something else at handle_other_thing().

Here is an important efficiency question:

In the above while loop, epoll_wait/poll/select functions in epoll_or_selecc_func() generally set a timeout. If you set the timeout to 0, then without any network IO time and other task processing, these worker threads will effectively idle, wasting CPU time slices for nothing. If the timeout time is greater than 0, epoll_wait/poll/select still has to wait for a specified time to return when there is no network IO time, resulting in handle_other_thing() not being executed in time, affecting other tasks not being processed in time, that is to say, once other tasks are generated, they have a certain delay in processing. That's not good either.

So how do you solve this problem?

The effect is that if there is no network IO time and other tasks to be processed, then these worker threads are better off hanging rather than idling; if there are other tasks to be processed, these worker threads should be able to process them immediately rather than waiting for epoll_wait/poll/select to hang for a specified time before starting to process them.

We take the following approach to solve this problem. In linux, for example, we bind a default fd to epoll_fd, regardless of whether it has the file descriptor fd or not. This fd is called wake fd. When we need to handle other tasks, write 1 byte to this wake fd, so that this fd immediately becomes readable, the epoll_wait()/poll()/select() function immediately wakes up and returns, and then handle_other_thing() can be executed immediately, and other tasks are processed. Conversely, when there are no other tasks and no network IO events, epoll_or_select_func() hangs there and does nothing.

This wake-up fd can be implemented in several ways on linux platforms:

pipe, create a pipe, bind the pipe to epoll_fd. When needed, write a byte to one end of the pipe and the worker thread wakes up immediately.

Linux 2.6 added eventfd:

int eventfd(unsigned int initval, int flags);

The procedure is the same, binding the generated eventfd to epoll_fd. When needed, write a byte to this eventfd and the worker thread wakes up immediately.

The third method is the most convenient. Socket pair is a pair of sockets connected to each other, equivalent to two endpoints of server and client, each end can read and write data.

int socketpair(int domain, int type, int protocol, int sv[2]);

The two socket handles returned by calling this function are sv[0], and sv[1], where bytes are written in either and bytes are received in the other.

Bind the socket of the received bytes to epoll_fd. When needed, write a byte to another socket to write to, and the worker thread wakes up immediately.

If socketpair is used, the domain parameter must be set to AFX_UNIX.

Because in windows, select function only supports detecting socket this kind of fd, so windows can generally only use the principle of method 3. And you need to manually create two sockets, and then connect one to the other, binding the read segment to the fd of select. This is something to watch out for when writing code across two platforms.

Welcome to the public account "easyserverdev". If you have any technical or professional questions that need my help, you can contact me through this public account. This public account not only shares high-performance server development experience and stories, but also provides technical answers and professional puzzles for technical friends free of charge. If you have any questions, you can leave a message directly on Weixin Official Accounts, and I will reply to you as soon as possible.

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

Servers

Wechat

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

12
Report