Skip to content

Chapter 3 Processes

:material-circle-edit-outline: 约 1405 个字 :material-clock-time-two-outline: 预计阅读时间 5 分钟

进程和线程不一样

Process Concept

一个具有一定独立功能的程序在一个数据集合上的一次动态执行过程,是可并行执行的计算部分

是一个抽象实体,是一个独立的可以调度的活动,需要分配和释放各种资源

  • A process is a program in execution
    • Basic unit of work on a computer, a job, a task
    • A container of instructions with some resources
  • jobs 作业=user programs 用户程序= tasks 任务= process 进程

Processes can be described as either:

  • I/O-bound process (I/O型进程)
    • spends more time doing I/O than computations, many short CPU bursts
  • CPU-bound process (CPU 型进程)
    • spends more time doing computations; few very long CPU bursts.

Process in Memory

进程不仅仅是一串代码,还包括各种被分配的资源

  • A process includes:
    • The program code, also called text section(代码段)
    • Program counter (PC)
    • Registers
    • Data section (global data)(数据段)
    • Stack (temporary data)
    • Heap (dynamically allocated memory)

image-20241009115000846

image-20241009115127936

Process State 进程状态

As a process executes, it changes state

  • New(新)
    • The process is being created.
  • Running(运行、执行)
    • Instructions are being executed.
  • Ready(就绪)
    • The process is waiting to be assigned to a processor (CPU).
  • Waiting(等待、blocked阻塞)
    • The process is waiting for some event to occur.
  • Terminated(终止)
    • The process has finished execution

image-20241009115434531

等待→运行,就绪→等待 这二种状态转换一般不可能发生

进程与程序的区别

  • 进程是动态的,程序是静态的:程序是有序代码的集合;进程是程序的 执行。
  • 进程是暂时的,程序的永久的:进程是一个状态变化的过程,程序可长 久保存。
  • 进程与程序的组成不同:进程的组成包括程序、数据和进程控制块(即 进程状态信息)
  • 进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;通过调用关系,一个进程可包括多个程序

Process Control Block (PCB,进程控制块)

每个进程在操作系统内用进程控制块来表示,是进程存在的标识,它包含与特定进程相关的信息

PCBs 通常用链表组织

image-20241009120759366

我们习惯用一个进程的 PCB 来指代这个进程

Process Scheduling

Scheduling Queues

  • Job queue – set of all processes in the system
  • Ready queue – set of all processes residing in main memory, ready and waiting to execute.
  • Device queues – set of processes waiting for an I/O device

Process migration between the various queues

Process Scheduling

image-20241009121916817

Schedulers

  • Long-term scheduler 长程调度(或作业调度 or job scheduler)
    • 筛选出能推入就绪队列的进程,同时控制并发程度
    • 这种调度每时每分都在执行,会很慢,现代 OS 基本没有这种调度了
    • 从外存选进程进入内存
  • Short-term scheduler 短程调度(或CPU调度 or CPU scheduler)
    • 筛选出下一个被执行的进程,给其分配 CPU
    • 每时每分都在执行,一定很快
  • Medium-Term Scheduler 中程调度
    • 缓解内存紧张,临时取消CPU分配,见下图

image-20241009122653097

Context Switch(上下文切换)

When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process via a context switch

Context of a process represented in the PCB

Time dependent on hardware support

Operations on Processes 进程操作

Process Creation 进程创建

process identified and managed via a process identifier pid

Parent process create children processes

下面是各种操作的选项

  • Resource sharing
    • Parent and children share all resources.
    • Children share subset of parent’s resources.
    • Parent and child share no resources
  • Execution
    • Parent and children execute concurrently
    • Parent waits until children terminate.
  • Address space 地址空间
    • Child duplicate of parent.
    • Child has a program loaded into it.

The parent process may wait for termination of a child process by using the wait() system call

The call returns status information and the pid of the terminated process

父进程用 wait() 检查子进程情况,直至子进程完成

  • 如果子进程在父进程调用 wait() 之前终止,那么子进程进化为僵尸 zombie 进程
  • 如果父进程在调用 wait() 之前终止,那么子进程进化为孤儿 orphan 进程,并被一个特殊的进程收养,进行数据收集

2024年10月10日14点36分

Interprocess Communication 进程通信

  • Independent process cannot affect or be affected by execution of another process.
  • Cooperating process can affect or be affected by the execution of another process

Cooperating processes need interprocess communication (IPC,进程 间通信)

交流是为了交流、同步

*Interprocess Communication (IPC) 进程间通信

之后第六章和第七章会详细讲

IPC provides a Mechanism for processes to communicate and to synchronize their actions without sharing the same address space

  • 通信类型
    • 直接通信
    • 间接通信
  • 常用通信机制
    • 信号(signal)
    • 共享存储区(shared memory)
    • 管道(pipe)
    • 消息(message)
    • 套接字(socket
  • Two models of IPC

    • Shared memory(共享内存)
    • Message passing(消息传递)

image-20241010143956057

IPC in Shared-Memory Systems

设置一块共享内存进行交流

The communication is under the control of the users processes not the operating system

IPC in Message-Passing Systems

要通信时建立一条链路,发送 msg

  • If processes P and Q wish to communicate, they need to:
    • Establish a communication link between them
    • Exchange messages via send/receive

Direct Communication(直接通信)

send (P, message) – send a message to process P

receive(Q, message) – receive a message from process Q

  • Properties

    • Links are established automatically.
    • Between each pair there exists exactly one link.
    • The link may be unidirectional, but is usually bi-directional

Indirect Communication(间接通信)

Messages are directed and received from mailboxes (also referred to as ports)

Each mailbox has a unique id

  • Properties

    • Processes can communicate only if they share a mailbox
    • A link may be associated with many processes
    • Each pair of processes may share several communication links
  • Operations
    1. create a new mailbox (port)
    2. send and receive messages through mailbox
    3. destroy a mailbox

send(A, message) – send a message to mailbox A

receive(A, message) – receive a message from mailbox A

Buffering

无论直接还是简介,收到的消息都是先存放在进程的缓冲队列中

通信连接的对象实际是这些缓存队列

  • Implemented in one of three ways
    • Zero capacity – no messages are queued on a link.
      • Sender must wait for receiver (rendezvous)
    • Bounded capacity – finite length of n messages
      • Sender must wait if link full
    • Unbounded capacity – infinite length
      • Sender never waits