且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

等待IO的线程还会阻塞内核吗?

更新时间:2022-10-30 19:11:40

CPU内核通常不专用于一个特定的执行线程.内核不断切换正在CPU中执行的进程. CPU当前正在执行的进程处于正在运行"状态.等待轮换的进程列表处于就绪"状态.内核可以非常快速地切入和切出它们.现代CPU功能(多核,同步多线程等)试图增加可以一次物理执行的执行线程数.

如果某个进程被I/O阻止,则内核只会将其放在一边(将其置于等待"状态),甚至不考虑给它留在CPU中的时间. I/O完成后,内核将阻止的进程从正在等待"状态移到就绪"状态,以便可以在CPU中轮流(运行").

因此,您被阻止的执行线程仅阻止:执行线程. CPU和CPU内核继续将其他执行线程切入和切出它们,并且它们并不处于空闲状态.

In the synchronous/blocking model of computation we usually say that a thread of execution will wait (be blocked) while it waits for an IO task to complete.

My question is simply will this usually cause the CPU core executing the thread to be idle, or will a thread waiting on IO usually be context switched out and put into a waiting state until the IO is ready to be processed?

A CPU core is normally not dedicated to one particular thread of execution. The kernel is constantly switching processes being executed in and out of the CPU. The process currently being executed by the CPU is in the "running" state. The list of processes waiting for their turn are in a "ready" state. The kernel switches these in and out very quickly. Modern CPU features (multiple cores, simultaneous multithreading, etc.) try to increase the number of threads of execution that can be physically executed at once.

If a process is I/O blocked, the kernel will just set it aside (put it in the "waiting" state) and not even consider giving it time in the CPU. When the I/O has finished, the kernel moves the blocked process from the "waiting" state to the "ready" state so it can have its turn ("running") in the CPU.

So your blocked thread of execution blocks only that: the thread of execution. The CPU and the CPU cores continue to have other threads of execution switched in and out of them, and are not idle.