上下文切换中保存了什么? [英] What is saved in a context switch?

查看:476
本文介绍了上下文切换中保存了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在两个线程之间的上下文切换中准确保存和恢复的内容

What is exactly saved and restored in a context switch between two threads

  • 在同一过程中
  • 两个过程之间

推荐答案

这是一个相当复杂的问题,因为答案取决于很多事情:

This is rather a complex question since the answer(s) are dependent on many things:

  1. 有问题的CPU
    • 即使在同一系列中,它也可能有很大不同,例如,为SSE ​​/MMX操作添加的其他寄存器.
  1. The CPU in question
    • It can vary significantly even within the same family for example the additional registers added for SSE/MMX operations.
  • 例如,Windows不使用英特尔硬件,因为它不存储浮点寄存器,因此可以为您完成大部分上下文切换存储.
  • 也许表明它没有使用FP寄存器,所以请不要理会它们
  • 在像大多数RISC设计这样的具有大量寄存器文件的体系结构中,知道您仅需要这些寄存器的较小子集,便会带来很大的好处

至少需要保存使用中的通用寄存器和程序计数器寄存器(假设大多数当前CISC/RISC样式的通用CPU的通用设计).

At a minimum the in use general purpose registers and program counter register will need to be saved (assuming the common design of most current CISC/RISC style general purpose CPUs).

请注意,尝试仅针对上下文切换进行最少的工作是

Note that attempting to do only the minimal amount of effort in relation to a context switch is a topic of some academic interest

尽管我的参考文献可能有些过时,但Linux在公共领域显然有更多可用信息.

Linux obviously has more info available on this in the public domain though my references may be a little out of date.

有一个"task_struct",其中包含大量与任务状态以及任务所针对的过程有关的字段.

There is a ‘task_struct’ which contains a large number of fields relating to the task state as well as the process that the task is for.

其中之一是"thread_struct"

One of these is the ‘thread_struct’

/*此任务的CPU特定状态*/
-struct thread_struct线程;
包含有关缓存TLS描述符,调试寄存器的信息,
故障信息,浮点,虚拟86模式或IO权限.

/* CPU-specific state of this task */
- struct thread_struct thread;
holds information about cache TLS descriptors, debugging registers,
fault info, floating point, virtual 86 mode or IO permissions.

每个体系结构都定义了自己的thread_struct,用于标识寄存器和保存在交换机上的其他值.

Each architecture defines it's own thread_struct which identifies the registers and other values saved on a switch.

由于重命名寄存器的存在而使情况更加复杂,重命名寄存器允许使用多个飞行指令(通过超标量或与流水线相关的架构设计).上下文切换的恢复阶段可能依赖于以最初为空的状态恢复CPU的管道,因此尚未在管道中退出的指令无效,因此可以忽略.这使CPU的设计变得困难得多.

This is further complicated by the presence of rename registers which allow multiple in flight instructions (either via superscalar or pipeline related architectural designs). The restore phase of a context swicth will likely rely on the CPU's pipeline being restored in a initially empty state such the the instructions which had not yet been retired in the pipeline have no effect and thus can be ignored. This makes the design of the CPU that much harder.

进程和线程之间的区别在于,进程开关(在所有主流操作系统中始终表示线程开关)将需要更新内存转换信息,与IO相关的信息和与权限相关的结构.

The difference between a process and a thread is that the process switch (which always means a thread switch in all main stream operating systems) will need to update memory translation information, IO related information and permission related structures.

这些将主要是指向更丰富的数据结构的指针,因此与线程上下文切换相比,这不会花费很多.

These will mainly be pointers to the more rich data structures so will not be a significant cost in relation to the thread context switch.

这篇关于上下文切换中保存了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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