Chapter 3 Process Description and Control 进程描述与控制

3.1 What is a process?

  • Fundamental Task: ProcessManagement
  • The Operating System must

    –Interleave the execution of multiple processes实现多进程交替执行

    –Allocate resources to processes, and protect the resources of each process from other processes分配资源,保护进程资源

    –Enable processes to share and exchange information进程间可以共享、交换信息

    –Enable synchronization among processes.进程间可以同步

1 Background

  • Summary of Earlier Concepts:

    –Computer platforms consists of a collection of hardware resources计算机由资源的集合构成

    –Computer applications are developed to perform some task开发应用程序来完成某个任务

    –It‘s inefficient for apps to be written directly for a given hardware platform应用程序独立于硬件

    –OS was developed to provide a convenient,feature-rich, secure, and consistent interface for apps to use OS是应用程序使用资源的接口

    –OS provides a uniform, abstract representation of resources that can be requested and accessed by apps OS提供了应用程序所请求、

​ 访问资源的统一抽象的表示

The OS Manages Execution of Applications

  • Resources are made available to multiple applications使资源可被多个应用程序使用
  • The processor is switched among multiple application处理器在多个应用程序间切换
  • The processor and I/O devices can be used efficiently处理器、I/O设备可被有效利用

Concept Program enough?

2. Process and Process Control Block 进程和进程控制块

  • 进程的定义

    –A program in execution执行中的程序

    –An instance of a program running on a computer在计算机上运行的一个程序实例

    –The entity that can be assigned to and executedona processor能获得处理机并在其上执行的实体

    –A unit of activity characterized by the execution of a sequence of instructions, a current state,and an associated set of system resources具有如下特征的活动单元:一组指令序列的执行、一个当前状态和相关的系统资源

  • 引入:对正在运行的程序过程的抽象
  • 定义:可并发执行的程序在某个数据集合的一次计算活动,是操作系统进行资源分配和保护的基本单位 。
  • 引入多进程,提高了对硬件资源的利用率,但又带来额外的空间和时间开销,增加了OS 的复杂性(为什么?)

Process Elements 进程元素

  • A process is comprised of两个必要元素:

    –Program code程序代码 (possiblyshared)

    –A set of data数据集合

    –此外还有:A number of attributes describing the state of the process描述进程状态的一些属性

    ( 执行中的实体即为进程 )。

Attributes Describing the State of the Process描述进程状态的属性

  • AS the process is running, it has a number of elements that can uniquely characterize it 进程运行时能唯一刻画它的元素

    –Identifier标识符

    –State状态

    –Priority优先级

    –Program counter程序计数器

    –Memory pointers内存指针

    –Context data上下文数据

    –I/O status information I/O状态信息

    –Accounting information记帐信息

Process Control Block 进程控制块(PCB)

  • A data structure用于记录和刻划进程状态及有关信息的数据结构
  • It is possible to interrupt a running process and later resume execution as if the interruption had not occurred使运行中的进程中断执行、再恢复执行成为可能
  • Created and manage by the OS由操作系统创建、管理
  • Key tool that allows support for multiple processes支持多进程的关键
PCB
PCB
  • PCB是每个进程在OS中的登记表项,OS据此对进程进行控制和管理,是系统感知进程存在的唯一标志
  • 通常不能由应用程序自身的代码来直接访问,而要通过系统调用
  • 进程与PCB是一一对应的

进程的性质

  • 结构性:代码、数据、栈、PCB
  • 动态性:执行过程、有生命周期
  • 独立性:资源分配、保护的独立单位
  • 并发性:可并发执行、引入的原因
  • 异步性:每个进程都以其相对独立的、不可预知的速度向前推进

进程与程序的区别

  • 进程是动态的,程序是静态的:程序是有序代码的集合;进程是程序的执行。通常进程不可在计算机之间迁移;而程序通常对应着文件、静态和可以复制。
  • 进程是暂时的,程序的永久的
  • 进程与程序的组成不同
  • 进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;通过调用关系,一个进程可包括多个程序。

3.2 Process States进程状态

  • The behavior of an individual process is shown by listing the sequence of instructions that are executed.This list is called a Trace单个进程的行为可通过列出执行的指令序列来展示。列表称为进程的轨迹
  • Dispatcher is a small program which switches the processor from one process to another将处理器从一个进程切到另一个进程的程序称为分派器(调度程序)

Process Execution Trace

  • Consider three processes being executed三个进程正在被执行
  • All are in memory (plus the dispatcher)全在内存

Trace from the processes point of view:

  • Each process runs to completion每个进程运行直到结束

Trace from Processors point of view

1 Two-State Process Model

  • Process may be in one of two states

    –Running运行状态:占用处理机资源;

    –Not-running未运行状态:等待进程调度分配处理机资源;

State Transitions状态转换
  • 进程创建(Enter):系统创建进程,形成PCB,分配所需资源,排入等待进程队列(可为一个队列);
  • 调度运行(Dispatch):从等待进程队列中选择一个进程(要求已完成I/O操作),进入运行状态;
  • 暂停运行(Pause):用完时间片或启动I/O操作后,放弃处理机,进入等待进程队列
  • 进程结束(Exit):进程运行中止
Queuing Diagram队列图

Processes moved by the dispatcher of the OS to the CPU then back to the queue until the task is competed 进程由OS的分派程序分派到CPU,然后回到队列,反复直到任务完成

2 Process Creation and Termination进程的创建和终止

Process Creation进程创建
  • The OS builds a data structure to manage the process OS构建数据结构来管理进程
  • Traditionally,the OS created all processes

    –But it can be useful to let a running process create another运行进程可创建另一个进程

    –This action is called process spawning派生

  • Parent Process is the original, creating, process
  • Child Process is the new process
Process Termination进程终止
  • There must be some way that a process can indicate completion.进程可以标示它的终止
  • This indication may be:

    –A HALT instruction generating an interrupt alert to the OS.终止指令,产生中断给OS

    –A user action (e.g. log off, quitting an application)用户活动

    –A fault or error出错

    –Parent process terminating父进程终止

3 Three-State Process Model

Example

4 Five-State Process Model

States
  • 新建状态(New):进程刚创建但还不能运行
  • 就绪态(Ready):一个进程已经具备运行条件,但由于无CPU暂时不能运行的状态
  • 运行态(Running):进程占有CPU并在其上运行
  • 阻塞/等待态(Blocked):指进程因等待某种事件的发生而暂时不能运行的状态
  • 退出状态(Exit):进程已结束运行,回收除PCB之外的其他资源,并让其他进程从PCB中收集有关信息
State Transitions状态转换
  • →新建(Create) :创建一个新进程,以运行一个程序
  • 新建→就绪(Admit, 收容或提交):收容一个新进程,进入就绪状态。
  • 就绪→运行(Dispatch):调度程序选择一个新的进程运行
  • 运行→就绪 (Time out/etc):运行进程用完了时间片,运行进程被中断,或因为一高优先级进程处于就绪状态
  • 运行→阻塞(Event Wait):当一进程必须等待时
  • 阻塞→就绪(Event Occurs):当所等待的事件发生时
  • 运行→退出(Release):由于进程完成或失败而中止进程运行,进入退出状态;
  • 就绪→退出:父进程 (何种情况?)
  • 阻塞→退出 (何种情况?)
Using Two Queues两队列
Multiple Blocked Queues

5 Suspended Processes进程挂起

  • Processor is faster than I/O, so all processes could be waiting for I/O 可能所有进程等待I/O

    –Swap these processes to disk to free up more memory and use processor on more processes换出这些进程到磁盘以释放内存来运行更多进程

  • Blocked state becomes suspendstate when swapped to disk如果换出则成为挂起状态
  • Two new states

    –Blocked/Suspend阻塞/挂起

    –Ready/Suspend就绪/挂起

One Suspend State单挂起
Two Suspend States
Added state transitions
  • 挂起(Suspend):把一个进程从内存转到外存;

    –阻塞→阻塞/挂起:没有进程处于就绪状态或就绪进程要求更多内存资源时

    –就绪→就绪/挂起:空间需要;高优先级阻塞进程即将就绪,挂起低优先级就绪进程

    –运行→就绪/挂起:对可抢占式系统,当有高优先级阻塞/挂起进程因事件出现而进入就绪/挂起时,抢占CPU,运行进程让出内存、CPU

  • 激活(Activate):把一个进程从外存转到内存;可能有以下几种情况:

    –就绪/挂起→就绪:当没有就绪进程或就绪/挂起进程优先级高于就绪进程

    –阻塞/挂起→阻塞:当一个进程释放足够内存,系统会把高优先级(高于所有就绪/挂起进程,事件很快发生)阻塞/挂起进程激活

  • 事件出现(Event Occurs)

    –阻塞→就绪

    –阻塞/挂起→就绪/挂起

  • 收容(Admit)

    –新建→就绪

    –新建→就绪/挂起

  • 释放(Release)

    –任意状态→退出

Reason for Process Suspension挂起原因
  • 进程均处于等待状态,需要把一些阻塞进程对换出去,腾出足够内存装入就绪进程运行。
  • 进程竞争资源,导致系统资源不足,负荷过重,需要挂起部分进程以调整系统负荷,保证实时性或让系统正常运行。
  • 定期执行的进程(如审计、监控、记账程序)对换出去,以减轻系统负荷。
  • 用户要求挂起自己的进程,以便进行某些调试、检查和改正。
  • 父进程要求挂起后代进程,以进行某些检查和改正,或协调各子进程。
  • 操作系统需要挂起某些进程,检查运行中资源使用情况,以改善系统性能;或当系统出现故障或某些功能受到破坏时,需要挂起某些进程以排除故障。

3.3 Process Description 进程描述

  • Processesand Resources

    –操作系统通过进程管理资源的使用

1.Operating System Control Structures操作系统的控制结构

  • For the OS is to manage processes and resources, it must have information about thecurrent status of each process and resource. 操作系统为管理进程和资源,必须有进程和资源当前状态的信息
  • Tables are constructed for each entity theoperating system manages要为OS管理的每个实体构造表格。

OS Control Tables

Memory Tables

  • Memory tables are used to keeptrack of both main and secondary memory.

    (为什么还有辅存?)

  • Must include this information:

    –Allocation of main memory to processes进程主存空间的分配

    –Allocation of secondary memory to processes进程辅存空间的分配

    –Protection attributes of blocks of main or virtual memory主存或虚拟存储块的保护属性

    –Information needed to manage virtual memory管理虚存的信息

I/O Tables

  • Used by the OS to manage the I/O devices and channels(通道) of the computer.管理设备和通道
  • At any given time, an I/O device may be available or assigned to a particular process可用或已分配
  • The OS needs to know

    –The status of I/O operation

    –The location in main memory being used as the source or destination of the I/O transfer传输用内存地址

File Tables

  • These tables provide information about:

    –Existence of files

    –Location on secondary memory

    –Current Status

    –other attributes.

  • Sometimes this information is maintained by a file management system

Process Tables

  • Must be maintained to manage processes

    –Current state、Process ID、Location in memory,etc

  • There must be some reference to memory, I/O, and files, directly or indirectly必然有对存储、I/O、文件的直接或间接的引用参照
  • The tables themselves must be accessible by the OS and therefore are subject to memory management操作系统可访问,接受内存管理

2 Process Control Structure

  • OS must know to manage and control a process OS管理控制进程必须知道:

    –Processlocation位置

  • Process image is the collection of program,Data, stack, and attributes (Process control block):进程映像是程序、数据、栈和属性的集合(PCB),是进程的物理表示
  • 表明每个进程映像的存储位置(哪些位置信息?取决于存储管理)

    –Process Attributes属性

Process Attributes

  • We can group the process control block information into three general categories:

    –Process identification进程标识信息

    –Processor state information处理器状态信息

    –Process control information进程控制信息

Process Identification

  • Each process is assigned a unique numeric identifier.进程有唯一的数字标识符

    –Many of the other tables controlled by the OS may use process identifiers to cross-reference process tables利用PID交叉访问各种表格

  • Other identifier:

    –Parent process ID

    –User ID

Processor State Information

  • This consists of the contents of processor registers.

    –User-visible registers

    –Control and status registers

    –Stack pointers

  • Program status word (PSW)

    –contains status information

    –Example:the EFLAGS register on Pentium processors

Process Control Information

  • This is the additional information needed by the OS to control and coordinate the various active processes.操作系统控制、协调各个活动进程所需的额外信息

    –调度和状态信息:进程状态、优先级、调度信息、事件

    –数据结构:各种链表指针

    –进程间通信:标志、信号、消息等

    –存储管理:指向存储描述符的指针

    –资源所有和使用信息:

双向循环链表(prev_task和next_task)

进程可运行队列链表(prev_run和next_run)

散列链表(pidhash_next和pidhash_pprev)

等待队列链表(TASK_INTERRUPTIBLE和TASK_UNINTERRUPTIBLE)

Structure of Process Images in Virtual Memory

实际不连续存放

进程队列

  • 处于同一状态的进程PCB链接在一起的数据结构称为进程队列(Process Queues)。
  • 队列的组织方式:线性、链接、索引