简体   繁体   中英

TCP/UDP high-performance server under linux

I want to understand some question of high-performance server program which use Multithreading.

  1. epoll can handle TCP socket listening and socket connect,and i can use epoll_wait in the main thread, if there are any socket connect comming, program can accept connections and recv data in the work thread. there is no conflict between work thread. am i right?

    udp is an connectionless protocol,can we use recvfrom function in the all of work thread at the same time? the main thread just use epoll to notice the work thread receive data. (assume that i can process each UDP packet individually).

  2. this is a UDP server. i set it work with non-blocking socket. i receive data in the main thread and handle it , then send it to the task queue , if the task queue have data, the program wake up the work thread and lock the task queue ,then get the data from task queue, unlock the task queue so that other work thread can get task from task queue, and then handle it.

    is this a good multithread UDP server?i am not sure. is there another design of UDP server with high-performance。

    I am in a puzzle about this questions, thank you very much!

Your basic approach is right. Start by studying the infamous C10K problem and how it was overcome. Once you understand the major bottlenecks in various implementations, you need to consider the following as part your design process :

  • Minimise thread creation/deletion cycles.
  • Always choose an "event-based" model over a blocking model.

Basically the one-thread-per-request model is preferred for its simplicity of implementation. But it does NOT scale well with the number of concurrent requests. When designing systems required to support more than a few 1000 concurrent requests, one prefers the use of sockets over threads.

The optimum number of "worker-threads" to instantiate depends upon :

  • Load (number of concurrent requests)
  • System (CPU, RAM)

Since this is a very popular problem when implementing web-server designs, you can find several analyses like this one , by simply searching for Apache vs. Nginx .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM