A quesion about Asynchronous Device I/O
-
Hello, this is a very simple program to test asynchronous device io. But when this program runs on Vista, io is always asynchronous; when it is XP, io is always synchronous. I don't know why? Does Vista's io more efficient than XP? Thank you! :)
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE g_hIOCP = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (g_hIOCP == NULL)
{
cout<<"can not create io completion port"<<endl;
return 1;
}
// open file with FILE_FLAG_OVERLAPPED flag to ensure that io is asynchronous
HANDLE hFile = CreateFile(L"d:\\test.abc", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hFile == NULL)
{
cout<<"can not open file"<<endl;
}
HANDLE hTest = ::CreateIoCompletionPort(hFile, g_hIOCP, (ULONG_PTR)hFile, 0);
if (hTest != g_hIOCP)
{
cout<<"failed to associate file to completion port"<<GetLastError()<<endl;
return 0;
}
for(int i = 0; i < 5; ++i)
{
OVERLAPPED ov = {0};
char szBuf[128];
DWORD lpNumberOfBytesRead = 0;
BOOL b = ::ReadFile(hFile, szBuf, 128, &lpNumberOfBytesRead, &ov);
// when run on Windows Vista, ReadFile always returns FALSE, and GetLastError returns ERROR_IO_PENDING
if (b == FALSE)
{
cout<<"ReadFile failed,last error code is:"<<::GetLastError()<<endl;
if (::GetLastError() == ERROR_IO_PENDING)
{
cout<<"read file asynchronously"<<endl;
}
}
// when run on Windows XP, ReadFile always returns TRUE
else
{
cout<<"read file synchronously"<<lpNumberOfBytesRead<<"bytes"<<endl;
}
}
return 0;
}A Chinese VC++ programmer
-
Hello, this is a very simple program to test asynchronous device io. But when this program runs on Vista, io is always asynchronous; when it is XP, io is always synchronous. I don't know why? Does Vista's io more efficient than XP? Thank you! :)
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE g_hIOCP = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (g_hIOCP == NULL)
{
cout<<"can not create io completion port"<<endl;
return 1;
}
// open file with FILE_FLAG_OVERLAPPED flag to ensure that io is asynchronous
HANDLE hFile = CreateFile(L"d:\\test.abc", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hFile == NULL)
{
cout<<"can not open file"<<endl;
}
HANDLE hTest = ::CreateIoCompletionPort(hFile, g_hIOCP, (ULONG_PTR)hFile, 0);
if (hTest != g_hIOCP)
{
cout<<"failed to associate file to completion port"<<GetLastError()<<endl;
return 0;
}
for(int i = 0; i < 5; ++i)
{
OVERLAPPED ov = {0};
char szBuf[128];
DWORD lpNumberOfBytesRead = 0;
BOOL b = ::ReadFile(hFile, szBuf, 128, &lpNumberOfBytesRead, &ov);
// when run on Windows Vista, ReadFile always returns FALSE, and GetLastError returns ERROR_IO_PENDING
if (b == FALSE)
{
cout<<"ReadFile failed,last error code is:"<<::GetLastError()<<endl;
if (::GetLastError() == ERROR_IO_PENDING)
{
cout<<"read file asynchronously"<<endl;
}
}
// when run on Windows XP, ReadFile always returns TRUE
else
{
cout<<"read file synchronously"<<lpNumberOfBytesRead<<"bytes"<<endl;
}
}
return 0;
}A Chinese VC++ programmer
I don't know why it is different in XP and Vista, but specifying the
FILE_FLAG_OVERLAPPED
flag inCreateFile()
means that it is an asynchronous call; however, this article may be of use. [Edit] Apologies I didn't see your FLAG_FILE_OVERLAPPED remark in your code sample - the article may still be useful to you though :) Regards, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
-
Hello, this is a very simple program to test asynchronous device io. But when this program runs on Vista, io is always asynchronous; when it is XP, io is always synchronous. I don't know why? Does Vista's io more efficient than XP? Thank you! :)
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE g_hIOCP = ::CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, 0);
if (g_hIOCP == NULL)
{
cout<<"can not create io completion port"<<endl;
return 1;
}
// open file with FILE_FLAG_OVERLAPPED flag to ensure that io is asynchronous
HANDLE hFile = CreateFile(L"d:\\test.abc", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (hFile == NULL)
{
cout<<"can not open file"<<endl;
}
HANDLE hTest = ::CreateIoCompletionPort(hFile, g_hIOCP, (ULONG_PTR)hFile, 0);
if (hTest != g_hIOCP)
{
cout<<"failed to associate file to completion port"<<GetLastError()<<endl;
return 0;
}
for(int i = 0; i < 5; ++i)
{
OVERLAPPED ov = {0};
char szBuf[128];
DWORD lpNumberOfBytesRead = 0;
BOOL b = ::ReadFile(hFile, szBuf, 128, &lpNumberOfBytesRead, &ov);
// when run on Windows Vista, ReadFile always returns FALSE, and GetLastError returns ERROR_IO_PENDING
if (b == FALSE)
{
cout<<"ReadFile failed,last error code is:"<<::GetLastError()<<endl;
if (::GetLastError() == ERROR_IO_PENDING)
{
cout<<"read file asynchronously"<<endl;
}
}
// when run on Windows XP, ReadFile always returns TRUE
else
{
cout<<"read file synchronously"<<lpNumberOfBytesRead<<"bytes"<<endl;
}
}
return 0;
}A Chinese VC++ programmer
-
I don't know why it is different in XP and Vista, but specifying the
FILE_FLAG_OVERLAPPED
flag inCreateFile()
means that it is an asynchronous call; however, this article may be of use. [Edit] Apologies I didn't see your FLAG_FILE_OVERLAPPED remark in your code sample - the article may still be useful to you though :) Regards, --Perspx"The Blue Screen of Death, also known as The Blue Screen of Doom, the "Blue Screen of Fun", "Phatul Exception: The WRECKening" and "Windows Vista", is a multi award-winning game first developed in 1995 by Microsoft" - Uncyclopedia Introduction to Object-Oriented JavaScript
Thank you! I'll read this article :)
A Chinese VC++ programmer
-
There is a MSKB on Asynchronous IO with details on Synchronous I/O occuring even when the file is opened using FILE_FLAG_OVERLAPPED attribute. See if this helps - Asynchronous I/O Still Appears to be Synchronous[^]
Sohail
I blindly believe that if FILE_FLAG_OVERLAPPED flag was set, then all the i/o operation will be processed asynchronously, but it seems that not quite right. Thank you! :)
A Chinese VC++ programmer
-
I blindly believe that if FILE_FLAG_OVERLAPPED flag was set, then all the i/o operation will be processed asynchronously, but it seems that not quite right. Thank you! :)
A Chinese VC++ programmer