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

What is the IO model of server network programming?

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

Share

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

This article mainly explains "what is the IO model of server network programming". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the IO model of server network programming".

The picture of    is relatively simple, but many people must think that every network read (recvfrom ()) or write (sendto ()) operates between the network card and the user process before seeing this picture, but it is not. As can be seen from the above figure, the data needs to pass through the kernel from the network card to the user space and from the user space to the network card. The same is true of reading and writing data from disk. So with mmap technology, those who are interested can have their own Baidu. Application process (Web server also belongs to application process, here we need to unify several concepts: user process, application program, Web server program, they are all application processes relative to the kernel, so they are unified into application processes in later articles) need to read and write data to the kernel through system calls (such as recvfrom/sendto), and the kernel further operates the network card.

According to the blocking and non-blocking of the system call mode of the application process, and the difference between the synchronous and asynchronous processing of the operating system when dealing with application requests,    can be divided into five IO models:

1. Blocking IO model (blocking IO)

  

Description: when an application makes a recvfrom system call, it blocks this call until there is data on the socket and copied to the user space buffer. This mode is generally used with multithreading. For each connection received by the application process, a thread is created for this connection to handle the read, write and business processing on the connection.

   advantages: simple programming, suitable for teaching. Many of the examples on UNIX Network programming Volume I are based on this pattern.    disadvantage: if there is no data on the socket, the process will always block. At this time, other sockets have data and can not be processed in time. In the case of multithreading, threads will always exist unless the connection is closed, and the creation, maintenance, and destruction of threads are very resource-intensive, so the number of connections that can be established is very limited.

2. Non-blocking IO model (nonblocking IO)

  

Description: every time the application process calls recvfrom, it will not block even if there is no data ready, and will continue to execute, avoiding the malpractice of the process blocking on a certain connection.

Advantages of   : the code is relatively simple, the process does not block, and all connections can be handled in the same thread.

Disadvantages of   : frequent polling is required, which consumes CPU. When concurrency is large, it will spend a lot of time polling on connections without any data. So the model appears only in systems that specialize in providing certain functions.

3. IO reuse model (IO multiplexing)

  

Description: the application process blocks system functions such as select/poll/epoll to wait for a connection to become readable (data is coming), and then call recvfrom to read data from the connection. Although this mode also blocks on the select/poll/epoll, unlike the blocking IO model, it blocks the occurrence of read (write) events on waiting for multiple connections, which significantly improves efficiency and increases the possibility of parallel processing of multiple connections in a single thread / single process.

Advantages of   : unified management of connections, not necessarily multithreaded, and no polling required. You only need to block select and you can manage multiple connections at the same time.

   disadvantage: when the number of connections managed by select/poll/epoll is too small, this model will degenerate into a blocking IO model. And there is one more system call: one select/poll/epoll, one recvfrom.

4. Signal-driven IO model (signal-driven IO)

  

Description: the application process creates a SIGIO signal handler that handles read, write and business processing of data on the connection. And install this signal to the operating system, the process can be executed. When the kernel data is ready, it sends a signal to the application process, triggering the execution of the signal handler. Then recvfrom and business processing are carried out in the signal processing program.

Advantages of   : non-blocking

   disadvantage: if the previous notification signal is not processed, the latter signal cannot be processed even if it comes. Therefore, when the semaphore is large, the subsequent signal can not be sensed in time.

5. Asynchronous IO model (asynchronous IO)

  

Description: the application process tells the kernel to start an operation through aio_read, and then notifies the application process after the entire operation is completed, including copying data from kernel space to user space. Signal-driven IO is when the kernel tells us when to start an IO operation, while the asynchronous IO model tells us when the IO operation is completed.

Note: the first four models are all with blocking parts, some blocking waiting for data to be ready, and some blocking copying data from kernel space to user space. This model application process from calling aio_read to the data is copied to user space without any blocking, so this mode is called asynchronous IO model. I have reservations about the naming and juxtaposition of these five models, and I feel that it is easy to confuse readers.

The advantage of   : without any blocking, make full use of the kernel of the system to parallel IO operations with computing logic.

Disadvantages of   : complex programming and poor operating system support. Currently, only iocp under windows implements the real AIO. Linux was introduced in version 2.6, and it is not perfect at present, so the multiplexing model is generally adopted in Linux.

Comparison of IO models

The first four models of    are mainly different from the first phase because their second phase is the same: the process blocks recvfrom calls while the data is copied from the kernel to the buffer of the application process. In contrast, the asynchronous IO model needs to be processed at both stages, which is different from the other four models.

  

Thank you for your reading, the above is the content of "what is the IO model of server network programming". After the study of this article, I believe you have a deeper understanding of what the IO model of server network programming is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report