On the concept of IO overlap in Winsock programming

Source: Internet
Author: User

I was reading the Windows Network and Communication program design (Wang Yanping) book, the overlap Io is very understanding, suddenly came out of such a concept, not a little clue. Make a collation of the current understanding.

The first understanding: OVERLAPPED, as the name implies overlap, at first glance will be very strange, overlapping? Who overlaps with whom? It seems that there is no such concept in WIN32 's programming? To discuss this issue goes back to the access to device I/O.
In WIN32, users cannot access the hardware directly as they did before, making the layer a "black box" for developers and providing a set of interfaces for the corresponding API. Let the developer develop based on the interface provided, and give the low-level access to the driver or the kernel. In WIN32, The concept of equipment has far exceeded the scope of moniter,printer and so on, presumably can include files, directories, serial ports, and ports, pipelines and consoles. Naturally, when we want to access this device, our first step is to turn on the device, where WIN32 The API provides CreateFile, which can be used to refer to MSDN, which includes parameters that indicate whether the device already exists (dwcreationdisposition) and whether it is in exclusive (dwShareMode) mode Open and so on. Everyone here may have produced this idea: Ah, since it is dealing with equipment, so the speed of the device, and the speed of the CPU so fast, how should the two be coordinated? For example, I want to access the information on the floppy disk, even if it is a second to read out, That's actually a lot of waste to the CPU. Yes, it does, and since we have a problem, we're going to solve it, and Microsoft's solution is here: what does the overlapped character mean? In fact, it means that when the program is waiting for the device to operate, it can continue to do without blocking to the place to wait for the return of the device operation, which causes the program to run and the device operation time overlap. Yes, it is, magic, then the program how to know when the operation of the device has returned ...

The second understanding of:

There is an API called ReadFile in Windows
BOOL ReadFile (
HANDLE hfile,//handle to File
LPVOID lpbuffer,//Data buffer
DWORD nNumberOfBytesToRead,//number of bytes to read
Lpdword Lpnumberofbytesread,//number of bytes read
lpoverlapped lpoverlapped//overlapped buffer
);
If we do not use the FILE_FLAG_OVERLAPPED flag at the time of CreateFile, and when the ReadFile is called, the lpoverlapped lpoverlapped parameter is set to NULL, Then readfile the call to this function will not return until the data is specified, and if it is not read, it will be blocked here. Likewise, WriteFile and ReadFile are like this. So when we read and write large files, we spend a lot of time waiting for the return of ReadFile and WriteFile. If ReadFile and WriteFile are reading and writing data to the pipeline, then it is possible to block longer and cause program performance to degrade. To solve this problem, Windows introduced the concept of overlapping IO, which is also the above ReadFile and WriteFile, if the file_flag_overlapped is set at CreateFile, Then, when calling ReadFile and WriteFile, you can pass a overlapped structure to their last argument. So the ReadFile or WriteFile call will return immediately, when you can do what you want to do, the system will automatically complete for you readfile or WriteFile, after you call ReadFile or WriteFile, you continue to do your thing, The system also helps you perform ReadFile or WriteFile operations, which is called overlap. Another benefit of using overlapping IO is that you can make several ReadFile or WriteFile calls at the same time, and then use WaitForSingleObject or waitformultipleobjects to wait for the operating system to complete the notification. After the notification signal is received, the GetOverlappedResult can be used to query the results of the IO call.
Basically, that's it. I don't know if you know that,
As for the overlapping IO in the socket, and the difference is good, the difference is that ReadFile WriteFile was replaced by WSARecv and WSASend. This involves a lot of things, in fact, the asynchronous IO mechanism of Windows is very advanced, so I recommend you to see "windows2000 Programming Insider", you can also go to see "Inside Windows2000"
When the CPU executes your code and encounters an I/O request [such as a read-write file], the system generates an interrupt to allow the CPU to complete the I/O request, and when it is finished, the system again generates an interrupt to allow the original program to continue running. It also says that the synchronization between the two is maintained by interruption. The terminal can be understood as a hardware semaphore.
This is the so-called synchronization concept, where only one I/O request can be processed in one thread
You know, an I/O operation is very time consuming, and when your code hangs and waits for I/O to finish, your thread wastes n instruction cycles.
If you want to read and write large files at the same time, the efficiency of synchronization is very low.
To solve this problem, when the CPU executes your code with an I/O request, the system is for you to open an internal thread to handle I/O requests, and your thread does not hang, but you may feel that if I am not finished, the subsequent code even if he let me execute, I do not go on?
If the following code is related to this I/O operation, then it will wait until the I/O operation is complete and I/O completion messages can be obtained by calling Waitformultiobject () and GetOverlappedResult () in a thread. And then deal with it accordingly.
But if the subsequent code doesn't have anything to do with this I/O operation, you can go at a faster pace without waiting for the IO request to complete.
This is asynchronous.
Do you want to when you have such a request that is
ReadFile (...) -1
WriteFile (...) -2
ReadFile (...) -3
If you use synchronization in the program, it is only when you complete 1 after 2 will continue to execute, 2 after the execution of 3 will continue to execute. This is the synchronization.
When asynchronous is used, when the system encounters 1 o'clock, OK, a thread is given to it to complete the IO request, and then the system continues to run 2, 3, respectively, to open two threads.
If it is a time-consuming operation, especially on the network, then the three IO requests are parallel, that is, overlapping.
Overlapping I/O is capable of processing multiple I/O at the same time with multiple threads, in fact, you can open more than one thread to handle multiple I/O, of course, the internal optimization of the system will certainly be better than your performance, hehe.
I just said a little bit. overlap [overlapped] did not analyze you from the point of view of the code. I hope you can understand the overlap Io. Look at Windows network programming, it's not a model.
Finally mention the shortcomings of the overlapping model, he opened a thread for each IO request, when there are 1000 requests, then the system processing thread context switch is also very time-consuming, so this also caused the completion of the port model IOCP, with the thread pool to solve the problem, I'm not going to say much.

On the concept of IO overlap in Winsock programming

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.