Handling IRPs 5: Synchronous I/O Responses

本文深入探讨了Windows操作系统中同步I/O操作的工作原理,包括如何通过检查IoCallDriver返回的状态来判断请求是否为同步或异步完成。详细解释了同步I/O操作与异步I/O操作的区别,以及在不同情况下如何获取I/O请求的完成状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Synchronous I/O Responses

Although the Windows operating system is designed for asynchronous I/O, most applications issue synchronous I/O requests. Drivers can also issue both synchronous and asynchronous requests and can respond to requests either synchronously or asynchronously.

To determine whether a request was completed synchronously or asynchronously, a driver checks the status returned by IoCallDriver, as the following code sample shows:

KEVENT   event;

 

KeInitializeEvent(&event, NotificationEvent, FALSE);

 

IoCopyCurrentIrpStackLocationToNext(Irp);

 

IoSetCompletionRoutine(Irp,

                       CatchIrpRoutine,

                       &event,

                       TRUE,

                       TRUE,

                       TRUE

                       );

 

status = IoCallDriver(DeviceObject, Irp);

 

//

// Check for synchronous or asynchronous completion.

//

 

    if (status == STATUS_PENDING) {

    // Code to handle asynchronous response

    // omitted for now.

    }

 

return status;

 

The driver initializes an event, sets the I/O stack location, sets an IoCompletion routine, and calls IoCallDriver to forward the IRP. The status returned by IoCallDriver indicates whether lower drivers are handling the IRP synchronously or asynchronously. If the request is being handled asynchronously, IoCallDriver returns STATUS_PENDING. If lower drivers respond synchronously, IoCallDriver returns the completion status that was returned by the next lower driver. As the code sample shows, the driver simply returns that same completion status.

When an IRP is completed synchronously, the driver returns the IRP’s completion status from its dispatch routine. Drivers above it in the device stack can get the status in either of two ways:

·         In the dispatch routine, from the value returned by IoCallDriver.

·         In the IoCompletion routine, from the IoStatus.Status field of the IRP.

 

When the I/O Manager calls the driver’s IoCompletion routine, the driver owns the IRP and thus can access the IoStatus.Status field. If the driver does not set an IoCompletion routine, it does not own the IRP after it calls IoCallDriver and therefore must not access fields within the IRP. 

Figure 4 shows the two ways a driver or application can get the status of an IRP. For ease of explanation, the figure shows the IoCompletion routines in the same I/O stack location as the parameters with which they are called, instead of one location lower.

Figure 4. Status returned by IoCallDriver and available to IoCompletion routine

On the left side of Figure 4, the IoCallDriver routine returns the completion status reported by the next lower driver. On the right, the IoCompletion routines read the status from the IoStatus.Status field of the IRP. If the IRP completes synchronously, the IoCompletion routine for each driver is called before IoCallDriver returns, so the status value is available to the IoCompletion routine before it is available to the dispatch routine.

Figure 4 shows that driver C returns STATUS_SUCCESS, driver B returns STATUS_RETRY, and driver A returns STATUS_ERROR. The final status of the IRP is available only to the initiator of the request; other drivers can read only the status returned by the next-lower driver.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值