Overlapping I/O waiting for overlapping I/O [Series 1]

Source: Internet
Author: User

The key difference between synchronous I/O and asynchronous I/O is whether the thread is blocked after an I/O request is sent. When a thread sends a device I/O Request, the thread is mounted until the device completes the I/O request. This is called synchronous I/O. For asynchronous I/O, when the thread submits a device I/O request, it can continue to run, when the kernel completes the I/O request, it will notify the thread that I/O has been completed. Because device I/O is the slowest among most other operations performed by the computer, asynchronous I/O can greatly improve the new performance of the program in most cases, of course, it may not be used in some cases.

 

In Windows, there are four asynchronous I/O technologies, which differ in the method for starting an I/O operation and when the operation is completed:

  • Waiting for overlapping I/O. The thread continues execution after it sends an I/O request. When the thread needs the result of an I/O request to continue, it either waits for the file handle or an event specified in the overlapped structure. According to different waiting objects, the overlapping I/O of waiting file handles and the overlapping I/O of waiting events are divided. Waiting for overlapping I/O of the file handle is not very useful, because it needs to wait for all the operations associated with the file handle to complete, synchronization I/O is almost the same. Generally, we use the overlapping I/O of the wait event and the human event to implement the wait.
  • Completes the overlapping I/O of the routine. Completion routine, this term seems very advanced. To put it bluntly, it is a destroy function. This method allows us to send multiple I/O requests to a device. These I/O Requests carry a callback function, that is, the completion routine. When the I/O request is complete, if the thread is in the reminder status, the system will notify the thread to call the callback function in its asynchronous queue to process the completed I/O.
  • I/O complete port. The I/O completion port allows us to send multiple I/O requests to a device at the same time. It allows one thread to send an I/O request and the other thread to process the result. This is different from the completion routine. This technology has good scalability and flexibility, and is the best method in asynchronous I/O. The I/O completion port has a set of thread pool scheduling policies, which frees us from the trouble of maintaining multithreading.

 

Next let's take a look at the first method in overlapping I/O-overlapping I/O (waiting ). Start with understanding the overlapping structure:

"Overlapped" indicates that the time for executing an I/O Request overlaps with the time for other threads to execute other tasks. The OVERLAPPED structure is as follows:

   

typedef     ULONG_PTR InternalHigh;             DWORD OffsetHigh; } OVERLAPPED, *LPOVERLAPPED;

Parameter description:

Offset and OffsetHigh

The two members constitute a 64-bit offset. If the current operation is a file device, it indicates where the current file I/O operation should start. When asynchronous I/O is executed, the system ignores the Kernel File pointer associated with the file, but uses the starting offset specified in OVERLAPPED, this avoids confusion during asynchronous calls to the same object. For non-file devices, you must set these two parameters to 0. Otherwise, the I/O request will fail and the error ERROR_INVALID_PARAMETER will be returned.

HEvent

In different overlapping I/O operation methods, hEvent settings are different. In the overlapped I/0 operations that can be waited for, if you are waiting for the file handle of the operation, you do not need to set the hEvent. If you want to wait through the hEvent In the OVERLAPPED structure, or the I/O completed port method, you need to set this parameter as a manual event, note that automatic events cannot be performed. If it is an I/O completion routine method, hEvent is generally used to pass the information of the I/O operation.

Internal

Used to save the error codes of the processed I/O requests. When an asynchronous I/O request is sent, the device driver sets internal to STATUS_PENDING, indicating that the operation has not started yet.

InternalHigh

When an asynchronous I/O request is completed, it is used to save the number of transmitted bytes.

Notes for asynchronous I/O:

1. Before the asynchronous I/O request is completed, the data cache and OVERLAPPED mechanism used for sending I/O requests cannot be moved or destroyed. Otherwise, the memory will be damaged. Because the addresses we send to the I/O devices are the addresses of the two data, and the I/O devices do not know that they have been destroyed.

2 when executing asynchronous I/O, the device does not have to process the I/O requests in the queue in the first-in-first-out mode.

3. When an asynchronous I/O operation is executed, the return value of related operations such as file read/write is FALSE, which does not necessarily indicate a failure. GetLastError must be called. If GetLastError returns ERRO_IO_PENDING, it indicates that the I/O request has been successfully added to the I/O queue and will be completed later.

 

When we use ReadFile or WriteFile to send a read/write request to the file, the function will return immediately. In most cases, I/O operations are not completed at the time of return, so FALSE is returned. Naturally, we do not know the number of transmitted bytes. Therefore, we need another method to obtain the number of bytes transmitted when the file I/O is complete. This function is GetOverlappedResult:

  

                    __in  LPOVERLAPPED lpOverlapped,                     __out LPDWORD lpNumberOfBytesTransferred,                     __in  BOOL bWait                       );

 

The combination of hFile and lpOverlapped can uniquely represent an I/O operation. When bWait is set to True, the function will wait until this I/O operation is completed before returning. In this case, lpNumberOfBytesTransfferred is the number of bytes transmitted. If bWait is set to False, the function will return immediately. If False is returned and GetLastError () is ERRO_IO_PENDING, it indicates that I/O has not been completed and I/O needs to be checked round-robin.

 

To cancel an I/O request that has been added to the I/O queue but has not been processed, you can call CancelIoEx to cancel the request.

 

CancelIoEx can not only cancel the I/O requests of related files issued by this thread, but also cancel the I/O requests to be processed by other threads outside the calling thread. This function will cancel all requests associated with the lpOverlapped parameter in the I/O requests to be processed by the hFile device. However, each pending I/O Request has a specific OVERLAPPED structure. Therefore, if lpOverlpaped is not empty, CancelIoEx can only cancel one request at a time. If lpOverlpaped is NULL, All I/O requests in the hFileI/O Request queue are canceled.

 

In this example, events are used to wait for overlapping I/O. This program asynchronously reads data from the input file and writes data to the output file asynchronously. The program uses the multi-buffer method for file conversion. Assume that N buffers are used for input and output respectively, then, N input buffering and N output buffering require N input overlapping structures and N output overlapping structures to correspond to them. Initially, N input buffers are used to separate overlapping read operations. Then, the program uses WaitForMultipleObjects to wait for a single I/O operation to complete the event, indicating that a read or write operation is completed. When a read operation is complete, the buffer zone is copied and a write operation request is initiated. After writing the data, you can perform the next read operation request.

 

 

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.