Multithreading – what is saved in context switching?

Precisely saves and restores content in context switching between two threads

>In the same process > between two processes

Solution

This is a rather complex question because the answer depends on many things:

>CPU in question

>Even in the same series, there may be significant differences, such as additional registers added for SSE / MMX operations

>Operating system, because it controls the handler triggered in context switching and determines whether to use CPU hardware (if any) to assist context switching

>For example, Windows does not use Intel hardware that can do a lot of context switching storage for you because it does not store floating point registers

>The program enables any optimization, is aware of its own requirements, and is able to notify the operating system

>Perhaps it indicates that it does not use FP registers, so don't bother them > in an architecture with a large number of register files with a large number of RISC designs, you can get considerable benefits by knowing that you only need a small subset of these registers

At least using general-purpose registers and program counter registers will need to be saved (assuming the general design of most existing CISC / RISC style general-purpose CPUs)

Note that the minimal effort to attempt only context switching is topic of some academic interest

Linux obviously has more information available in the public domain, although my references may be a little outdated

This is a task_ Struct, which contains a large number of fields related to the task state and the process used by the task

One of them is thread_ struct

Each architecture defines its own thread_ Struct, which identifies registers and other values stored on the switch

This is further complicated by the existence of rename registers that allow multiple flight instructions (through superscalar or pipeline related architecture design) The recovery phase of context swith may rely on the CPU pipeline to recover to the initial empty state, so the instructions that have not exited in the pipeline have no effect, so they can be ignored This makes the design of CPU more difficult

The difference between process and thread is that process switching (always means thread switching in all mainstream operating systems) will need to update memory conversion information, IO related information and permission related structures

These will mainly be pointers to richer data structures, so the cost associated with thread context switching will not be very high

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>