Device I/O overlapped

Source: Internet
Author: User

Overlapped, as its name implies, is overlapping. At first glance, it may be strange, overlap? Who and who overlap? Does it seem to have this concept in Win32 programming? To discuss this issue, we need to trace the access to device I/O.

In Win32, users cannot directly access the hardware as before, so that this layer is a "black box" for developers and provides a set of corresponding API interfaces. developers are allowed to develop based on the provided interfaces, and lower-layer access is handed over to the driver or kernel. in Win32, the concept of a device is far beyond the range of moniter and printer. It may include files, directories, serial ports, parallel ports, pipelines, and the console. naturally, when we want to access this device, our first step is to open this device. The Win32 API provides createfile. For specific usage, see msdn, some of the parameters are used to determine whether the device already exists (dwcreationdisposition), whether to enable it in an exclusive mode, and so on. we may have come up with this idea: Ah, since we are dealing with devices, the device speed is so slow, and the CPU speed is so fast, how should we coordinate the two? For example, if I want to access the information on a floppy disk, even if it is read in a second, it is actually a great waste of CPU resources. yes, there is indeed this problem. Since there is a problem, we have to solve it. The Microsoft solution is here our discussion question: what is the meaning of the overlapped character? In fact, it means that when the program is waiting for device operation, it can continue to do so without blocking to that place and waiting for the device operation to return, this leads to the overlap between program running and device operation time. yes, it's amazing. How can the program know when the device operation is complete...

At this point, we need to introduce the concept of multithreading. in fact, I believe that everyone has a certain understanding of multithreading. In fact, multithreading is mainly a synchronization problem. How can we coordinate these threads that "run without authorization, win32 provides two functions: waitforsingleobject and waitformultiobject. Win32 provides a set of objects specifically used for synchronization, including critical section, mutex, semaphore, and event, most of these objects belong to kernel objects. The biggest difference between these objects and general objects is that they belong to a data structure maintained by the system kernel. programs cannot directly access them. these objects have two forms. Here we can call them signal-free and signal-free. in this way, when we use the wait function, we can determine whether the program is blocked in the wait Based on the signal. Simply put, when we call a function: waiforsingobject (Event
A); If event A has a signal, the program will run down. If there is no signal, the program will be blocked at the current position, waiting for it to become a signal. take an image of particles. For example, a thread is a car running on a highway. The wait function is used to open the car to a crossroads and wait for the signal lights to be green, if so, the car will continue to run down. Otherwise, sorry, please wait there and wait until the signal turns green ..

Here, the most convenient thing for me to use is the event object, because we can easily operate on it, for example, setevent makes it a signal, the resetevent makes it non-signal-free. Of course, it is convenient for others, such as mutex, to make the program unable to be loaded repeatedly ..

In this way, when we want to access the device asynchronously (overlapped), we only need to first use the (overlapped) Flag during creatafile, and then read and write operations (corresponding to writefile and readfile) you can also use this sign...

Take a look at the following section:

/// Place 1 /// receives the specified character in an overlapping manner to check whether the function has been read successfully

Freadstat = readfile (hcom, lpblock, dwlength, & dwlength, & osread );

If (! Freadstat)

{

File: // The operations that overlap in the background

If (getlasterror () = error_io_pending)

{

/// 2 places

File: // wait for 1 S. If the received event is in the signal state, the overlapping operation is completed and the timeout occurs.

/// If (waitforsingleobject (osread. hevent, 1000) = wait_timeout)

Dwlength = 0;

}

Else dwlength = 0; // exception

}

In this way, when the program reads data asynchronously, no matter whether the device has completed the operation, the program will immediately run down without waiting for its return (if it is synchronous, the thread will be blocked in this place ). in this way, we can do our own thing in the second place without having to worry about the device (This achieves the overlap of time ), it will not be used until we have to wait for three pieces of data to come in before further processing.

Wait for the device ..

This operation undoubtedly improves the efficiency and makes the coordination between the program and the device a lot. Of course, overlapped has its own new thread for processing, which is undoubtedly...

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.