Device IO Overlapped

zhaozj2021-02-11  164

Overlapped, as the name sustain, at first glance, it will be strange, overlap? Who is overlapping? It seems that there is no concept in the win32 programming? To discuss this problem, it is going to be traced back to the device I / O visit in Win32 in Win32 The user cannot access the hardware directly as before, so that this layer is a "black box" for developers, but provides a set of corresponding API interfaces. Developers to develop, Give the low-level access to Driver or kernel. In Win32, the concept of devices has far exceeds the range of Moniter, Printer, etc., which can include files, directory, serial port, parallel port, pipeline, and console. Very nature When we have to access this device, our first step is to open this device, where the Win32 API provides CreateFile, the specific use can be referred to MSDN, including some of the parameters indicating whether this device already exists (dwcreationDisposition) Whether to open a exclusive way (dwsharemode), you may have already generated this idea: Yeah, since it is dedicated to devices, then the speed of the equipment is so slow, and the speed of the CPU is so fast, how should both coordinate? Ok, give an example, I want to access the information on the floppy disk, even if it is a second, it is also a very waste of the CPU. Yes, there will be this problem, since there is Question, we have to solve, and Microsoft's solution is here our discussion topic: What is the meaning of Overlapped characters? In fact, it means that when the program is waiting for the device, you can continue to do it without Going to the return of the device operation, this has caused the overlap of the program operation and the operation time. Yes, this is the same, the magical, then how do the program know when the equipment is finished? Tell this place, we have to introduce multithreaded concepts. In fact, I believe that everyone has a certain understanding of multi-threading. In fact, multi-thread is mainly a synchronous problem, how to coordinate these threads that "run up" Win32 is provided to us is the two functions of WaitForsingleObject and WaitFormultiobject, while Win32 provides a set of objects that specifically used to synchronize, including Critical Section, Mutex, Semaphore, Event, etc., which belong to the kernel object ( Kernal Object, which is the biggest difference between these objects and general objects is that they are a piece of data that belongs to the system kernel maintenance. The program cannot access them directly. These objects have two forms, here we may wish to call it : There is signal and no signal. This way we use the wait function, you can block whether the program is blocked in Wait, as we call a function: WAIForsingObject (Event A If, if the A event has a signal, then the program runs down. If there is no signal, then the program blocks in the current position, waiting for it to become a signal. Even the particles of the image, such as thread is The car running on the highway, the role of the WAIT function is to let this car open to a crossroads, waiting for the signal light of the intersection to be green, if it is then running down, otherwise, please wait, etc. Green .. Here, I used to use it is Event this object, because we can make it easy to operate it, such as setEvent makes it become signal, and resetEvent makes it a signal Of course, some other things are also very convenient to make programs that cannot be loaded and other places .. This time we want to be asynchronously (overlapped) access device, as long as you first use (overlapped) flags when Creatafile, then Use this flag when reading and writing (corresponding is Writefile and ReadFile) ... Take a look at this paragraph: 1 receive the specified character in overlapping mode, see if the function reads successful FREADSTAT = readfile (hcom, LPBLOCK, DWLENGTH, & DWLENGTH, & OSREAD; if (!freadstat) {file: // The superimposed mode operation is in the background ... IF (getLastError () ==

ERROR_IO_PENDING) {2 File: // Wait 1s Else dwlength = 0; // Abnormal situation} This time when the program reads the information in an asynchronous manner, the program will immediately run down without waiting for it (if it is the thread that is synchronized) It will blocked in this place). In this way we can do your own things in 2, you can do your own things without devices (this will achieve time overlapping), until we need to wait 3 information and then further When we handle, we will only use the Waitfor waiting device. This will undoubtedly improve the efficiency, making the program and equipment coordination a lot, and of course Overlapped is a new thread, this is There is no doubt that...

Reference: Advanced Windows Mlutithreading Applications in Wim32

For the first time to write something, the bad place to write, please have a lot of criticism.

转载请注明原文地址:https://www.9cbs.com/read-5366.html

New Post(0)