Free Web Hosting | free host | Free Web Space | BlueHost Review

WST 25 - Overlapped IO
Home Up Interests Downloads (New) My Resume Links Feedback

 

Interests
Downloads (New)
My Resume
Links
WST
Feedback

Hi all,

Today we will look at overlapped (asynchronous) IO operations and how they are used to develop efficient multithreaded applications. Overlapped or asynchronous (we will use overlapped only from now on) operations revolve around an structure, creatively named OVERLAPPED.

This structure is heart and soul of overlapped io operations. You can lookup the details of this structure in MSDN. The first thing to do for overlapped operation is to get a handle to your IO resource (Files, sockets, named pipes, communication resource etc.) and specify FILE_FLAG_OVERLAPPED as the flag in CreateFile() function.

You can lookup the details of this flag in MSDN. This flag makes sure that the handle you got for IO, is capable of overlapped (and even simultaneous) IO operations.

Next thing you do is to read/write to the resource. But in the ReadFile() or WriteFile() functions you have to give a pointer to an OVERLAPPED structure.

The most important field of this structure that you have to fill is the hEvent parameter. This is a handle to an event that you should create. This event gets fired when the overlapped operation finishes.

So here's how you will go about doing the overlapped operation. You will create an event and put it's handle in the overlapped structure's hEvent field. Next, you call ReadFile/WriteFile function with the last parameter being the pointer to the overlapped structure. The function will pass if the underlying device has data to send to you or has immediately accepted the data from you (depending on Read/WriteFile).

But, if the underlying device/driver does not have data, it will fail the read/write operation and the GetLastError() will return 997 (overlapped IO operation in progress). This is an indication for you that the IO operation is currently underway (and has NOT failed). Now you can see the obvious advantage of overlapped IO. You can do any other operation when the underlying overlapped operation is going on. When the IO operation finishes, the event that you created, get's fired, and you can use WaitForSingle(Multiple)Objects() functions to know this.

You should understand that Read/WriteFile() functions return immediately upon an overlapped operation. This gives you an opportunity to do something else while the IO operation is underway.

You can also use GetOverlappedResults() function to wait for the IO operation and upon completion of it, also get the result of it (bytes transferred etc.). There is also a macro (HasOverlappedIoCompleted) that can tell you whether the operation has completed.

You should lookup details of all these and more overlapped functions in MSDN. Overlapped IO is the basic way in which asynchronous operations take place in Windows. Try to experiment with different types of overlapped IO (sockets, serial ports, file etc.).

Exercises:

1. Study the details of Overlapped IO in MSDN.

2. Write a socket based client/server application which uses overlapped IO for better performance.

3. Lookup the details of IO Completion Ports in MSDN.

IO completion ports is the next step in high performance overlapped/multithreaded IO. We will look at it next week.

Thanks for your time,

-Farooque

 

 

HyperCounter
Bpath Counter