等待IO的线程还会阻塞内核吗? [英] Does a thread waiting on IO also block a core?

查看:378
本文介绍了等待IO的线程还会阻塞内核吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在同步/阻塞计算模型中,我们通常说执行线程在等待IO任务完成时将等待(被阻塞).

我的问题是,这是否通常会导致执行线程的CPU内核处于空闲状态,或者通常会在上下文中切换等待IO的线程,并使其处于等待状态,直到准备好处理IO?

解决方案

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.

这篇关于等待IO的线程还会阻塞内核吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆