multithreading 线程上下文切换 Vs。进程上下文切换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5440128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread context switch Vs. process context switch
提问by Leon
Could any one tell me what is exactly done in both situations? What is the main cost each of them?
谁能告诉我在这两种情况下究竟做了什么?他们每个人的主要成本是多少?
回答by Abhay Buch
The main distinction between a thread switch and a process switch is that during a thread switch, the virtual memory space remains the same, while it does not during a process switch. Both types involve handing control over to the operating system kernel to perform the context switch. The process of switching in and out of the OS kernel along with the cost of switching out the registers is the largest fixed cost of performing a context switch.
线程切换和进程切换的主要区别在于,在线程切换期间,虚拟内存空间保持不变,而在进程切换期间则不然。这两种类型都涉及将控制权移交给操作系统内核以执行上下文切换。切入和切出 OS 内核的过程以及切出寄存器的成本是执行上下文切换的最大固定成本。
A more fuzzy cost is that a context switch messes with the processors cacheing mechanisms. Basically, when you context switch, all of the memory addresses that the processor "remembers" in its cache effectively become useless. The one big distinction here is that when you change virtual memory spaces, the processor's Translation Lookaside Buffer (TLB) or equivalent gets flushed making memory accesses much more expensive for a while. This does not happen during a thread switch.
一个更模糊的代价是上下文切换会干扰处理器缓存机制。基本上,当您进行上下文切换时,处理器在其缓存中“记住”的所有内存地址实际上都变得无用了。这里的一个很大区别是,当您更改虚拟内存空间时,处理器的转换后备缓冲区 (TLB) 或等效项会被刷新,从而使内存访问在一段时间内变得更加昂贵。这不会在线程切换期间发生。
回答by aditya dogra
Process context switching involves switching the memory address space. This includes memory addresses, mappings, page tables, and kernel resources—a relatively expensive operation. On some architectures, it even means flushing various processor caches that aren't sharable across address spaces. For example, x86 has to flush the TLB and some ARM processors have to flush the entirety of the L1 cache!
进程上下文切换涉及到内存地址空间的切换。这包括内存地址、映射、页表和内核资源——一个相对昂贵的操作。在某些体系结构上,它甚至意味着刷新不能跨地址空间共享的各种处理器缓存。例如,x86 必须刷新 TLB,某些 ARM 处理器必须刷新整个 L1 缓存!
Thread switching is context switching from one thread to another in the same process (switching from thread to thread across processes is just process switching).Switching processor state (such as the program counter and register contents) is generally very efficient.
线程切换是在同一个进程中从一个线程到另一个线程的上下文切换(跨进程从一个线程切换到另一个线程就是进程切换)。切换处理器状态(例如程序计数器和寄存器内容)通常是非常有效的。
回答by ZarathustrA
First of all, operating system brings outgoing thread in a kernel mode if it is not already there, because thread switch can be performed only between threads, that runs in kernel mode. Then the scheduler is invoked to make a decision about thread to which will be performed switching. After decision is made, kernel saves part of the thread context that is located in CPU (CPU registers) into the dedicated place in memory (frequently on the top of the kernel stack of outgoing thread). Then the kernel performs switch from kernel stack of outgoing thread on to kernel stack of the incoming thread. After that, kernel loads previously stored context of incoming thread from memory into CPU registers. And finally returns control back into user mode, but in user mode of the new thread. In the case when OS has determined that incoming thread runs in anotherprocess, kernel performs one additional step: sets new active virtual address space.
首先,操作系统将传出线程带入内核模式(如果它还没有),因为线程切换只能在运行在内核模式下的线程之间执行。然后调用调度程序来决定将要执行切换的线程。做出决定后,内核将位于 CPU 中的部分线程上下文(CPU 寄存器)保存到内存中的专用位置(通常位于传出线程的内核堆栈顶部)。然后内核执行从传出线程的内核堆栈到传入线程的内核堆栈的切换。之后,内核将先前存储的传入线程的上下文从内存加载到 CPU 寄存器中。最后将控制权返回到用户模式,但在新线程的用户模式下。在操作系统确定传入线程运行的情况下另一个进程,内核执行一个额外的步骤:设置新的活动虚拟地址空间。
The main cost in both scenarios is related to a cache pollution. In most cases, the working set used by the outgoing thread will differ significantly from working set which is used by the incoming thread. As a result, the incoming thread will start its life with avalanche of cache misses, thus flushing old and useless data from the caches and loading the new data from memory. The same is true for TLB (Translation Look Aside Buffer, which is on the CPU). In the case of reset of virtual address space (threads run in different processes) the penalty is even worse, because reset of virtual address space leads to the flushing of the entire TLB, evenif new thread actually needs to load only few new entries. As a result, the new thread will start its time quantum with lots TLB misses and frequent page walking. Direct cost of threads switch is also not negligible (from ~250 and up to ~1500-2000 cycles) and depends on the CPU complexity, states of both threads and sets of registers which they actually use.
两种情况下的主要成本都与缓存污染有关。在大多数情况下,传出线程使用的工作集将与传入线程使用的工作集显着不同。结果,传入线程将在大量缓存未命中的情况下开始其生命周期,从而从缓存中刷新旧的和无用的数据并从内存中加载新数据。TLB(Translation Look Aside Buffer,在 CPU 上)也是如此。在重置虚拟地址空间(线程在不同进程中运行)的情况下,惩罚更严重,因为虚拟地址空间重置导致整个 TLB 的刷新,即使如果新线程实际上只需要加载几个新条目。结果,新线程将开始它的时间段,并带有大量 TLB 未命中和频繁的页面遍历。线程切换的直接成本也不容忽视(从 ~250 到 ~1500-2000 个周期)并且取决于 CPU 复杂性、线程的状态和它们实际使用的寄存器组。
P.S.: Good post about context switch overhead: http://blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html
PS:关于上下文切换开销的好帖子:http: //blog.tsunanet.net/2010/11/how-long-does-it-take-to-make-context.html
回答by john
- process switching:it is a transition between two memory resident of process in a multiprogramming environment;
- context switching:it is a changing context from an executing program to an interrupt service routine (ISR).
- 进程切换:是多道程序环境中进程的两个驻留内存之间的转换;
- 上下文切换:它是从一个正在执行的程序到一个中断服务程序(ISR)的一个不断变化的上下文。
回答by Palak Jain
In Thread Context Switching, the virtual memory space remains the same while it is not in the case of Process Context Switch. Also, Process Context Switch is costlier than Thread Context Switch.
在线程上下文切换中,虚拟内存空间保持不变,而在进程上下文切换的情况下则不同。此外,进程上下文切换比线程上下文切换成本更高。
回答by Dra?en G.
I think main difference is when calling switch_mm()
which handles memory descriptors of old and new task. In the case of threads, the virtual memory address space is unchanged (threads share virtual memory), so very little has to be done, and therefore less costly.
我认为主要区别在于调用switch_mm()
which 处理旧任务和新任务的内存描述符时。在线程的情况下,虚拟内存地址空间不变(线程共享虚拟内存),因此需要做的工作很少,因此成本较低。
回答by Herr Günther
Though thread context switching needs to change the execution context (registers, stack pointers, program counters), they don't need to change address space as processes context switches do. There's an additional cost when you switch address space, more memory access (paging, segmentation, etc) and you have to flush TLB when entering or exiting a new process...
尽管线程上下文切换需要更改执行上下文(寄存器、堆栈指针、程序计数器),但它们不需要像进程上下文切换那样更改地址空间。当您切换地址空间、更多内存访问(分页、分段等)并且您必须在进入或退出新进程时刷新 TLB 时,会产生额外成本...
回答by CtrlDot
In short, the thread context switch does not assign a brand new set of memory and pid, it uses the same as the parent since it is running within the same process. A process one spawns a new process and thus assigns new mem and pid.
简而言之,线程上下文切换并没有分配一套全新的内存和pid,它使用与父进程相同的方法,因为它运行在同一个进程中。一个进程产生一个新进程,从而分配新的 mem 和 pid。
There is a loooooot more to it. They have written books on it.
还有一个更重要的事情。他们已经写了关于它的书。
As for cost, a process context switch >>>> thread as you have to reset all of the stack counters etc.
至于成本,进程上下文切换 >>>> 线程,因为您必须重置所有堆栈计数器等。
回答by delete me
Assuming that The CPU the OS runs has got Some High Latency Devices Attached,
假设操作系统运行的 CPU 连接了一些高延迟设备,
It makes sense to run another thread Of the Process's Address Space, while the high latency device responds back.
运行进程地址空间的另一个线程是有意义的,同时高延迟设备响应。
But, if the High Latency Device is responding faster than the time to need do set up of table + translation of Virtual To Physical memories for a NEW Process, then it is questionable if a switch is essential at all.
但是,如果高延迟设备的响应速度快于需要为新进程设置表 + 将虚拟内存转换为物理内存的时间,那么交换机是否必不可少就值得怀疑。
Also, HOT cache(data needed for running the process/thread is reachable in less time) is better choice.
此外,HOT 缓存(运行进程/线程所需的数据可在更短的时间内访问)是更好的选择。