Complete port usage Summary

Source: Internet
Author: User

<! -- [If! Supportlists] -->

<! -- [Endif] --> Preface

This article does not fully introduce the completed port, but briefly introduces the completed port and several common concepts. This article focuses on the Resource release when the port is closed.

<! -- [If! Supportlists] --> Basic Introduction

Completion port-it may be the most complex I/O model in Win32 and the most complex Kernel Object in Win32. It manages overlapping I/O requests through a specified number of threads to provide services for completed I/O requests. Compared with other I/O models, it manages any number of I/O sockets. If an application needs to manage a large number of sockets at the same time, this model can often achieve the best system performance.

Create an I/O completion port through createiocompletionport (the only Win32 function that creates a kernel object without the lpsecurity_attributes parameter, because the completed port is only applied to the process, when you create an I/O completion port, the kernel actually creates five different data structures.

  • Device List.

<! -- [If! VML] --> <! -- [Endif] -->
<! -- [If! Supportlinebreaknewline] -->
<! -- [Endif] -->

  • I/O Completion queue (FIFO ). When a device's asynchronous I/O request is complete, the system checks whether the device is associated with a complete port, if the system adds the completed I/O request item to the I/O Completion queue of the completed port.

<! -- [If! VML] --> <! -- [Endif] -->
<! -- [If! Supportlinebreaknewline] -->
<! -- [Endif] -->

  • Wait thread Queue (LIFO ). When a thread in the thread pool calls getqueuedcompletionstatus, the thread ID of the calling thread is put in the queue.
  • Release thread Queue (active thread Queue ). The completion port monitors and limits the number of active threads through this queue. This limit is usually the number of cpus. Too many active threads have no practical significance and will lead to thread switching to reduce performance.
  • Pause the thread queue. When the thread calls sleep, waitforsingleobject, waitformultipleobjects, and so on.

<! -- [If! VML] --> <! -- [Endif] -->

 

<! -- [If! Supportlists] --> related concepts <! -- [If! Supportlists] --> worker thread and completion port

After a port is successfully created, You can associate the socket handle with the object. However, before associating a socket, you must first create one or more "worker threads" to provide services for the completed port after I/O requests are delivered to the completed port object.

The workflow for completing the port I/O model is as follows:

1) create a port through createiocompletionport.

2) create a worker thread.

3) Use createiocompletionport to associate the port with a device.

4) Send an asynchronous I/O request through wsaxxx.

5) in the worker thread, call getqueuedcompetionstatus to obtain the I/O Request items for subsequent processing.

<! -- [If! Supportlists] --> <! -- [Endif] --> Single Handle data and single I/O operation data

<! -- [If! VML] --> <! -- [Endif] --> handleCreateiocompletionport(Handle filehandle, handle existingcompletionport,
<! -- [If! VML] --> <! -- [Endif] --> ulong_ptr completionkey, DWORD numberofconcurrentthreads );
<! -- [If! VML] --> <! -- [Endif] --> boolGetqueuedcompletionstatus(Handle completionport, lpdword lpnumberofbytestransferred,
<! -- [If! VML] --> <! -- [Endif] --> pulong_ptr lpcompletionkey, lpoverlapped * lpoverlapped, DWORD dwmilliseconds ); <! -- [If! VML] --> <! -- [Endif] -->

The lpcompletionkey parameter of getqueuedcompetionstatus contains "Single Handle Data". It is set through the completionkey parameter when calling createiocompletionport to associate the port with the device. That is to say, this data is specific to the device (socket ).
The lpoverlapped parameter of getqueuedcompetionstatus contains "single I/O operation data". After the function is used to obtain the I/O request Completion items in the I/O Completion queue, lpoverlapped points to an overlapped data structure that is passed when this I/O request is initiated, that is, the data is specific to the I/O Request.
What is the use of single handle data and single I/O data? Same as single-handle data, we can associate a specific processing function, processor, or other structure to perform specific processing on the I/O of the handle. Single I/o Data establishes a connection between the initiation and completion of asynchronous I/O, which can be associated with a buffer or a processor (see ace_proactor) to facilitate asynchronous I/O operations.

<! -- [If! Supportlists] --> Notes

The following are several situations in which the I/O Completion queue is inserted in the request Completion notification:

  • Closesocket is called.
  • Cancelio is called.
  • The thread that initiates an I/O Request terminates.
  • Timeout
  • Postqueuedcompletionstatus
  • The I/O request is successfully completed.

In addition to normal completion and postqueuedcompletionstatus, other completion notifications will return false for getqueuedcompletionstatus. In this case, lpoverlapped (time-out is null) points toSingleI/OData. After understanding this, most of the things I will talk about later are not a problem. I 'd like to explain more about it.

<! -- [If! Supportlists] --> Resource Management Question 1
  • If I/O Requests return non-pending errors and getqueuedcompletionstatus returns falseSingleI/ODataResources used. (How to Handle socket errors and resource release in iocp)
  • When overlapping I/O operations are performed, An Overlapped structure is forcibly released. To avoid this situation, the best way is to call the closesocket function for each socket handle, and any overlapping I/O operations that have not been performed will be completed.
<! -- [If! Supportlists] --> resource management question 2 (Closing the port service)

We usually end the worker thread by calling postqueuedcompletionstatus to add a special completion item to the I/O Completion queue. At this time, we need to handle unfinished I/O requests in different situations:

  • For I/O Requests initiated in the worker thread (this is generally the case), these I/O requests will be completed as the worker thread ends, in this case, we need another thread to clean up the queue by calling the getqueuedcompletionstatus function with the timeout parameter 0 and traversing I/O, lpoverlapped contains data specific to I/O operations.
  • You can also close the socket or cancel related operations after receiving the closing notification so that the I/O request is completed and processed. This requires recording these sockets and corresponding I/O operations.
<! -- [If! Supportlists] --> about postqueuedcompletionstatus


Since the waiting thread queue is lifo, It is tricky for this function to notify every worker thread.

<! -- [If! Supportlists] --> <! -- [Endif] --> references

[1] Jeffery Richter. Advanced windows (3rd edition), Microsoft Press, 1997
[2] Anthony Jones, Jim ohlund. Network Programming for Microsoft Windows, Microsoft Press, 2002

 

Reference: http://blog.csdn.net/dananhai/archive/2008/02/26/2122778.aspx

Related Article

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.