Overlapped I/O Learning

Source: Internet
Author: User
Tags readfile

The articles I 've compiled on the Internet are just a note for my understanding and memory,
There is no value for exploitation. It is purely Learning and Memory. plagiarism or learning is just clear to people.
The main task is to develop your own programs.

I/O device processing will inevitably stop the main program and wait for the completion of I/O,
There are

Method 1: Use another thread for I/O. This solution is feasible but troublesome.

Method 2: Use overlapped I/O.
As the book says: "overlapped
I/O is a Win32 technology,

You can ask the operating system to send data for you and notify you when the transfer is complete.

This technology enables your program to continue to process transactions during I/O operations.

In fact, the operating system uses threads to complete overlapped I/O.

You can get all the benefits of the thread without the need to pay any painful price ".

How to Use overlapped I/O:

Specifies the overlapped mode when performing I/O operations.
Use createfile
(), And set its 6th parameters to file_flag_overlapped,
Is to use overlapped to construct or open the file;
If
Overlapped, a pointer must be provided for the 5th parameters of readfile () and writefile,
Point to an overlapped structure.
Overlapped is used to record some information about the file currently being operated.

// Function: Read 1500 bytes from the specified file location 300
Int main ()
{
Bool
RC;
Handle hfile;
DWORD
Numread;
Overlapped overlap;
Char
Buf [2, 512];
Char
Szpath = "x: // xxxx/xxxx ";


// Check the system and determine whether overlapped is supported. (The operating system above NT supports overlapped)

Checkosversion ();
//
Open a file in overlapped Mode
Hfile = createfile (
Szpath,

Generic_read,

File_pai_read | file_pai_write,

Null,

Open_existing,

File_flag_overlapped,

Null

);

// The overlapped structure starts to 0.

Memset (& overlap, 0, sizeof (overlap ));

// The specified file location is 1500;
Overlap. offset =
1500;

Rc =
Readfile (hfile, Buf, 300, & numread, & overlap );

// Because it is an overlapped operation, readfile will immediately return (false) after the read file request is put into the read queue ),

// It will not be returned until the file is read (true)
If (RC)

{

// The file is read, and RC is true.
//
Or when the data is put into the cache, or the operating system thinks it can get data quickly, RC is true

}
Else

{
If (getlasterror () =
Error_io_pending)

{// When the error is error_io_pending, it means that the file reading operation is still in progress.

// Wait until the file is read

Waitforsingleobject (hfile,
Infinite );

Rc =
Getoverlappedresult (hfile, & overlap, & numread, false );

// The functions completed by the preceding two statements are equivalent to those of the following statement:

//
Getoverlappedresult (hfile, & overlap, & numread, true );

}

Else

{

// Error
}

}
Closehandle (hfile );
Return
Exit_success;
}

In actual work, if there are several operations on the same file,
What should I do? We can use the event provided in the overlapped structure to solve the above problems.
Note that the event object you use must be of the manual type; otherwise, competition may occur.
For the reason, see p159.
Int
Main ()
{
Int I;
Bool
RC;
Char szpath = "x: // xxxx/xxxx ";
//
Open a file in overlapped Mode
Ghfile = createfile (
Szpath,

Generic_read,

File_pai_read | file_pai_write,

Null,

Open_existing,

File_flag_overlapped,

Null

);
For (I = 0; I <max_requests; I ++)

{

// Read the same file in the overlapped Mode

// Check how the queuerequest function is run. Read 16384 blocks each time.

Queuerequest (I, I * 16384, read_size );

}
// Wait until all operations are completed;

// Implicit condition: When an operation is completed, the corresponding event object will be activated.

Waitformultipleobjects (max_requests, ghevents, true,
Infinite );
// Close the operation
For (I = 0;
I <max_requests; I ++)

{
DWORD
Dwnumread;
Rc =
Getoverlappedresult (

Ghfile,

& Goverlapped [I],

& Dwnumread,

False

);

Closehandle (goverlapped [I]. hevent );

}
Closehandle (ghfile );
Return
Exit_success;
}

// After the read operation is complete, goverlapped [nindex]. hevent will be triggered
Int queuerequest (INT nindex,
DWORD dwlocation, DWORD dwamount)
{

// Construct a manual event object
Ghevents [nindex] = createevent (null,
True, false, null );

// Place this event object in the overlapped Structure
Goverlapped [nindex]. hevent =
Ghevents [nindex];
Goverlapped [nindex]. offset =
Dwlocation;
For (I = 0; I <max_try_count;
I ++)
{

// The file ghfile is unique.
Rc = readfile (ghfile,
Gbuffers [nindex], & dwnumread, & goverlapped [nindex]);

If (RC)
Return
True;
Err =
Getlasterror ();
If (ERR =
Error_io_pending)

{

// When the error is error_io_pending, it means that the file reading operation is still in progress.

Return true;

}
//
Handle recoverable errors
If (ERR =
Error_invalid_user_buffer
|
Err =
Error_not_enough_quota
|
Err =
Error_not_enough_memory)

{
Sleep (50 );


Continue; // retry

}
//
If getlasterror () does not return the errors listed above, discard

Break;
}

Return-1;

}

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.