Windows completion Port IOCP model (i)

Source: Internet
Author: User

1 Basic Introduction to Windows completion ports


2 He's a socket-based event management model that's only available under Windows

3 Unlike Select, select needs to reset the management handle multiple times, IOCP only once

4 after the event select needs to operate to obtain data, and IOCP notify you when the data operation is good

The number of 5select management handles is limited, IOCP No Limit

6IOCP supports multithreading while waiting.


My design idea a thread is used to listen for an accept event, a thread listens for the IO event of the socket,

Most frameworks are like this, and you can actually use just one thread to do an asynchronous socket, which is completely enough, and now

Using multiple threads is to look at the multithreading usage of this IOCP,


Before that, we need to understand the next asynchronous communication and overlapping I/O models

Asynchronous communication mechanism: http://blog.51cto.com/blogger/draft/412685

overlapped I/O model: http://blog.51cto.com/12158490/2058180


I recommend using single-threaded reason: it should be multiple threads, after getting the data, or sending it to another thread,

Then make the event queue, that is, when your thread gets the data, it still has to queue thread, logical processing, multithreading meaningless.



2 complete port Internal running process

To complete the port practice: a few threads in advance, you have a few CPUs I open a few, the first is to avoid the context of the thread switch, because the thread wants to

At the time of execution, there is always a CPU resource available, and then several threads wait until a user request arrives, adding these requests to a

Public message queue, and then these open threads queued up to take the message out of the message queue and process it.

Asynchronous communication and load balancing issues because he provides a mechanism to use several threads "fairly" to process input/output from multiple clients

, and the thread will be suspended by the system if nothing is done, and the CPU will not be occupied.



3WSAAsyn Csele CT or WSAEventSelect and completion port

WSAAsyncSelect or WSAEventSelect Two asynchronous models, these two patterns must not use the overlapped (overlapping) mechanism,

is not really asynchronous, it may be that the internal maintenance of a message queue, these two patterns, although the implementation of asynchronous receive, but not asynchronous send,

Complete Port He is the first to receive the user data back after notifying the user directly to fetch, and these two modes will only receive data arrival notification, can only be by

The application goes recv the data, and it sends the difference on performance.

to implement asynchronous communication, a strong I/O data structure is needed . called re-junction structure "overtlapped",

W Indow All asynchronous pass-through the letter was based on him, and the completion port was no exception.

Is that the time to execute the I/O request is overlapping (overlapped) with the thread performing other tasks.


Overlapping structures are a core data structure implemented by asynchronous communication mechanisms, because almost all network operations, such as Send/Receive, are

WSASend () and WSARecv () instead, the parameters will be accompanied by an overlapping structure, because the overlapping mechanism can be understood as a

The ID number of the network operation, that is to say we want to take advantage of the asynchronous mechanism provided by overlapping I/O, each network operation must have a unique ID number,

Because in the system kernel, a call to see overlapping I/O comes in, it uses its asynchronous mechanism, and the operating system can only rely on

This overlapping structure with the ID number to distinguish which network operation, and then processed in the kernel, according to the ID number, the corresponding data to go up.




4 Complete port Basic usage flow

The completion port is also divided into steps

1 Call the CreateIoCompletionPort () function to create a completion port, and in general, we need and need only

Create a completion port, save his handle, and then just use the handle.

2 based on and customer I/O operations it's best to build a worker thread yourself.

3 Access socket connection, there are two ways, 1 is the same as other models, there is a separate thread, specifically to accept the client's connection,

The second is to use a better performance asynchronous AcceptEx () request

4 Whenever a client comes in, or calls the CreateIoCompletionPort () function, this is not the completion port,

Instead, bind the newly-connected socket handle with the completion port you created.

5 After the client is connected, we will submit a request to this client socket, such as receiving a file to call WSARecv () and then the system

Will go to perform the operation of receiving data, it is not our control.

6 then call GetQueuedCompletionStatus () (a blocking function) inside the queue that scans the port for

A request for network communication exists (such as reading data, sending data, etc.), and once there, the request is retrieved from the queue of the completion port,

Continue execution of the subsequent code of this thread, after processing, you must again post network communication request (WSARECV), so loop.




The difference between 5AcceptEx and accept

AcceptEx and accept the biggest difference, is to cancel the blocking mode of the accept call, that is to say Accentex is also through

Completes the port to complete asynchronously.

The advantage of this is that if the client does not have many concurrent connection requests in a short period of time, the performance difference between the accept and the AcceptEx is small.

Although we create sockets with only one line of socket S =socket (...) A single line of code, but building a socket inside the system is quite

resource-intensive, because Winsock2 is a tiered system, creating a socket needs to be handled between multiple provider,

Eventually form a usable socket, in short, creating a socket for the happiness is quite high.

AcceptEx three points stronger than accept:

(1) The key is AcceptEx before the client is connected, the client socket is established, that is, AcceptEx is first established

Socket, and then issue the AcceptEx call, that is, before the client communicates, whether or not a client is connected,

Sockets are built in advance, and do not need to think that the accept is in the client after the connection, on-site phone time to establish a socket,

(2) compared to the accept can only block the way to establish a connecting entrance, for a large number of concurrent clients, is a bit crowded,

And AcceptEx can post multiple requests on the completion port, and when the client is connected, it is graceful and easy.

(3) AcceptEx also has an advantage, is the delivery of Accepex when the client sent the first set of data, which is at the same time

, it means that if the client simply connects but does not send the data, we will not receive a notification that the Accepeex is complete.

Asynchronous AcceptEx are more cumbersome to use than accept.



Due to the time relationship, the use of the completion port is detailed in the next blog post

Implementing features in a picture






Reference Blog Address http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html




Windows completion Port IOCP model (i)

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.