I/O completion ports Learning

Source: Internet
Author: User

The Http://www.cnblogs.com/xiangshancuizhu/p/3325980.htmlIO completion port provides an efficient threading model for processing multiple asynchronous IO requests on multiprocessor systems. When a process creates a new completion port, the operating system creates a new queue object that is intended to serve these requests. Processing multiple concurrent asynchronous IO Requests is faster and more efficient by using the IO completion port to process the current request with the associated pre-allocated thread pool instead of the new one. IO completion port How does the function CreateIoCompletionPort create an IO completion port and associate one or more file "filename descriptors" on this port. When one or more asynchronous IO operations are completed on these operators, an IO completion packet (packet) enters the FIFO queue associated with the IO completion port. A powerful use of this mechanism is to synchronize multiple descriptors within a single object, even though they have other useful applications. Note: Packets in the queue may be queued in a different order. "This is not a very understanding, is not multiple threads in the dequeue operation?" Note: The file handle is a system-abstract representation representing overlapping IO endpoints, not just file on disk. For example, it could be a network endpoint, a TCP socket, a named pipe, or a mail slot. Any system object that supports the completion port can be exploited. When a file handle was associated with a completion port, the status of block passed in won't be updated until the packet I s removed from the completion port. The only exception are if the original operation returns synchronously with an error. A thread (either one created by the main thread or the main thread itself) uses the GetQueuedCompletionStatus function to Wait for a completion packet to is queued to the I/O completion port, rather than waiting directly for the asynchronous I/O to complete. Threads that block their execution on an I/O completion port is released in Last-in-first-out (LIFO) order, and the next Completion packet is pulled from the I/O completion port ' s FIFO queue for that thread. This means if a completion packet is released to a thread and the system releases the last (most recent) thread Assoc Iated with this port, passing it the completion information for the oldest I/O completion. When the descriptor is associated with a completion port, the incoming state is not updated until the package is finished Take it away from the Chengduan mouth. The only exception is that the original operation returned an error asynchronously. A thread (which can be created by the main thread or the main thread itself) uses the GetQueuedCompletionStatus function to wait for the package to enter the completion port, rather than waiting for the asynchronous IO to complete. The thread that completes the port blocking operation is released with a backward appearance, and the next port packet of the thread is removed from the IO port with the FIFO queue. This means that when a package is fed to a thread, the system will release the last thread associated with the port, and pass the completion port information for the oldest completion port "This sentence is not very understanding." Although any thread can call GetQueuedCompletionStatus to a specific completion port, when a specific thread calls it for the first time, It begins to associate with a specific completion port to know one of the following three scenarios: 1. A thread exists, associates another completion port, and 2 closes the completion port. This means that a thread can only associate one completion port. When a completion package is placed in a queue that completes the port, the system first checks how many threads are currently associated with the port. If the number of threads running is less than the concurrency value, a recent wait thread is allowed to process the completion package. When a running thread finishes processing, it will continue to call GetQueuedCompletionStatus again, because at this point it either waits for the next completion package, or waits until the package queue is listed as empty. Threads can call Postqueuedcompletionstatus to deliver the queue that completes the packet to IO port. By doing so, the completion port can be used to collect interaction data from other threads or processes, and the completion port package can be charged from the IO system. The PostQueuedCompletionStatus function allows an application to post a specific completion package to the IO completion port without opening the asynchronous IO operation. For example, this is useful for non-event notification worker threads. The IO completion port handle and each description of the descriptor associated with a specific IO completion port are considered to be the completion port, and when there is no reference associated with it, the completion port is freed. Therefore, all these handles must be properly cared for to release the completion port and its associated system resources. After these conditions are met, the completion port handle is closed by calling CloseHandle. Note A completion port is associated with creating it and cannot be shared between processes. However, a single handle can be shared among different threads in the same process. The most important nature of the thread and the concurrent IO completion port is the concurrency value. The concurrency value of the completion port is determined by the thenumberofconcurrentthreads parameter of the CreateIoCompletionPort. This value limits the number of threads associated with the completion port. When the number of associated threads exceeds this concurrency value, the system will block any subsequent threads until the number of running threads drops to the concurrency value. The most effective semantics are when there is a completion package in the queue, but no wait is satisfied because the port has reached the concurrency limit. Consider that under concurrent values, one or more threads are waiting to be called at GetQueuedCompletionStatus. In this case, if the queue has a completion package waiting, when the running thread calls GetQueuedCompletionStatus, he will not block because the thread queue mentioned above is FIFO first. Instead, the thread will immediately get the next completion package. No context switch occurs because the running thread continues to take the complete package, and the other thread cannot run the "how does this paragraph understand". Note In the previous example, the extra thread seems to be useless and will not be allowed, but assume that the running thread has stopped or closed its associated completion port because some synchronization mechanisms have never entered the wait state. Note the execution branches of all these threads when the application is redesigned. The best concurrency Max is the number of CPUs on your computer. If your transport requires a complex calculation, this requires a large concurrency value. Each completion package may be completed with an increase in time, but more completion packages are processed concurrently. You can experiment with setting concurrency values to achieve optimal program optimization. The system runs when a thread that is associated with the same completion port is waiting for some reason (for example, the SuspendThread function), another thread waits for the GetQueuedCompletionStatus to process the completion package. When this thread in the waiting state starts to run,This is a short process when the number of active threads exceeds the concurrency value. However, the system will quickly reduce the number of active threads by not allowing any new active threads to start until the number of active threads drops to the concurrency value. This is why you can create threads that are more constructor than concurrent values online. Thread pool management is beyond the scope of this topic, but a good experience is that online constructor has a minimum of twice threads (>=2*corenum) of the system processor. You can refer to the Thread pools. The following functions of the supported IO functions can be used to initiate IO operations by using the completion port. You must pass an instance of the overlapped structure and the descriptor associated with the IO to these functions (by calling CreateIoCompletionPort) to open the IO completion port mechanism: Connectnamedpipedeviceiocontrollockfileexreaddirectorychangeswreadfiletransactnamedpipewaitcommeventwritefilewsasendmsgws Asendtowsasendwsarecvfromwsarecvmsgwsarecvok, translated, many sentences are not fluent ah, some places themselves do not understand Ah!

(go) I/O completion Ports learning

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.