6 Windows Socket I/O models are explained humorously)

Source: Internet
Author: User
Tags microsoft mail

Http://dotcpp.iteye.com/blog/599268

This article on the basis of others' articles (http://foxhack.blog.51cto.com/96963/25908), slightly changed, I hope to help beginners.

Old Chen has a daughter who works in a field and cannot come back frequently. She and she contact him by letter. Their mail will be delivered to their mailbox by the postman.
This is very similar to the socket model. The following describes the socket I/O model by taking Lao Chen as an example ~~~

I. Select model

Chen wanted to hear from his daughter. So that he goes downstairs every 10 minutes to check whether he has a daughter's email ~~~~~
In this case, "going downstairs to check the mailbox" and going back to the upstairs has delayed Lao Chen's time, so that he cannot do other work.
The select model is very similar to the Old Chen model in this case: Check again and again... if there is data... receive/send .......

It should be a common practice to use a thread to select:
Http://tangfeng.iteye.com/blog/518135

Ii. wsaasyncselect Model

Later, Chen used a new Microsoft mailbox. This kind of mailbox is very advanced. Once there are new letters in the mailbox, Gates will call Chen: Hello, Lord, you have new letters! From then on, Old Chen no longer has to go up and down frequently to check the mailbox, and his teeth do not hurt. You can look at it, blue sky ...... No, Microsoft ~~~~~~~~
This is what the wsaasyncselect model provided by Microsoft means.

The wsaasyncselect model is the most easy-to-use socket I/O model in windows. When this model is used, Windows notifies the application of network events as messages.

Http://tangfeng.iteye.com/blog/518141

Iii. wsaeventselect Model

Later, Microsoft's mailbox became very popular. The number of people who bought Microsoft Mail was counted as one million ...... as a result, Gates calls the customer 24 hours a day, causing a sore waist and a poor experience ~~~~~~
Microsoft improved their mailbox: by adding an additional device to the customer's home, the device monitors the customer's mailbox, whenever new mail comes, this device will send a "new letter arrives" to remind old Chen to receive the mail. Gates can finally go to bed.

Http://tangfeng.iteye.com/blog/518142

4. overlapped I/O Event Notification Model

Later, Microsoft found through a survey that Chen did not like sending and receiving letters from the upper and lower floors, because the upper and lower floors were a waste of time. So Microsoft improved their mailbox again. The new mailbox uses more advanced technologies. As long as the user tells Microsoft the number of its home on the floor, the new mailbox will send the mail directly to the user's home and then tell the user, your letter has been placed in your home! Old Chen is very happy because he no longer needs to send and receive emails in person!

The overlapped I/o Event Notification model is very similar to the wsaeventselect model in implementation. The main difference is "overlapped". The overlapped model allows applications to use the overlapping data structure (wsaoverlapped ), one or more Winsock I/O requests are shipped at a time. After these submitted requests are completed, the application will receive a notification. What does it mean? That is to say, if you want to receive data from a socket, you only need to tell the system that the system receives data for you, and all you need to do is provide a buffer zone for the system ~~~~~
Http://tangfeng.iteye.com/blog/518146

V. overlapped I/O completion routine model

After Chen receives a new letter, the general procedure is: open the envelope, pull out the letter paper, read the letter, and reply to the letter ...... to further reduce the burden on users, Microsoft has developed a new technology: as long as you tell Microsoft the operation steps on the mail, Microsoft Mail will follow these steps to process the mail, you no longer need to personally split/read/reply! Chen has finally lived a petty asset!

Http://tangfeng.iteye.com/blog/518146

Vi. iocp Model

Microsoft mailbox seems perfect, and Chen is also very satisfied. But in some big companies, the situation is completely different! These large companies have tens of thousands of mailboxes, and hundreds of mails need to be processed every second, so that Microsoft mailboxes often crash due to overload! Need to restart! Microsoft had to launch a killer ......
Microsoft assigned a super robot named "completion port" to every big company to process those letters!

"The Windows NT team noticed that the performance of these applications was not as high as expected. In particular, processing many concurrent client requests means that they run in the system in multiple threads concurrently. Because all these threads are runable [not suspended or waiting for something to happen], Microsoft realizes that the NT kernel spends too much time converting the context of the running thread [Context], threads do not get much CPU time to do their work. You may also feel that the bottleneck of the parallel model is that it creates a new thread for each customer request. Creating a thread has a lower overhead than creating a process, but it is far from having no overhead. Let's imagine that if n threads are enabled in advance to hold [blocking] them there, all user requests can be delivered to a message queue. Then the n threads extract messages from the Message Queue one by one and process them. You can avoid opening threads for every user request. This not only reduces thread resources, but also improves thread utilization. Theoretically, it is quite good. How can Microsoft not take it into consideration when I come up with questions that can be raised by general users? "----- From nonocast's understanding I/O completion port

Let's take a look at the implementation of the iocp model:

Http://tangfeng.iteye.com/blog/518148

Iocp is not a common object and does not need to consider thread security issues. It will automatically allocate the thread to access it: if a thread a is accessing a socket, the access request of thread B will be allocated to another socket. All of this is automatically distributed by the system, so we don't need to ask.

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.