process/thread 运行 可以在中断被禁用的情况下吗?

Can a process/thread run while interrupts are disabled?

我有以下 "pretend" 信号量 wait() 操作的实现。假设单核、单处理器环境:

wait () { 
 Disable interrupts
 sem->value--
 if (sem->value < 0) {
 save_state (current) ; //"Manually" save the context of the current running process
 State[current] = Blocked; //Block it
 Queue current to block queue;
 current = Select from the ready queue; //Select another process to run
 State[current] = Running; //Put the retrieved process in the running state
 restore_state (current); //"Manually" restore the context of the new process
 }
 Enable interrupts
}

该实现是为了测试我们关于禁用中断以保护临界区的知识。其中一个问题是判断新进程是在wait()运行秒内在中断关闭时还是在中断开启后从就绪队列中选出的。

我在两个方面都在努力寻找答案。

如有任何提示,我们将不胜感激。

如果 process/thread 能够 运行 禁用中断,那么 process/thread 能够防止操作系统中断它,因此能够占用所有 CPU 时间,因此可以成为不可阻挡的恶意拒绝服务攻击。

对于某些 CPUs 在某些条件下(例如 80x86,IOPL 设置为 3),OS 可能允许 process/thread 禁用 IRQ,并且是可能的让 process/thread 运行 禁用 IRQ 但不能 enable/disable IRQ(例如,在返回到 user-space 之前在内核中禁用 IRQ);但因为它们是安全灾难,所以很少有操作系统允许。

但是;信号量还涉及与调度程序的交互(阻塞任务直到它可以获取信号量,并在任务可以获取信号量时解除阻塞),以及调度程序(它的 "ready to run" 队列,processs/thread 状态等) 和访问完整 process/thread 状态的能力(例如特殊 "kernel only" 寄存器,比如控制当前选择哪个虚拟地址 space 的寄存器)通常也只能从内核代码访问(并且不允许 process/thread 从用户-space 访问)。

换句话说;假设 wait() 函数中超过 50% 的代码不能在 user-space 中实现并且必须在内核中实现是合理的(忽略奇怪和不太可能的情况);因此 可以合理地假设您的 wait() 函数旨在在内核中实现(而不是通过进程或线程在用户 space 中实现).