首先来说中断
计算机的中断分为软中断和硬中断,即IRQL
和DIRQL
,共32个级别,从0~31级别依次提升,0~2属于软中断
一般线程运行于PASSIVE_LEVEL
级别,如果不想在运行时切换到其他线程,可以将中端级别提升致DISPATCH_LEVEL
,但线程在这个中端级别下运行无法使用分页内存,因为无法触发分页中断。可能有些内存地址访问不了,蓝屏。
当硬件的中断信号发给CPU
之后,CPU会把IRQL
级别提升致DIRQL
,操作系统开始执行ISR
(中断服务例程)。流程大概是这个样子
但是如果ISR
的代码过于复杂,我理解的是首先会影响系统的正常运行,PASSIVE_LEVEL
级别的代码根本不会执行。其次影响其他硬件中断的响应。所以这时引入了DPC
执行例程。ISR
会把一些不重要的代码,放入DPC
队列,当执行程序从DIRQL
降到DISPATCH_LEVEL
时,会自动执行DPC
队列里面的代码。
ISR:Interrupt Service Routines
(中断服务例程
必须马上做的事情,比如硬件中断,响应鼠标点击
DPC:Deferred Procedure Call Details
(延迟过程调用
可以推迟做的事情,比如大数据拷贝,U盘拷贝电影。
APC: Asynchronous Procedure Calls
(异步过程调用
An asynchronous procedure call (APC) is a function that executes asynchronously in the context of a particular thread. When an APC is queued to a thread, the system issues a software interrupt. The next time the thread is scheduled, it will run the APC function. An APC generated by the system is called a kernel-mode APC. An APC generated by an application is called a user-mode APC. A thread must be in an alertable state to run a user-mode APC.
Windows中断请求级别
IRQL | Interrupts Masked Off | Driver Routines |
---|---|---|
PASSIVE_LEVEL | None | PASSIVE_LEVEL |
APC_LEVEL | APC_LEVEL interrupts | APC_LEVEL |
DISPATCH_LEVEL | DISPATCH_LEVEL ,APC_LEVEL interrupts | DISPATCH_LEVEL |
DIRQL | All interrupts at IRQL<= DIRQL | DIRQL |