进程上下文

进程的上下文包括:

用户空间:虚拟内存、栈、全局变量

内核空间:内核堆栈、寄存器状态

进程的状态

下面是进程的状态转移:

@startuml
[*] -> 创建
创建 --> 就绪
就绪 --> 执行
执行 -> 阻塞 : (调度)
执行 -> 就绪 : (调度)
阻塞 -> 就绪 : (调度)
执行 --> 终止 : (调度)
终止 -> [*]
@enduml

从运行到就绪:中断

从就绪到运行:被调度

时间片用完:转移到就绪

1)就绪 —— 执行:对就绪状态的进程,当进程调度程序按一种选定的策略从中选中一个就绪进程,为之分配了处理机后,该进程便由就绪状态变为执行状态;

2)执行 —— 阻塞:正在执行的进程因发生某等待事件而无法执行,则进程由执行状态变为阻塞状态(不是变成等待),如进程提出输入 / 输出请求而变成等待外部设备传输信息的状态,进程申请资源(主存空间或外部设备)得不到满足时变成等待资源状态,进程运行中出现了故障(程序出错或主存储器读写错等)变成等待干预状态等等;

当进程进入阻塞状态,是不占用 CPU 资源的。

3)阻塞 —— 就绪:处于阻塞状态的进程,在其等待的事件已经发生,如输入 / 输出完成,资源得到满足或错误处理完毕时,处于等待状态的进程并不马上转入执行状态,而是先转入就绪状态,然后再由系统进程调度程序在适当的时候将该进程转为执行状态;

4)执行 —— 就绪:正在执行的进程,因时间片用完而被暂停执行,或在采用抢先式优先级调度算法的系统中,当有更高优先级的进程要运行而被迫让出处理机时,该进程便由执行状态转变为就绪状态。

挂起进程在操作系统中可以定义为暂时被调离内存的进程

阻塞的两种情况

  1. 因为用户原因,程序所固有的。如等待输入。
  2. 因为系统原因,被迫的。如 CPU 被调配给另一个进程。

进程和线程的区别

In a system with the operating system supporting kernel-level threads, the process is the basic unit for resources allocation, while the thread is the basic unit for CPU scheduling.

PCB 包括哪些?

A process control block (PCB) contains information about the process, i.e. registers, quantum, priority, etc.

  • 为了调度需要保存进程状态(Process scheduling state) –The state of the process in terms of “ready”, “suspended”, etc., and other scheduling information as well, such as priority value, the amount of time elapsed since the process gained control of the CPU or since it was suspended. Also, in case of a suspended process, event identification data must be recorded for the event the process is waiting for.
  • 为了知道进程之间的关系,要保存进程结构信息(Process structuring information) – the process’s children id’s, or the id’s of other processes related to the current one in some functional way, which may be represented as a queue, a ring or other data structures
  • 不同的进程有各自的栈,因此还要保存栈指针(Stack pointer) – It is a stack pointer which is required to be saved when the process is switched from one state to another to retain the current position of the process.
  • 为了区分进程,需要保存进程号(Process number or PID) – Every process is assigned with a unique id known as process ID or PID which stores the process identifier.
  • 为了继续从半途继续,要保存程序计数器(Program counter)– It stores the counter which contains the address of the next instruction that is to be executed for the process.
  • 同样的,要保存寄存器状态(Register) – These are the CPU registers which includes: accumulator, base, registers and general purpose registers.
  • 内存管理信息(Memory Management Information) This field contains the information about memory management system used by operating system. This may include the page tables, segment tables etc.
  • 打开文件列表(Open files list) – This information includes the list of files opened for a process.
  • 跨进程通信信息(Interprocess communication information) – flags, signals and messages associated with the communication among independent processes
  • 程序的权限(Process Privileges) – allowed/disallowed access to system resources
  • CPU 调度信息(CPU Scheduling Information) – information scheduling CPU time
  • 统计信息(Accounting Information) – amount of CPU used for process execution, time limits, execution ID etc.
  • IO 设备状态(I/O Status Information) – list of I/O devices allocated to the process.

PCB 不包括用户数据(user data)