开发者

How is context switching of threads done on a multi-core processor?

When doing context switching on a single-core processor, 开发者_Go百科the code responsible is executed on the only CPU which takes care of switching the threads.

But how is this done when we have multiple CPUs? Is there a master CPU which does all the context switching of all slave CPUs? Is each CPU responsible for its own context switching? If so, how is the switching synchronized so that two CPUs are not executing the same thread? Or is there some other mechanism in place?


The kernel is multi-threaded. It can execute on any core. When a core comes to need to swap threads, it invokes the part of the kernel responsible for selecting the next thread it should execute.

The kernel is multi-threaded; which is to say, it is written to be safe executing concurrently on multiple cores. As such, only one CPU ends up running any given thread, because the code is constructed such that if multiple CPUs reschedule concurrently, the correct outcome occurs.


CPU's don't do context switching. Operating Systems do.

In essence, an OS executes a context switch by loading a new context (registers, memory mappings, etc) in a CPU core. Threads are an OS structure in which such contexts can be saved. Hence, the OS is also responsible for picking a nonrunning thread to load the CPU context from.

If the OS were to pick a running thread, two cores would try to run the same thread. That's bound to cause confusion as they'd share the same memory, and that single thread won't expect to be run in parallel with itself (!) So no OS would do such a thing.


Assume we have two process P1 and P2 The approximate Sequence of Steps should be like This.

The current registers are stored into the process structure for P1.
The stored register values from the process structure for P2 are loaded into the CPU's registers.
CPU returns the User mode
P1 is context switched out and P2 is context switched in and running

Note, CPU only Sends the intterupts to OS to perform the Context Switch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜