I/O completion Ports

Source: Internet
Author: User

I/O completion Ports
I/O completion ports provide an efficient threading model for processing multiple asynchronous I/O requests on a multiproc Essor system.
When a process creates an I/O completion port, the system creates an associated queue object for requests whose sole PURPO SE is to service these requests.
Processes that handle many concurrent asynchronous I/O requests can do so further quickly and efficiently by using I/O comple tion ports
In conjunction with a pre-allocated thread pool than by creating threads at the time they receive an I/O request.

The I/O completion port provides an efficient threading model for processing multiple asynchronous I/O requests on a multi-core processor system.
When an I/O completion port is created, the operating system creates a queue object associated with the I/O completion port to handle these I/O requests.
The I/O completion port uses a combination of a pre-allocated thread pool instead of a thread to be created when the I/O request is received to efficiently and quickly handle multiple concurrent asynchronous I/O requests.

How I/O completion Ports work
The CreateIoCompletionPort function creates an I/O completion port and associates one or more file handles with that port.
When an asynchronous I/O operation on one of the these file handles completes, an I/O completion packet is queued in first-in- First-Out (FIFO) order to the associated I/O completion port.
One powerful use-this mechanism-combine the synchronization point-to-multiple file handles into a single object , although there is also other useful applications.
Please note this while the packets is queued in FIFO order they is dequeued in a different order.

CreateIoCompletionPort () can create an I/O completion port and can be managed with one or more sockets.
When these asynchronous sockets are complete, an I/O completion port packet is queued on the completion port in FIFO order.
Note: Although packets are queued in FIFL order, they do not necessarily queue in FIFL order.

When a file handle was associated with a completion port, the status of block passed in would not be updated until the Packe T is 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 Asynchro Nous 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 N Ext completion packet is pulled from the I/O completion port ' s FIFO queue for that thread.
This means. Completion packet is released to a thread, and the system releases the last (most recent) thread A Ssociated with this port,
passing it the completion information for the oldest I/O completion.

When a socket is associated with a completion port, the blocking state is not updated until the packet is removed from the completion port.
A thread uses the GetQueuedCompletionStatus () function to wait until the packet is queued above the completion port, rather than waiting for the asynchronous I/O operation to complete.
Blocking threads above the I/O completion port are freed in the last-in-first-out order, however, packets are popped from the completion port in FIFL order to wait for the thread to process.
This means that when a packet pops up from the queue, it will be the last freed thread in the thread pool.


Although any number of threads can call GetQueuedCompletionStatus for a specified I/O completion port,
When a specified thread calls GetQueuedCompletionStatus the first time, it becomes associated with the specified I/O compl Etion Port until one of three things occurs:
1 the thread exits,
2 specifies a different I/O completion port
3 Closes the I/O completion port
In the other words, a single thread can is associated with, at most, one I/O completion port.

When the specified thread calls GetQueuedCompletionStatus () for the first time, the thread is associated with the specified completion port until any of the following events occur:
1 the thread exits,
2 specifies a different I/O completion port
3 Closes the I/O completion port
In other words, a thread is associated with at most one completion port

When a completion packet was queued to an I/O completion port, the system first checks how many threads associated with tha T Port is running.
If the number of threads running is less than the concurrency value, one of the waiting threads (the most recent one) is Allowed to process the completion packet.
When a running thread completes its processing, it typically calls GetQueuedCompletionStatus again, at which point it eith ER returns with the next completion packet or waits if the queue is empty.

When a completion Packet is queued at the completion port, the operating system first checks the running threads that are associated with the completion port in the thread pool.
When the number of threads that are running is less than the specified concurrency value, the waiting thread is allowed to process completion Packet.
When a running thread finishes processing completion Packet, GetQueuedCompletionStatus () is called again to process the next completion Packet. If the wait queue is empty, GetQueuedCompletionStatus () will block.


Threads can use the PostQueuedCompletionStatus function to place completion packets in an I/O completion port ' s queue.
By doing so, the completion port can is used to receive communications from other threads of the process, in addition to R eceiving I/O completion packets from the I/O system.
The PostQueuedCompletionStatus function allows an application to queue it own special-purpose completion packets to the I /O completion port without starting an asynchronous I/O operation
This was useful for notifying worker threads of external events, for example.


Threads and Concurrency

The most important property of an I/O completion port to consider carefully is the concurrency value.
The concurrency value of a completion port was specified when it was created with CreateIoCompletionPort via the Numberofcon Currentthreads parameter.
This value limits the number of runnable threads associated with the completion port.
When the total number is runnable threads associated with the completion port reaches the concurrency value,
The system blocks the execution of any subsequent threads associated with that completion port until the number of RUNNABL E threads drops below the concurrency value.

The most important attribute in the completion port and is carefully used is the concurrency value.
Concurrency values are set by the NumberOfConcurrentThreads parameter when you create a IOCP object using the CreateIoCompletionPort () function.
The concurrency value limit allows the maximum number of threads to run concurrently on the completion port.
When the number of running threads is greater than the concurrency value, the system blocks any subsequent threads. Until the number of threads running is less than the concurrency value.


The most efficient scenario occurs when there is completion packets waiting in the queue, but no waits can Sati Sfied because the port has a reached its concurrency limit.
Consider happens with a concurrency value of one and multiple threads waiting in the GetQueuedCompletionStatus fu Nction call.
In this case, if the queue is always have completion packets waiting, when the running thread calls Getqueuedcompletionsta Tus, it won't block execution because, as mentioned earlier, the thread queue is LIFO.
Instead, this thread would immediately pick up the next queued completion packet.
No Thread Context switches would occur, because the running thread is continually picking up completion packets and the Other threads is unable to run.

The most efficient scenario for using the completion port is that there is no waiting thread after the thread that completes the port packet reaches the concurrency value.
What happens when a thread calls GetQueuedCompletionStatus () when the concurrency value is reached?
When a packet arrives at the completion port, a running thread calls the GetQueuedCompletionStatus () function, which does not block immediate execution and continues processing the next packet. Because the thread is the LILO LIFO order.
Because a running thread can immediately handle the completion port of the packet, all other threads will not run because there is no thread context switch.

I/O completion Ports

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.