Multiple I/O models and improvements to socket efficiency

Source: Internet
Author: User
Tags epoll

In the process of reading the Redis source, I have been thinking about the question of "why can single-threaded redis be so efficient?" ”。 In order to understand this problem, I looked up some information, probably to figure out the development of epoll and other I/O model and its principle, the following is a record collation.

# #I/O model

# # #操作系统与网络I/O

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/5B/wKioL1SAIOyBfD_sAAFuJXzzVVE649.jpg "title=" 1.jpg " Width= "260" height= "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:260px;height:400px; "alt=" Wkiol1saioybfd_ Saafujxzzvve649.jpg "/>

From Wikipedia, is a basic computer structure. The computer accomplishes two main tasks, operations, and I/O. Because CPU processing efficiency and the I/O efficiency of various devices vary widely, it is an important task for the operating system to coordinate I/O to improve efficiency. Various I/O models emerge.

# # #同步和异步

The entity that the computer task executes is the process, and the relationship between the process and I/O needs to be cleared. The I/O operations can be divided into synchronous and asynchronous ways, depending on the state of the process when I/O is executed.

* Synchronous IO Operation: Causes process blocking until IO operation is complete

* Asynchronous IO Operation: IO operation does not cause process blocking

Where synchronous and asynchronous refers to the relationship between process and Io, blocking and non-blocking refers to the state of the process. In conjunction with the process management of the operating system, it can be considered that the optimal state is: a) the process is completely unaffected, continues execution until I/O completes and then processes B), the process is completely suspended, the resources are handed over to other processes, and the I/O is completed before waking up.

# # #网络IO模型

# # #阻塞IO

The most basic way, the problem is that the process is blocked by one IO and cannot respond to other requests and is inefficient.

650) this.width=650; "src=" Http://images.51cto.com/files/uploadimg/20120222/2121550.png "width=" 528 "height=" 300 " Border= "0" hspace= "0" vspace= "0" title= "" style= "width:528px;height:300px;" alt= "2121550.png"/>

# # #非阻塞IO

Using polling can consume a lot of computing resources and should only be used in special cases.

650) this.width=650; "src=" Http://images.51cto.com/files/uploadimg/20120222/2121551.png "width=" 537 "height=" 300 " alt= "2121551.png"/>

# # #IO复用

The key is reuse, a process can wait for multiple IO at once.

650) this.width=650; "src=" Http://images.51cto.com/files/uploadimg/20120222/2121552.png "width=" 561 "height=" 300 " alt= "2121552.png"/>

# # #信号驱动IO

The use of the range is very small, the signal generated under TCP is too frequent to distinguish the meaning, so only in the application of the UDP protocol. "The actual use of the signal-driven I/O program that the author can find is a UDP-based NTP server program." The idea difference from asynchronous IO is simply whether the operating system automatically copies the data to the kernel space, and I don't understand why it is not directly developed into asynchronous IO, but instead joins the model.

650) this.width=650; "src=" Http://images.51cto.com/files/uploadimg/20120222/2121553.png "width=" 540 "height=" 300 " alt= "2121553.png"/>

# # #异步IO

The entire process is non-blocking.

650) this.width=650; "src=" Http://images.51cto.com/files/uploadimg/20120222/2121554.png "width=" 513 "height=" 300 " Border= "0" hspace= "0" vspace= "0" title= "" style= "width:513px;height:300px;" alt= "2121554.png"/>

The first 4 types are synchronous, and the 5th is asynchronous


# #socket

# # #socket基本概念

The socket works above the session layer and receives data by binding and listening on the specified port.

650) this.width=650; "src="/images/column179-1.jpg "width=" 408 "height=" "" alt= "Column179-1.jpg"/>

# # #socket示例

The simplest accept () mode is blocked, and the program hangs before the connection is established. You can use the while loop accept, but this way the read () is blocked, so the second connection cannot be processed until the first connection ends.

Write two interesting points in the code:

* The Listen function backlog has a magic number 511

* Close () followed by a time_wait process, this time if the same port will fail to bind again, you can use setsockopt ()

# #改进

# # #使用多进程改进

In the while loop, each accept will fork out a new process so that multiple connections can be processed at the same time. The relationship between the parent process and the child process, reference counting, signal processing, and so on, needs to be dealt with. And the destruction process is more expensive each time it is created.

# # #使用select改进

In the While loop, use Select to listen for an FD collection, which recovers from blocking when the FD read/write/exception occurs. Multiple fd can be monitored at the same time. Of course, read and write will also block, so to cooperate with multi-process/multi-threaded use. Because you do not need each connection to generate a new process, it is more optimized than the fork way.

The problems that exist are:

* FD Quantity is limited

* Cycle check every time all FD efficiency is low

* Low efficiency of user and kernel-state memory copy

# # #使用epoll改进

The Epoll is optimized for select issues:

* Upper limit is the maximum number of files that can be opened

* Only focus on "active" links do not cycle check

* Use memory sharing to avoid copying

Use Epoll to invoke the main 3 APIs:

int epoll_create (int size); 2.6.8 after the size parameter is ignored, refer to
int epoll_ctl (int epfd, int op, int fd, struct epoll_event *event);
int epoll_wait (int epfd, struct epoll_event * events, int maxevents, int timeout);

In particular, the concept of the horizontal trigger (LT) and the Edge Trigger (ET), as well as the handling of events (error, client shutdown, and so on) are especially important.

I have implemented the above methods in a simple way, see "20" in the code.

# # #性能对比测试

This paper, "18", makes a comparative test of the performance of Select/poll/epoll. You can see that the performance of select and Epoll is similar in the case where all I/O is active. However, when there is a large number of idle connections, the performance of Epoll is significantly higher than that of select, which is in line with Epoll's improved thinking.

In the process of finding information, I found that most of the information is vague or unclear. So the IO and socket as the predecessor knowledge, combined with some code examples. Hopefully, you should be better able to clarify your ideas and understand why I/O models need to be so designed and used in real-world applications. Not many things to write, dry goods are in the reference article.

Reference

The difference between the "1" Unix five basic I/O models-the line-blog Park

"2" Operating systems:i/o Systems

"3" OSI Model-wikipedia, the free encyclopedia

"4" Simple Understanding Socket-dolphinx-Blog Park

"5" Linux howtos:c/c++, Sockets Tutorial

"6" Linux file device with I/o:read/write function and blocking Block_ Bakery _ Baidu Space

"7" Socket programming-listen function Backlog_ Flying fish in Beijing _ Sina Blog

"8" [C + +] solves the problem of socket bind with a port | The business is a reality

"9" Linux multi-process disadvantage-web Capture-Iteye technology website

"10" The GNU C library:server Example

"11" Linux Epoll Introduction and program Examples-Sparkliang's column-Blog channel-csdn.net

"12" Linux under Select, poll and Epoll IO model-tianmo2010 's column-Blog channel-csdn.net

"13" UNIX Network programming--I/O multiplexing: The Select function and the poll function (vi)-Fish think Yuan's column-Blog channel-csdn.net

What is the principle of "14" Epoll or Kqueue? -Epoll Detailed

"15" How do I use Epoll? A Complete example in C-banu Blog

"16" Epoll (4): I/O event notification Facility-linux man page

"17" Epoll_create (2): Open epoll file Descriptor-linux man page

"18" https://www.kernel.org/doc/ols/2004/ols2004v1-pages-215-226.pdf

"19" Networking-caveats of Select/poll vs. Epoll reactors in Twisted-stack Overflow

"20" Mickhan/socket_demo

"21" Unix I/O------blocking, non-blocking, synchronous, asynchronous-51cto.com

This article from "My Journey is the Star Sea" blog, please be sure to keep this source http://mickhan.blog.51cto.com/2517040/1586370

Multiple I/O models and improvements to socket efficiency

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.