Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. A quesion about Asynchronous Device I/O

A quesion about Asynchronous Device I/O

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    zengkun100
    wrote on last edited by
    #1

    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

    P L 2 Replies Last reply
    0
    • Z zengkun100

      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

      P Offline
      P Offline
      Perspx
      wrote on last edited by
      #2

      I don't know why it is different in XP and Vista, but specifying the FILE_FLAG_OVERLAPPED flag in CreateFile() 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

      Z 1 Reply Last reply
      0
      • Z zengkun100

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        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

        Z 1 Reply Last reply
        0
        • P Perspx

          I don't know why it is different in XP and Vista, but specifying the FILE_FLAG_OVERLAPPED flag in CreateFile() 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

          Z Offline
          Z Offline
          zengkun100
          wrote on last edited by
          #4

          Thank you! I'll read this article :)

          A Chinese VC++ programmer

          1 Reply Last reply
          0
          • L Lost User

            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

            Z Offline
            Z Offline
            zengkun100
            wrote on last edited by
            #5

            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

            L 1 Reply Last reply
            0
            • Z zengkun100

              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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              :) Glad I could help

              Sohail Please mark helpful answers

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups