Asynchronous IO in Windows

Source: Internet
Author: User

The so-called asynchronous Io is when the thread is driven to the deviceProgramAfter an IO request is sent, the thread does not wait for processing, but continues to execute. the device driver completes the actual Io operation and then notifies the program in other ways.

Note:

If we send an asynchronous Io request to the device driver, the device driver may not necessarily process the IO request asynchronously. Sometimes, the request will be synchronized. After an I/O request is sent, the operating system checks whether our data is in the system cache. If it is in the cache, the operating system will not send this request to the device driver, the program that will directly copy the data from the cache to directly complete the IO request.

 

If the IO operation is performed in synchronous mode, readfile and writefile will return a non-0 value. If it is asynchronous or an error occurs, false will be returned.

1. overlapped

Overlapped is overlapped, that is, a thread can do other things during reading. Specifically, I have written this

Http://www.cnblogs.com/linyilong3/archive/2012/05/03/2480451.html

What I do not know before is that offset and offsethight are always set to 0, and then the buffer goes to the maximum. Today, we can see that these two numbers constitute a 64-bit number, assume that the offset value is set to 10 and the offsethight value is set to 0, the data will be read from 10

 

The position of the overlapped Io operation needs to be specified each time. In synchronous Io, after an IO operation is completed, the file will move the offset pointer, next time we perform Io operations on the file again, the file will start from the end of the last operation. However, in asynchronous Io, If we initiate two Io operations, the second Io operation may be executed and completed first. At this time, the second Io request may start from the starting position, and the first operation may start from the end position of the second Io request, this will cause confusion.

 

Notification waiting for Io completion:

1. waitforsingleobject: The device Kernel Object

 

2. you can use the hevent in overlapped. This example in Windows core programming mainly involves reading multiple IO requests, for example, waitforsingleobject cannot know which task is completed, you can create a separate hevent for each event.

 

2. I/O reminders


In Windows core programming, I/O can be reminded that I/O is very bad and should be avoided. He/she uses readfileex or writefileex. These two functions have one more parameter than the original function and are a function pointer, this function is the function that requires callback after the IO request is completed, but these callback functions do not call back immediately, but wait until the thread sets itself as a reminder. These six functions: sleepex, waitforsingleobjectex, waitformultipleobjectsex, signalobjectandwait, getqueuedcompletionstatusex, and msgwaitformultipleobjectsex. When the thread enters the reminder status and the IO Request queue is completed, the operating system will start to call these callback functions, until there is no message in the Completion queue, we will return the function that can be reminded to call,We can use this feature to continue the thread with paused execution.

 

Disadvantages:

There must be a callback function, addCodeComplexity, because these callback functions do not have enough context information related to a problem, so some information can only be placed in global variables (originally I wanted to use C ++ classes to solve this problem, but I thought about it later. If the local variable is to be a member variable of the class, the complexity of the class will be increased)

 

The thread that sends an IO request simultaneously processes the completion notification. If a thread sends multiple requests, it must process multiple requests. The load is not balanced and the program scalability is not good.

 

Complete Port:

The user creates a port and associates the handle of the device with the port. After the IO is complete, the user will notify the port and then process it.

 

The completion port will create a thread based on the number of threads specified by the user, generally the number of CPU * 2. because too many threads are created, most of the time will be spent on thread context switching.

 

The completion port stores a total of four lists, one is the IO Completion queue, the other is the waiting thread queue, the other is the release thread list and the list of paused threads, the pause thread list is different from the wait thread queue. The pause thread list uses functions to suspend the thread. If the user wakes up the thread, the thread will be added to the release list, in the waiting thread queue, the user calls getqueuedcompletionstatus to wait for the IO message. The user can also add the thread to the IO Completion queue through postqueuedcompletionstatus.

 

You can overlapped. hevent = (handle) (dword_ptr) overlapped. hevent | 1)

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.