1, the preface



High performance network programming (6) : understand the threading model of high performance network programming






High performance network programming (6) : understand the threading model of high performance network programming


2. About the author



Caison Chen:






About Guangzhou Bay Chat:






Guangzhou Beitchat Information Technology Co., LTD., founded on August 21, 2013, is an information technology company focusing on building a kindergarten home co-education platform. The company’s product “Beichao” is a working platform for Parents of Kindergartens in China. It is committed to helping kindergartens solve the pain points in parents’ work such as display, notification and communication through Internet products and customized solutions, and promoting the harmony of home relations. Beitchat is the only brand jointly invested by Vtron (the first share of A-share preschool education), Tsinghua Enlightenment and netease. Up to now, “Beitchat” has covered 50,000 kindergartens and institutions in 31 provinces of China, with more than 10 million registered users. The retention rate of users in the next month is 74%, with a compound growth rate of 18.94%, leading the whole industry.


3, C10K problem series of articles



This article is the fifth in a series of articles on C10K issues. The overall table of contents is as follows:





  • “High Performance Network Programming (1) : How many Concurrent TCP connections can a Single server Have?”
  • “High Performance Network Programming part II: The Famous C10K Concurrent Connection Problem in the Last Decade”
  • High Performance Network Programming (PART 3) : The Next 10 Years, It’s Time to Consider C10M Concurrency
  • “High Performance Network Programming (IV) : Theoretical Exploration of High Performance Network Applications from C10K to C10M”
  • “High Performance Network Programming (5) : Understanding the I/O Model in High Performance Network Programming” (This article)
  • High Performance Network Programming (6) : Understanding the Threading Model in High Performance Network Programming
  • High Performance Network Programming Classic: The C10K Problem


4. The principle of Internet server processing network request



Let’s start with a typical process for a typical Internet server to handle a network request:










As can be seen from the figure above, the main processing steps include:





  • 1) Obtain request data, the client establishes a connection with the server and sends a request, and the server accepts the request (1-3);
  • 2) Build the response, when the server receives the request and processes the client’s request in user space until the build response is complete (4);
  • 3) Return the data, and the server sends the constructed response back to the client through the network I/O in the kernel space (5-7).



When designing the server-side concurrency model, there are two key points:





  • 1) How the server manages connections and obtains input data;
  • 2) How the server handles the request.





5. Basic understanding of “I/O model”



Before introducing the OPERATING system I/O model, let’s understand a few concepts:





  • 1) Blocking and non-blocking calls;
  • 2) A blocked call means that the current thread will be suspended until the result of the call is returned, and the calling thread will return only after the result is returned;
  • 3) A non-blocking call does not block the current thread until the result is not immediately available.









blocking
non-blocking






Synchronous and asynchronous processing:






The difference between blocking and non-blocking and synchronous and asynchronous (
:





  • 1) Blocking and non-blocking discussion objects are callers;
  • 2) Synchronous and asynchronous discussion objects are called.



Recvfrom function:









An input operation usually consists of two distinct stages:





  • 1) Wait for data to be ready;
  • 2) Copy data from the kernel to the process.









UNIX Network Programming Volume 1


I/O model 1: Blocking I/O
















Metaphor:



Advantages:



Disadvantages:


7, I/O model 2: Non-blocking I/O model






























I/O multiplexing






















Metaphor:



Advantages:



Disadvantages:





I/O Model 4: Signal-driven I/O Model






















Metaphor:



Advantages:



Disadvantages:














10, I/O model 5: ASYNCHRONOUS I/O model






















Advantages:



Disadvantages:












The principle of Java new generation network programming model AIO and the introduction of Linux system AIO


Summary of 5 I/O models