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

处理器总处于以下状态中的一种:

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)。模式切换与进程切换比较起来,容易很多,而且节省时间,因为模式切换最主要的任务只是切换进程寄存器上下文的切换。

转自:http://blog.chinaunix.net/uid-26126915-id-3018791.html

linux 中断和进程 传递,Linux内核之进程上下文和中断上下文的区别相关推荐

  1. 表正在被别的用户或进程使用_linux内核对进程的管理分为两个方面

    嵌入式开发直播课 - linux内核通知链 - 创客学院直播室​www.makeru.com.cn 众所周知,现在的分时操作系统能够在一个CPU上运行多个程序,让这些程序表面上看起来是在同时运行的.l ...

  2. php-frm进程管理,PHP内核探索-进程管理

    进程管理方式 首先我们了解一下php的三种不同的进程管理方式: static:静态管理进程.在启动时,master按照pm.max_children配置fork出对应数量的work进程,即work的进 ...

  3. Linux:上下文,进程上下文和中断上下文概念,上下文切换

    Linux:上下文,进程上下文和中断上下文概念,上下文切换 1. 上下文 context:(就是一个环境) 2. 进程上下文 2.1 进程上下文的三个部分:用户级上下文.寄存器上下文以及系统级上下文 ...

  4. linux的线程要makefile,Linux内核线程之父pid=2的kthreadd线程

    这里所谓的内核线程,实际上是由kernel_thread函数创建的一个进程,有自己独立的task_struct结构并可被调度器调度,这种进程的特殊之处在于它只在内核态运行. 在Linux source ...

  5. Linux进程间关系之守护进程

    概念 守护进程也称精灵进程,是运行在后台的一种特殊进程.守护进程独立于控制终端并且周期性的执行某种任务或者等待处理某些打算的事件.可认为守护进程目的就是防止终端产生的一些信号让进程退出 特点 所有的守 ...

  6. 进程上下文和中断上下文

    1.进程上下文 进程上下文实际上是进程执行活动全过程的静态描述.我们把已执行过的进程指令和数据在相关寄存器与堆栈中的内容称为上文,把正在执行的指令和数据在寄存器和堆栈中的内容称为正文,把待执行的指令和 ...

  7. linux判断cpu是否过载,Linux CPU 如何判断忙

    原标题:Linux CPU 如何判断忙 摘录自:http://www.ruanyifeng.com/blog/2016/12/user_space_vs_kernel_space.html 学习 Li ...

  8. Electron 入门,主进程向渲染进程发送事件,渲染进程向主进程发送事件

    Electron 入门,主进程向渲染进程发送事件,渲染进程向主进程发送事件 相关教程: Electron教程(二)启动过程:主进程,渲染进程是什么 刚入门 electon ,整 electon + v ...

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

    内核空间和用户空间是操作系统理论的基础之一,即内核功能模块运行在内核空间,而应用程序运行在用户空间.现代的CPU都具有不同的操作模式,代表不同的 级别,不同的级别具有不同的功能,在较低的级别中将禁止某 ...

最新文章

  1. struts.xml向页面传参
  2. 妙用 Intellij IDEA 创建临时文件,Git 跟踪不到的那种
  3. Sentinel: 分布式系统的流量防卫兵 1
  4. 命令2-Create Project Tree
  5. 双针模型:验证括号,特殊case处理
  6. 再次强调事件绑定中this的坑
  7. 汽车电子专业知识篇(九)-charge pump的原理介绍
  8. WinCE驱动开发问题精华集锦
  9. c语言程序输入n个数字排序,请问,C语言能人请进,用写一个程序,要求输入N个整数,按从小到大的顺序输出,就说说...
  10. ListUtil常用操作
  11. getElementById和querySelector方法的区别
  12. 计算机网络与通信之物理层中的数据传输
  13. SSM流程及核心原理
  14. 。快充的原理有三种:电压不变、提升电流,电流不变,提升电压,电流电压两者都提高。要想达到这三种方式的其中一种,我们都需要对充电头和充电线进行掌控。
  15. Mac电脑程序无响应怎么办?
  16. 韦小宝高超的说谎技巧
  17. html自定义指针,如何自定义鼠标指针 怎样在wpf中自定义鼠标指针
  18. GitGitHub 笔记
  19. ad电阻原理图_arduino传感器专辑之光敏电阻模块
  20. 手机处理器排行榜2019_2019十大手机读书软件排行榜

热门文章

  1. linux建立ftp suse_suse开通ftp的实例
  2. Python论做游戏外挂,Python输过谁?
  3. mac电脑本地运行MapReduce, Permission denied
  4. 微软.NET各技术应用前景 针对vs.net2010
  5. 石油、黄金与美元的游戏
  6. 换手率与股价成交量 关系
  7. 漫步最优化六——数学规划
  8. Python中FileIO
  9. Spring boot admin 升级到2.3.1 遇到的问题总结
  10. embedding_Keras嵌入层