线程上下文切换和进程上下文切换的区别

Difference between context switch of thread and context switch of process

我知道 here ,对此有一个解释 question.But 我对某些点有点困惑-:

让我拥有属于进程 P1 的线程 T(1-a) 和 T(1-b) 以及属于进程 P2 的线程 T(2-a) 和 T(2-b)。

现在我的问题是-:

  1. 线程 T(1-a) 想要切换到线程 T(1-b)。根据 this 的回答,

    Both types(process Context switch and thread context switch) involve handing control over to the operating system kernel to perform the context switch(i am mainly talking about thread context switch).

疑问

if T(1-a) and T(1-b) are user level threads,Kernel won't be able to distinguish T(1-a) and T(1-b),then how context switch will be done?

  1. 让所有线程 T(1-a)、T(1-b)、T(2-a) 和 T(2-b) 都是内核级线程,并且如果线程 T(1-a) 想要上下文切换到 T(2-b).

疑问

won't the cost/dealy will be same as that of process context switch as not only virtual memory space gets changed but also TLB gets flushed??

考虑这一点的方式是,用户线程和内核线程是两种完全不同的动物。

在该术语的正常和习惯含义下,用户线程中没有上下文切换。

Both types(process Context switch and thread context switch) involve handing control over to the operating system kernel to perform the context switch(i am mainly talking about thread context switch).

我看到了你的困惑。首先,这里所描述的仅适用于内核线程。其次,(这个问题显然是措辞的结果而不是整体消息),在内核线程中,没有 "process context switches," 只有 "Thread Context Switches".

在没有线程的旧时代,调度的变化意味着进程上下文的变化。执行此操作的硬件指令具有加载进程上下文和保存进程上下文等名称。但是在调度线程(内核线程)的操作系统中,我们留下了这个不再非常精确的旧术语。 (这是在讲解操作系统的时候很多地方都会出现的问题)

if T(1-a) and T(1-b) are user level threads,Kernel won't be able to distinguish T(1-a) and T(1-b),then how context switch will be done?

如果它们是用户级线程,内核不知道也不关心它们。它们之间的切换由用户库处理。 "threads" 的切换不是上下文切换。

won't the cost/dealy will be same as that of process context switch as not only virtual memory space gets changed but also TLB gets flushed??

希望操作系统足够聪明,知道在同一进程中的线程之间切换时不要刷新内存缓存。

我相信学术界需要放弃 kernel/user 线程(更糟糕的是,1 对 1、多对 1 和多对多 [yuk])构造。在他们的位置,我建议使用术语

"real threads" 和 "simulated threads".