Asynchronous I/O

After sending asynchronous I/O using Readfile()/Writefile() / Deviceiocontrol() from user mode, and wait for I/O completion using waitforsingleobject(event,…), how I/O gets completed

  1. will context switch happens, i mean will thread be preempted by another thread?
  2. if context switch happens how I/O is completed?
  3. when wait is satisfied?
  4. how the system sets the event on which user is waiting when I/O completes?
  5. will the APC routine queued to APC queue of that thread, if yes when APC routine will be run?

xxxxx@gmail.com wrote:

After sending asynchronous I/O using Readfile()/Writefile() / Deviceiocontrol() from user mode, and wait for I/O completion using waitforsingleobject(event,…), how I/O gets completed

  1. will context switch happens, i mean will thread be preempted by another thread?

When the last driver in the stack calls IoCompleteRequest, the I/O
manager fires the event.  This makes the thread “ready to run”.  The
driver has the option of specifying a small priority boost in the
IoCompleteRequest call.  This is because an app that was waiting for an
I/O request is often going to submit another one right away, so we might
as well give it a shot at the CPU so it can go wait again.

 

  1. if context switch happens how I/O is completed?

I’m not sure what you are asking.  The thread is marked “ready to run”,
and then the scheduler runs.  If the waiting thread is now the highest
priority thread, then a context switch will happen.

 

  1. when wait is satisfied?

Already answered above.

  1. how the system sets the event on which user is waiting when I/O completes?

Just like any other event.  There’s no magic here.

  1. will the APC routine queued to APC queue of that thread, if yes when APC routine will be run?

Why does it need an APC?  There’s no procedure call required.  The
thread is blocked, and I/O manager unblocks it.  Just that simple.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.