免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 869 | 回复: 0
打印 上一主题 下一主题

Processes(十二) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-04-24 23:23 |只看该作者 |倒序浏览
Process Switch
To control the execution of processes, the kernel must be able to suspend the execution of the process running on the CPU and resume the execution of some other process previously suspended. This activity goes variously by the names process switch, task switch, or context switch. The next sections describe the elements of process switching in Linux.
为了能够控制进程的执行,内核必须能够暂停正在CPU上运行的进程,唤醒另一个之前暂停的进程的执行。这一行为称为进程切换,任务切换,或者上下文切换。

Hardware Context
While each process can have its own address space, all processes have to share the CPU registers. So before resuming the execution of a process, the kernel must ensure that each such register is loaded with the value it had when the process was suspended.
尽管每个进程有自己的地址空间,所有的进程都不得不共享CPU的寄存器。所以,在恢复某个进程执行之前,内核必须保证每个这样的寄存器都装载进程暂停之前的那些值。
The set of data that must be loaded into the registers before the process resumes its execution on the CPU is called the hardware context . The hardware context is a subset of the process execution context, which includes all information needed for the process execution. In Linux, a part of the hardware context of a process is stored in the process descriptor, while the remaining part is saved in the Kernel Mode stack.
这些数据,必须在进程唤醒之前被装载到寄存器,称为硬件上下文。硬件上下文是进程执行上下文的子集,进程执行上下文包括了进程执行的所有信息。Linux中,进程的一部分硬件上下文存储在进程描述符中,剩下的存储在内核栈中。
In the description that follows, we will assume the prev local variable refers to the process descriptor of the process being switched out and next refers to the one being switched in to replace it. We can thus define a process switch as the activity consisting of saving the hardware context of prev and replacing it with the hardware context of next. Because process switches occur quite often, it is important to minimize the time spent in saving and loading hardware contexts.
下面的描述中,我们假设局部变量prev指向被换出的进程的描述符,next指向换入取代前者的进程的描述符。因此我们可以定义进程切换为包括了保存prev的硬件上下文,用next的硬件上下文取代的行为。因为进程切换很频繁,所以减小花在保存和装载硬件上下文上的时间就变得很重要了。
Old versions of Linux took advantage of the hardware support offered by the 80x86 architecture and performed a process switch through a far jmp instruction
  • to the selector of the Task State Segment Descriptor of the next process. While executing the instruction, the CPU performs a hardware context switch by automatically saving the old hardware context and loading a new one. But Linux 2.6 uses software to perform a process switch for the following reasons:
  • far jmp instructions modify both the cs and eip registers, while simple jmp instructions modify only eip.
    老版本的Linux利用了x86架构提供的硬件支持来实现进程切换,通过一个far jmp指令跳到next进程的TSS的选择子。在执行这个指令时,CPU自动地保存老的硬件上下文,并装载新的,从而完成硬件上下文切换。但Linux2.6使用软件来进行进程切换,是因为:
    · Step-by-step switching performed through a sequence of mov instructions allows better control over the validity of the data being loaded. In particular, it is possible to check the values of the ds and es segmentation registers, which might have been forged by a malicious user. This type of checking is not possible when using a single far jmp instruction.
    通过一系列mov指令一步一步地切换允许更好地控制对将要装载数据的合法性检查。实际上,检查ds和es段寄存器的内容很重要,因为它们可能被恶意的用户修改。在使用简单的far jmp指令时不可能使用这种检查。
    · The amount of time required by the old approach and the new approach is about the same. However, it is not possible to optimize a hardware context switch, while there might be room for improving the current switching code.
    旧的方法要求的时间和新的相差不多。然而,不可能优化硬件上下文切换,而改进当前的软件切换代码还有空间。
    Process switching occurs only in Kernel Mode. The contents of all registers used by a process in User Mode have already been saved on the Kernel Mode stack before performing process switching (see Chapter 4). This includes the contents of the ss and esp pair that specifies the User Mode stack pointer address.
    进程切换仅发生在内核空间。一个进程在用户空间使用的所有寄存器的内容,在执行进程切换之前已经存储到了内核空间栈内。其中包括了ss和esp对的内容,它们指明了用户空间栈指针地址。


    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/65228/showart_576678.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP