Compare 5 Types of IO models under Windows

Source: Internet
Author: User

See a very interesting explanation:

Lao Chen had a daughter who worked outside and could not come back often, and she contacted her by letter. Their letter will be delivered to their mail box by the postman.
This is very similar to the socket model. Here I will take the old Chen received letters as an example to explain the SOCKETI/O model.
Select Model:
Lao Chen wanted to see his daughter's letter very much. So that he went downstairs every 10 minutes to check the mailbox to see if there is a daughter's letter, in this case, "go downstairs to check the mailbox" and then back upstairs to delay the old Chen too much time, so that Lao Chen can not do other work.
The Select model is very similar to that of old Chen: Go round and round to check ... If there is data ... Receive/Send ...

WSAAsyncSelect Model:

Later, Lao Chen used the new mailbox of Microsoft Corporation. This kind of mailbox is very advanced, once the new letter in the mailbox, Gates will call old Chen: Hello, Grandpa, you have a new letter! From then on, Lao Chen no longer have to check the mailbox frequently, the tooth also does not ache, you look quasi, the blue sky ... No, Microsoft ...
The WSAAsyncSelect model provided by Microsoft is what this means.
The WSAAsyncSelect model is the most easy-to-use socket I/O model under Windows. With this model, Windows notifies the application of network events in the context of the message.

WSAEventSelect Model:
Later, Microsoft's mailbox is very popular, the number of people who buy Microsoft mailbox is counted in millions ... So that Gates 24 hours a day to call customers, tired of the back pain, drinking ant force God is not good. Microsoft has improved their mailbox by adding an add-on device to the customer's home that monitors the customer's mailbox, and whenever a new letter arrives, the device sends a "new letter arrival" to remind Lao Chen to collect the letter. Finally, gates can sleep.

Overlapped I/O event notification Model:
Later, Microsoft through the investigation found that Lao Chen does not like to go downstairs to send and receive letters, because the upper and lower floors are actually a waste of time. So Microsoft is improving their mailbox again. New mailboxes use more advanced technology, as long as users tell Microsoft their home in the number of floors, the new mailbox will send the letter directly to the user's home, and then tell the user, your letter has been put in your home! Lao Chen is very happy because he doesn't have to send and receive letters himself!
The Overlapped I/O event notification model and the WSAEventSelect model are very similar in implementation, primarily in the "Overlapped", where the Overlapped model is to let applications use overlapping data structures (wsaoverlapped), One or more Winsock I/O requests are posted at one time. When these submitted requests are complete, the application receives a notification. What do you mean? That is, if you want to receive data from the socket, just tell the system that the system is receiving the data for you, and all you need to do is provide a buffer for the system ~~~~~

Overlapped I/O completion routine model:
Old Chen received a new letter, the general procedure is: Open the envelope----out the stationery----Read the letter----reply to the letter ... In order to further alleviate the user burden, Microsoft has developed a new technology: Users just tell Microsoft about the steps of the letter, Microsoft Mailbox will follow these steps to process the letter, no longer require users to personally eagerness/read/reply! Lao Chen finally had a petty bourgeoisie life!

IOCP Model:
Microsoft mailbox seems to be perfect, old Chen is also very satisfied. But in some big companies the situation is completely different! These big companies have tens of thousands of mailboxes, each with hundreds of letters to deal with, so Microsoft mailbox often crashes due to overloaded operation! Need to reboot! Microsoft had to make a killer ...
Microsoft sent a super robot named "Completion Port" to each big company to get the robot to handle the letters!
The Windows NT team noticed that the performance of these applications was not as high as expected. In particular, handling many simultaneous customer requests means that many threads run concurrently in the system. Because all these threads are operational [not being suspended and waiting for what happens],microsoft realizes that the NT kernel spends too much time translating the context of running threads [context], the 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 is less expensive than creating a process, but it is far from overhead. We might as well imagine: if you open n threads in advance, let them jam in that hold[, then you can post all the user's requests to a message queue. The n threads then remove the message from the message queue and process it. You can avoid threads for every user request. Not only reduce the resources of the thread, but also improve the utilization of the thread.

Let's take a look at the implementation of the IOCP model:
Create a completion port
Fcompletport: = CreateIoCompletionPort (Invalid_handle_value, 0,0,0);
Accept the remote connection and bind the connected socket handle to the IOCP you just created
Aconnect: = Accept (Flistensock, addr, Len);
CreateIoCompletionPort (Aconnect, Fcompletport, nil, 0);
Number of CPUs created * + 2 threads
For I:=1 to Si.dwnumberofprocessors*2+2 do
Begin
Athread: = Trecvsendthread.create (false);
Athread.completport: = fcompletport;//Tell this thread that you're going to go to this IOCP to access the data
End
Just so simple, all we have to do is build a IOCP, bind the socket handle of the remote connection to the IOCP you just created, create n threads, and tell the N threads to go up to the IOCP to access the data.
And look at what Trecvsendthread threads do:
Procedure Trecvsendthread.execute;
Var
......
Begin
While, not self. Terminated) do
Begin
Querying IOCP status (data read and write operations completed)
GetQueuedCompletionStatus (Completport, BYTESTRANSD, Completkey, poverlapped (Pperiodat), TIME_OUT);
If BYTESTRANSD <> 0 Then
....//Data read/write operation completed

Post a read data request again
WSARecv (Completkey, @ pperiodat^. Bufdata), 1, BYTESRECV, Flags, @ (pperiodat^. overlap), nil);
End
End

The read-write thread simply checks to see if the IOCP has completed the read-write operation we posted, and then delivers a new read-write request if it completes.
It should be noted that all of the trecvsendthread we create are accessing the same IOCP (because we have only created one IOCP), and we are not using a critical section! Is there no conflict? Do you need to consider synchronization issues?
That's what IOCP is all about. IOCP is not an ordinary object and does not need to be considered for thread safety issues. It automatically deploys the thread that accesses it: if a socket has a thread A is accessing, then thread B's access request is assigned to another socket. All of this is automatically provisioned by the system, and we don't have to ask.

Note: IOCP uses the thread pool, where the number of threads is typically twice times the number of CPUs in the current computer.

Compare 5 Types of IO models under Windows

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.