`
dawuafang
  • 浏览: 1103713 次
文章分类
社区版块
存档分类
最新评论

进程上下文和中断上下文的区别

 
阅读更多

[原文:http://blog.chinaunix.net/u3/93613/showart_1907687.html]

进程上下文和中断上下文是操作系统中很重要的两个概念,这两个概念在操作系统课程中不断被提及,是最经常接触、看上去很懂但又说不清楚到底怎么回事的两个概念。造成这种局面的原因,可能是原来接触到的操作系统课程的教学总停留在一种浅层次的理论层面上,没有深入去研究。

处理器总处于以下三种状态之一:
1、内核态,运行于进程上下文,内核代表进程运行于内核空间;
2、内核态,运行于中断上下文,内核代表硬件运行于内核空间;
3、用户态,运行于用户空间。

用户空间的应用程序,通过系统调用,进入内核空间。这个时候用户空间的进程要传递 很多变量、参数的值给内核,内核态运行的时候也要保存用户进程的一些寄存 器值、变量等。所谓的“进程上下文”,可以看作是用户进程传递给内核的这些参数以及内核要保存的那一整套的变量和寄存器值和当时的环境等。

硬件通过触发信号,导致内核调用中断处理程序,进入内核空间。这个过程中,硬件的 一些变量和参数也要传递给内核,内核通过这些参数进行中断处理。所谓的“ 中断上下文”,其实也可以看作就是硬件传递过来的这些参数和内核需要保存的一些其他环境(主要是当前被打断执行的进程环境)。

LINUX完全注释中的一段话:

当一个进程在执行时,CPU的所有寄存器中的值、进程的状态以及堆栈中的内容被称 为该进程的上下文。当内核需要切换到另一个进程时,它需要保存当前进程的 所有状态,即保存当前进程的上下文,以便在再次执行该进程时,能够必得到切换时的状态执行下去。在LINUX中,当前进程上下文均保存在进程的任务数据结 构中。在发生中断时,内核就在被中断进程的上下文中,在内核态下执行中断服务例程。但同时会保留所有需要用到的资源,以便中继服务结束时能恢复被中断进程 的执行。

Interrupt Context
-------------------------------------------
When executing an interrupt handler or bottom half, the kernel is in interrupt context.Recall that process context is the mode of operation the kernel is in while it is executing on behalf of a process -- for example, executing a system call or running a kernel thread. In process context, the current macro points to the associated task. Furthermore, because a process is coupled to the kernel in process context(因为进程是以进程上文的形式连接到内核中的),process context can sleep or otherwise invoke the scheduler.

Interrupt context, on the other hand, is not associated with a process. The current macro is not relevant (although it points to the interrupted process). Without a backing process(由于没有进程的背景),interrupt context cannot sleep-- how would it ever reschedule?(否则怎么再对它重新调度?)Therefore, you cannot call certain functions from interrupt context. If a function sleeps, you cannot use it from your interrupt handler -- this limits the functions that one can call from an interrupt handler.(这是对什么样的函数可以在中断处理程序中使用的限制)

Interrupt context is time critical because the interrupt handler interrupts other code. Code should be quick and simple. Busy looping is discouraged. This is a very important point;always keep in mind that your interrupt handler has interrupted other code(possibly even another interrupt handler on a different line!).Because of this asynchronous nature, it is imperative(必须) that all interrupt handlers be as quick and as simple as possible. As much as possible, work should be pushed out from the interrupt handler and performed in a bottom half, which runs at a more convenient time.

The setup of an interrupt handler's stacks is a configuration option. Historically, interrupt handlers did not receive(拥有) their own stacks. Instead, they would share the stack of the process that they interrupted[1]. The kernel stack is two pages in size; typically, that is 8KB on 32-bit architectures and 16KB on 64-bit architectures. Because in this setup interrupt handlers share the stack, they must be exceptionally frugal(必须非常节省) with what data they allocate there. Of course, the kernel stack is limited to begin with, so all kernel code should be cautious.

[1] A process is always running. When nothing else is schedulable, the idle task runs.

Early in the 2.6 kernel process, an option was added to reduce the stack size from two pages down to one, providing only a 4KB stack on 32-bit systems. This reduced memory pressure because every process on the system previously needed two pages of nonswappable kernel memory. To cope with(应对) the reduced stack size, interrupt handlers were given their own stack, one stack per processor, one page in size. This stack is referred to as the interrupt stack(这个栈就程为中断栈). Although the total size of the interrupt stack is half that of the original shared stack, the average stack space available is greater because interrupt handlers get the full page of memory to themselves.

Your interrupt handler should not care what stack setup is in use or what the size of the kernel stack is. Always use an absolute minimum amount of stack space.


Process Context
-------------------------------------------
One of the most important parts of a process is the executing program code. This code is read in from an executable file and executed within the program's address space. Normal program execution occurs in user-space.When a program executes a system call or triggers an exception, it enters kernel-space. At this point, the kernel is said to be "executing on behalf of the process" and is in process context.When in process context, the current macro is valid[7]. Upon exiting the kernel, the process resumes execution in user-space, unless a higher-priority process has become runnable in the interim(过渡期), in which case the scheduler is invoked to select the higher priority process.

[7] Other than process context there is interrupt context, In interrupt context, the system is not running on behalf of a process, but is executing an interrupt handler. There is no process tied to interrupt handlers and consequently no process context.

System calls and exception handlers are well-defined interfaces into the kernel. A process can begin executing in kernel-space only through one of these interfaces -- all access to the kernel is through these interfaces.

-------------------------------------------
上下文context: 上下文简单说来就是一个环境,相对于进程而言,就是进程执行时的环境。具体来说就是各个变量和数据,包括所有的寄存器变量、进程打开的文件、内存信息等。
一个进程的上下文可以分为三个部分:用户级上下文、寄存器上下文以及系统级上下文。
用户级上下文: 正文、数据、用户堆栈以及共享存储区;
寄存器上下文: 通用寄存器、程序寄存器(IP)、处理器状态寄存器(EFLAGS)、栈指针(ESP);
系统级上下文: 进程控制块task_struct、内存管理信息(mm_struct、vm_area_struct、pgd、pte)、内核栈。

当发生进程调度时,进行进程切换就是上下文切换(context switch).操作系统必须对上面提到的全部信息进行切换,新调度的进程才能运行。而系统调用进行的模式切换(mode switch)。模式切换与进程切换比较起来,容易很多,而且节省时间,因为模式切换最主要的任务只是切换进程寄存器上下文的切换。
分享到:
评论

相关推荐

    对Linux内核中进程上下文和中断上下文的理解

    ( 对Linux内核中进程上下文和中断上下文的理解

    Linux 内核进程上下文和中断上下文

    对于linux中进程上下文和中断上下文的理解

    IO空间 内存管理 平台设备 中断上下文与进程上下文

    IO空间 内存管理 平台设备 中断上下文与进程上下文

    quronghui#Embedded-written-reference#Linux进程上下文切换和中断上下文切换1

    Linux进程上下文切换和中断上下文切换Linux缺页中断和中断区别。

    CPU上下文切换到底是个什么东西

    进程上下文切换1.2.2. 线程上下文切换1.2.3. 中断上下文切换 第一节,我们了解到了平均负载是个什么东西,并且通过三个案例展示了不同场景下(cpu密集型场景,io密集型场景,大量进程场景)的平均负载升高的分析方法...

    04 | 基础篇:经常说的 CPU 上下文切换是什么意思?(下)

    vmstat:是查看系统的整体上下文切换情况,想看具体的...-w:进程上下文切换情况。 -u :进程cpu使用情况 5:是每五秒输出一组数据。 pidstat -wt 1 -wt :输出线程的上线文切换情况。 1:每1秒输出一组数据。 中断次数

    深入剖析Linux中断机制

    进程切换,中断注册释放 request_irq,free_irq,共享中断,可重入,中断上下文 1 中断概述 1.1 为什么需要中断? 处理器的速度跟外围硬件设备的速度往往不在一个数量级上,因此,如果内核采取让处理器 向硬件...

    操作系统 进程创建实验报告

    子进程继承了父进程的许多特性,并具有与父进程完全相同的用户级上下文。父进程与子进程并发执行。 核心为fork( )完成以下操作: (1)为新进程分配一进程表项和进程标识符 进入fork( )后,核心检查系统是否有足够...

    Unix操作系统设计

    6.4 进程上下文的保存 6.4.1 中断和例外 6.4.2 系统调用的接口 6.4.3 上下文切换 6.4.4 为废弃返回(abortive return)而保存上下文 6.4.5 在系统和用户地址空间之间拷贝数据 6.5 进程地址空间的管理 6.5.1 区的上锁...

    spinlock_test.c

    ② 如果是在进程上下文中持有自旋锁,也是不能睡眠的。因为自旋锁会关抢占,该进程一旦睡眠,该CPU上就无法再调度其他任务执行,只能响应中断 在实际使用中,在关抢占的情况下调用schedule,内核允许会报bug 2.3....

    进程信号的处理过程.docx

    进程信号的处理过程:(1)在...进程注销信号后,目的进程根据当前进程对此信号设置的处理方式,暂时终止当前代码的执行,保护上下文,转而执行信号处理函数,即捕获该信号,执行完成后再恢复到被中断的位置继续执行。

    linux操作系统内核技术-uestc课件

     5熟悉在x86体系结构上Linux中断和异常的处理原理,中断注册、共享、控制,和中断上下文的意义,中断和设备驱动程序的关系,以及设备驱动程序结构和用户接口。(4小时)  6中断处理程序被分解为top half和bottom ...

    linux线程切换和进程切换的方法

    1、线程上下文切换和进程上下问切换一个最主要的区别是线程的切换虚拟内存空间依然是相同的,但是进程切换是不同的。这两种上下文切换的处理都是通过操作系统内核来完成的。内核的这种切换过程伴随的最显著的性能...

    Linux 系统内核空间与用户空间通信的实现与分析

    Linux 系统内核空间与用户空间通信的实现与分析 Linux 内核模块的运行环境与传统进程间通信 在一台运行 Linux 的计算机中,CPU...【3】 运行于内核态,但有进程上下文,即与一个进程相关。 【4】 运行一个用户态进程。

    如何选择您的网络的最佳路由器交换路径

    中断上下文交换和流程转换的之间主要的区别是:中断当前运行在处理器的进程转换信息包。仅当可以安排时,信息包被转换根据要求,而不是被转换ip_input进程。处理器使用路由高速缓存的某种表找到所有信息必要转换信息...

    浅谈linux线程切换问题

    1、内核态,运行于进程上下文,内核代表进程运行于内核空间; 2、内核态,运行于中断上下文,内核代表硬件运行于内核空间; 3、用户态,运行于用户空间; 一个进程的上下文可以分为三个部分:用户级上下文、...

    Linux操作系统性能监测:CPU篇

    要想监测和理解 CPU 的性能需要知道一些的操作系统的基本知识,比如:中断、进程调度、进程上下文切换、可运行队列等。这里 VPSee 用个例子来简单介绍一下这些概念和他们的关系,CPU 很无辜,是个任劳任怨的打工仔,...

    嵌入式系统中如何编写中断处理程序ISR

    当进程发出一个系统调用的请求时,由应用态切换到...这样的内核控制路径被成为进程内核路径,也叫进程上下文。当CPU执行一个与中断有关的内核控制路径的时候,被成为中断上下文。中断的上半部和下半部都属于ISR上下文。

    UNIX操作系统设计(The Design of the UNIX Operating System)

    6.4 进程上下文的保存 6.4.1 中断和例外 6.4.2 系统调用的接口 6.4.3 上下文切换 6.4.4 为废弃返回(abortive return)而保存上下文 6.4.5 在系统和用户地址空间之间拷贝数据 6.5 进程地址空间的管理 6.5.1 ...

    操作系统教程孙钟秀主编 习题解答

    当系统中发生了一个事件,如I/O结束事件,CPU便可把运行进程的上下文保存到专用硬件寄存器指针指向的PCB中保护起来,然后,CPU转向中断向量表,找到设备中断处理程序入口,让专用硬件寄存器指针指向(设备)中断服务例程,...

Global site tag (gtag.js) - Google Analytics