Chapter 2 Processes and Threads

2.1 进程(Process)


1. 顺序执行(Sequential Execution)

Sequential Execution

定义:
Definition:
• 在单道程序系统中,一个具有独立功能的程序独占处理器直至最终结束的过程称为程序的顺序执行。
In a single-program system, a program with independent functionality occupies the processor exclusively until it finishes, known as sequential execution.

特点:
Characteristics:
顺序性(Sequential): 程序结构中的指令按顺序执行(可能包含分支或循环)。
Sequential: Instructions in the program structure are executed in order (may include branches or loops).
封闭性(Close): 程序独占所有资源。
Close: The program occupies all resources exclusively.
可再现性(Reappear): 如果初始条件相同,结果也相同。
Reappear: If the initial conditions are the same, the result will be the same.


2. 并发执行(Concurrency Execution)

Concurrency Execution

定义:
Definition:
• 多个程序或程序段在同一时间段内交替执行,称为并发执行。
Multiple programs or program segments are executed alternately within the same time period, known as concurrency execution.

特点:
Characteristics:
并发性(Concurrency): 引入进程的概念,描述程序的并发执行。
Concurrency: Introduces the concept of processes to describe concurrent execution of programs.
多道程序设计(Multiprogramming): 多个程序同时驻留在内存中,交替执行。
Multiprogramming: Multiple programs reside in memory simultaneously and execute alternately.

问题:
Problems:
资源竞争(Compete for Resources): 多个进程竞争有限的系统资源。
Compete for Resources: Multiple processes compete for limited system resources.
通信(Communication): 进程之间需要交换信息。
Communication: Processes need to exchange information.
协作(Collaboration): 进程之间需要协同完成任务。
Collaboration: Processes need to cooperate to complete tasks.

解决方案:
Solution:
• 引入进程(Process)的概念,通过进程管理解决并发执行中的问题。
Introduces the concept of processes to manage concurrency and solve related problems.


3. 进程模型(Process Model)

Process Model

多道程序设计的进程模型:
Process Model in Multiprogramming:
图a: 四个程序的多道程序设计。
Figure a: Multiprogramming of four programs.
图b: 四个独立的顺序进程的概念模型。
Figure b: Conceptual model of four independent, sequential processes.
图c: 任何时刻只有一个程序在运行。
Figure c: Only one program is active at any instant.

imgimage-20250506124729021

核心思想:
Core Idea:
• 在进程模型中,所有可运行的软件都被组织为进程的集合。
In the process model, all runnable software is organized as a collection of processes.


4. 进程的概念(Process Concept)

Process Concept

起源:
Origin:
• 从MULTICS和CTSS/360系统(任务)发展而来。
Developed from MULTICS and CTSS/360 systems (task).

定义:
Definition:
• 进程是对正在运行的程序的抽象。
An abstraction of a running program.
• 进程是程序的一次执行过程。
A program in execution.
• 进程是一个具有一定独立功能的程序关于某个数据集合的一次活动。
A process is an activity of a program with independent functionality on a certain data set.

类比:
Analogy:
• 生日蛋糕食谱(程序)与制作蛋糕的活动(进程)。
Birthday cake recipe (program) vs. the activity of making a cake (process).


5. 进程与程序的区别

Difference Between Process and Program

  1. 程序(Program):
    • 指令的集合,是静态概念。
    The collection of instructions, a static concept.
    进程(Process):
    • 描述并发执行,是动态概念。
    Describes concurrency, a dynamic concept.

  2. 进程包括:
    • 程序、数据和进程控制块(PCB)。
    Process includes program, data, and PCB.

  3. 进程是临时的:
    • 进程在执行时存在,结束后消失。
    Process is temporary; it exists during execution and disappears after completion.
    程序是永久的:
    • 程序存储在存储设备中,长期存在。
    Program is permanent; it is stored on storage devices and exists long-term.

  4. 程序与进程的关系:
    • 一个程序可以是多个进程的执行程序。
    A program can be the execution program of multiple processes.
    • 一个进程可以调用多个程序。
    A process can call multiple programs.

  5. 进程的创建:
    • 进程可以创建其他进程。
    A process can create other processes.


总结

Summary

顺序执行: 程序独占资源,按顺序执行,具有封闭性和可再现性。
Sequential Execution: The program occupies resources exclusively, executes in order, and is closed and reproducible.

并发执行: 多个程序交替执行,引入进程的概念,解决资源竞争、通信和协作问题。
Concurrency Execution: Multiple programs execute alternately, introducing the concept of processes to solve resource competition, communication, and collaboration issues.

进程模型: 将可运行软件组织为进程的集合,描述多道程序设计的行为。
Process Model: Organizes runnable software as a collection of processes, describing the behavior of multiprogramming.

进程与程序的区别:
Difference Between Process and Program:
• 进程是动态的,包括程序、数据和PCB;程序是静态的指令集合。
Process is dynamic, including program, data, and PCB; program is a static collection of instructions.
• 进程是临时的,程序是永久的。
Process is temporary; program is permanent.
• 进程可以创建其他进程,程序可以被多个进程执行。
Process can create other processes; program can be executed by multiple processes.

2.1 进程创建(Process Creation)


1. 进程创建的触发事件

Principal Events That Cause Process Creation

  1. 系统初始化(System Initialization):
    • 系统启动或重启时,操作系统会创建初始进程。
    When the system starts or reboots, the OS creates initial processes.
    前台进程(Foreground Processes): 与用户交互并为用户执行任务的进程。
    Foreground processes interact with users and perform tasks for them.
    后台进程(Background Processes): 处理某些请求的进程,称为守护进程(Daemons)。
    Background processes handle some incoming requests and are called daemons.

  2. 执行进程创建系统调用(Execution of a Process Creation System Call):
    • 例如,在UNIX中使用fork()系统调用创建新进程。
    For example, in UNIX, the fork() system call is used to create a new process.

  3. 用户请求创建新进程(User Request to Create a New Process):
    • 用户在命令行或图形界面中请求创建新进程(例如,点击图标)。
    A user requests to create a new process via the command line or by clicking an icon.

  4. 批处理作业的启动(Initiation of a Batch Job):
    • 系统启动批处理作业时创建新进程。
    A new process is created when the system initiates a batch job.


2. UNIX中的进程创建

Process Creation in UNIX

fork()系统调用:
fork() System Call:
• 在UNIX中,fork()用于创建新进程。
In UNIX, fork() is used to create a new process.
• 最初,父进程和子进程具有相同的内存映像、环境字符串和打开的文件。
Initially, the parent and the child have the same memory image, the same environment strings, and the same open files.
execve()系统调用: 用于加载新程序,替换当前进程的内存映像。
execve() System Call: Used to load a new program, replacing the current process’s memory image.
• 父进程和子进程拥有各自独立的地址空间。
The parent and child have their own distinct address spaces.


3. Windows中的进程创建

Process Creation in Windows

CreateProcess()函数:
CreateProcess() Function:
• 在Windows中,CreateProcess()同时处理进程创建和加载正确程序到新进程中。
In Windows, CreateProcess() handles both process creation and loading the correct program into the new process.
• 函数原型:
Function Prototype:

1
2
3
4
5
6
7
8
9
10
11
12
BOOL CreateProcess(
LPCTSTR lpApplicationName, // 可执行模块的名称
LPTSTR lpCommandLine, // 命令行字符串
LPSECURITY_ATTRIBUTES lpProcessAttributes, // 进程安全属性
LPSECURITY_ATTRIBUTES lpThreadAttributes, // 线程安全属性
BOOL bInheritHandles, // 句柄继承标志
DWORD dwCreationFlags, // 创建标志
LPVOID lpEnvironment, // 新环境块
LPCTSTR lpCurrentDirectory, // 当前目录
LPSTARTUPINFO lpStartupInfo, // 启动信息
LPPROCESS_INFORMATION lpProcessInformation // 进程信息
);


4. 如何列出正在运行的进程?

How to List the Running Processes?

在UNIX中:
In UNIX:
• 使用ps命令列出正在运行的进程。
Use the ps command to list running processes.

在Windows中:
In Windows:
• 使用任务管理器(Task Manager)查看正在运行的进程。
Use the Task Manager to view running processes.


总结

Summary

进程创建的触发事件:
Events That Cause Process Creation:
• 系统初始化、执行fork()、用户请求、批处理作业启动。
System initialization, execution of fork(), user request, initiation of a batch job.

UNIX中的进程创建:
Process Creation in UNIX:
• 使用fork()创建新进程,execve()加载新程序。
Use fork() to create a new process, execve() to load a new program.

Windows中的进程创建:
Process Creation in Windows:
• 使用CreateProcess()同时处理进程创建和程序加载。
Use CreateProcess() to handle both process creation and program loading.

列出正在运行的进程:
List Running Processes:
• 在UNIX中使用ps命令,在Windows中使用任务管理器。
Use ps in UNIX and Task Manager in Windows.

2.1 进程终止(Process Termination)


1. 进程终止的条件

Conditions Which Terminate Processes

  1. 正常退出(Normal Exit - Voluntary):
    • 进程完成任务后主动退出。
    The process exits voluntarily after completing its task.
    示例:
    Examples:
    ◦ 在UNIX中使用exit()系统调用。
    Use exit() in UNIX.
    ◦ 在Windows中使用ExitProcess()函数。
    Use ExitProcess() in Windows.

  2. 错误退出(Error Exit - Voluntary):
    • 进程在执行过程中遇到错误,主动退出。
    The process exits voluntarily due to an error encountered during execution.
    示例:
    Examples:
    ◦ 编译错误(Compiling errors)。
    ◦ 文件未找到(File not found)。

  3. 致命错误(Fatal Error - Involuntary):
    • 进程在执行过程中遇到无法恢复的错误,被迫终止。
    The process is terminated involuntarily due to an unrecoverable error encountered during execution.
    示例:
    Examples:
    ◦ 除以零(Divide by 0)。
    ◦ 执行非法指令(Executing an illegal instruction)。
    ◦ 访问不存在的内存(Referencing nonexistent memory)。

  4. 被其他进程终止(Killed by Another Process - Involuntary):
    • 一个进程被另一个进程强制终止。
    A process is forcibly terminated by another process.
    示例:
    Examples:
    ◦ 在UNIX中使用kill命令。
    Use kill in UNIX.
    ◦ 在Windows中使用TerminateProcess()函数。
    Use TerminateProcess() in Windows.


总结

Summary

进程终止的四种主要条件:
Four main conditions for process termination:
1. 正常退出(Normal Exit): 进程完成任务后主动退出。
Normal Exit: The process exits voluntarily after completing its task.
2. 错误退出(Error Exit): 进程因执行错误主动退出。
Error Exit: The process exits voluntarily due to an execution error.
3. 致命错误(Fatal Error): 进程因无法恢复的错误被迫终止。
Fatal Error: The process is terminated involuntarily due to an unrecoverable error.
4. 被其他进程终止(Killed by Another Process): 进程被另一个进程强制终止。
Killed by Another Process: The process is forcibly terminated by another process.

这些条件涵盖了进程终止的常见场景,无论是自愿还是非自愿。
These conditions cover common scenarios for process termination, whether voluntary or involuntary.

2.1 进程层次结构(Process Hierarchies)


1. 进程层次结构的概念

Concept of Process Hierarchies

父进程与子进程:
Parent and Child Processes:
• 父进程可以创建子进程,子进程又可以创建自己的子进程,从而形成一个进程层次结构。
A parent process can create a child process, and the child process can create its own child processes, forming a process hierarchy.

进程组(Process Group):
Process Group:
• 在UNIX中,这种层次结构被称为“进程组”。
In UNIX, this hierarchy is called a "process group."
init进程: 在UNIX系统中,init进程是所有进程的根进程(PID为1)。
init Process: In UNIX systems, the init process is the root of all processes (with PID 1).


2. UNIX中的进程层次结构

Process Hierarchies in UNIX

特点:
Characteristics:
• 进程之间存在明确的父子关系,形成一个树状结构。
There is a clear parent-child relationship between processes, forming a tree structure.
进程组(Process Group): 用于管理一组相关的进程,例如一个终端会话中的所有进程。
Process Group: Used to manage a group of related processes, such as all processes in a terminal session.

示例:
Example:

1
2
3
4
5
6
init (PID: 1)
├─ bash (PID: 100)
│ ├─ ls (PID: 101)
│ └─ grep (PID: 102)
└─ sshd (PID: 200)
└─ sshd (PID: 201)


3. Windows中的进程层次结构

Process Hierarchies in Windows

特点:
Characteristics:
• Windows没有明确的进程层次结构概念,所有进程在创建时是平等的。
Windows has no concept of process hierarchy; all processes are created equal.
• 进程之间没有父子关系,而是通过句柄(Handle)和进程ID(PID)进行管理。
There is no parent-child relationship between processes; they are managed via handles and process IDs (PIDs).


总结

Summary

UNIX中的进程层次结构:
Process Hierarchies in UNIX:
• 进程之间存在明确的父子关系,形成一个树状结构,称为“进程组”。
There is a clear parent-child relationship between processes, forming a tree structure called a "process group."
init进程是所有进程的根进程。
The init process is the root of all processes.

Windows中的进程层次结构:
Process Hierarchies in Windows:
• 没有进程层次结构的概念,所有进程在创建时是平等的。
No concept of process hierarchy; all processes are created equal.
• 进程之间通过句柄和进程ID进行管理。
Processes are managed via handles and process IDs.

两种操作系统在进程管理上的设计理念不同,UNIX强调层次结构,而Windows强调平等性。
The design philosophies of the two operating systems differ in process management: UNIX emphasizes hierarchy, while Windows emphasizes equality.

2.1 进程状态(Process States)


1. 基本进程状态

Basic Process States

  1. 运行状态(Running):
    • 进程正在使用CPU执行指令。
    The process is using the CPU to execute instructions.

  2. 就绪状态(Ready):
    • 进程已准备好运行,正在等待CPU调度。
    The process is runnable and waiting to be scheduled by the CPU.
    • 处于就绪队列(Ready Queue)中。
    Located in the ready queue.

  3. 阻塞状态(Blocked):
    • 进程无法运行,直到某个外部事件发生(例如,等待键盘输入)。
    The process is unable to run until an external event occurs (e.g., waiting for a key to be pressed).


2. 进程状态之间的转换

Transitions Between Process States

  1. 进程因输入阻塞(Process Blocks for Input):
    • 运行中的进程因等待输入而进入阻塞状态。
    A running process blocks for input and enters the blocked state.

  2. 调度器选择另一个进程(Scheduler Picks Another Process):
    • 调度器从就绪队列中选择另一个进程运行。
    The scheduler picks another process from the ready queue to run.

  3. 调度器选择当前进程(Scheduler Picks This Process):
    • 调度器选择当前进程从就绪状态进入运行状态。
    The scheduler picks this process to move from the ready state to the running state.

  4. 输入可用(Input Becomes Available):
    • 阻塞的进程因输入事件完成而进入就绪状态。
    A blocked process becomes ready when the input event is completed.

img

3. 其他进程状态

Other Process States

  1. 新建状态(New State):
    • 进程刚刚被创建,尚未进入就绪状态。
    The process has just been created and has not yet entered the ready state.

  2. 退出状态(Exit State):
    • 进程已完成执行或被终止,正在等待操作系统回收资源。
    The process has finished execution or been terminated and is waiting for the OS to reclaim its resources.

img

4. 挂起状态(Suspend State)

Suspend State

  1. 阻塞挂起状态(Blocked, Suspend State):
    • 进程因等待外部事件而被挂起,且不占用内存资源。
    The process is blocked and suspended, not occupying memory resources.

  2. 就绪挂起状态(Ready, Suspend State):
    • 进程已准备好运行,但被挂起,不占用内存资源。
    The process is ready to run but is suspended, not occupying memory resources.

img

总结

Summary

基本进程状态:
Basic Process States:
运行(Running): 使用CPU执行指令。
Running: Using the CPU to execute instructions.
就绪(Ready): 等待CPU调度。
Ready: Waiting to be scheduled by the CPU.
阻塞(Blocked): 等待外部事件。
Blocked: Waiting for an external event.

进程状态转换:
Transitions Between Process States:
• 进程因输入阻塞 → 调度器选择另一个进程 → 调度器选择当前进程 → 输入可用。
Process blocks for input → Scheduler picks another process → Scheduler picks this process → Input becomes available.

其他进程状态:
Other Process States:
新建(New): 进程刚被创建。
New: The process has just been created.
退出(Exit): 进程已完成执行。
Exit: The process has finished execution.

挂起状态:
Suspend State:
阻塞挂起(Blocked, Suspend): 进程被挂起且不占用内存。
Blocked, Suspend: The process is suspended and not occupying memory.
就绪挂起(Ready, Suspend): 进程已准备好运行但被挂起。
Ready, Suspend: The process is ready to run but is suspended.

这些状态及其转换描述了进程在生命周期中的行为。
These states and their transitions describe the behavior of processes during their lifecycle.

2.1 进程的实现(Implementation of Processes)


1. 进程的组成(Process Image)

What is in a Process?

用户程序(User Program):
• 进程执行的代码。
The code executed by the process.

用户数据(User Data):
• 进程操作的数据。
The data manipulated by the process.

栈(Stack):
• 用于过程调用和参数传递。
Used for procedure calls and parameter passing.

进程控制块(PCB, Process Control Block):
• 描述进程的属性,用于操作系统管理和控制进程。
Describes the attributes of the process, used by the OS to manage and control the process.


2. 进程控制块(PCB)

Process Control Block (PCB)

PCB的内容:
Contents of a PCB:
• PCB的结构完全依赖于操作系统,不同操作系统可能包含不同的信息。
The architecture of a PCB is completely dependent on the operating system and may contain different information in different operating systems.
示例: Linux的PCB结构为task_struct,包含106个字段。
Example: Linux’s PCB structure is task_struct, containing 106 fields.

典型的PCB字段:
Some Fields of a Typical PCB Entry:
• 进程ID(PID)、进程状态、程序计数器(PC)、寄存器状态、内存管理信息、I/O状态等。
Process ID (PID), process state, program counter (PC), register state, memory management information, I/O status, etc.


3. 进程上下文(Process Context)

Process Context

定义:
Definition:
• 进程上下文的静态描述,包括:
The static description of the whole execution process, including:
用户上下文(User Context): 用户程序和数据。
User Context: User program and data.
寄存器上下文(Register Context): CPU寄存器的状态。
Register Context: State of CPU registers.
系统上下文(System Context): 操作系统资源的状态。
System Context: State of OS resources.

上下文切换(Context Switch):
Context Switch:
• 将CPU从一个进程切换到另一个进程的过程,由调度器执行(详见第2.4章)。
Switching the CPU from one process to another, performed by the scheduler (see Chapter 2.4).
包括:
Includes:
◦ 保存旧进程的PCB状态。
Save the PCB state of the old process.
◦ 加载新进程的PCB状态。
Load the PCB state of the new process.
◦ 刷新内存缓存。
Flush the memory cache.
◦ 更改内存映射。
Change memory mapping.
开销:
Overhead:
◦ 上下文切换是昂贵的(1-1000微秒),不执行有用工作(纯开销),可能成为系统瓶颈。
Context switch is expensive (1-1000 microseconds), no useful work is done (pure overhead), and it can become a bottleneck.


4. 进程表或PCB表(Process Table or PCB Table)

Process Table or PCB Table

定义:
Definition:
• 操作系统维护一个进程表,每个条目是一个PCB。
The OS maintains a process table, where each entry is a PCB.

系统并发度:
Concurrency Degree of the System:
• PCB表的大小决定了系统的并发度。
The size of the PCB table determines the concurrency degree of the system.

组织形式:
Organization Forms:

  1. 链表(Link):
    ◦ 使用链表组织PCB表。
    Use a linked list to organize the PCB table.
  2. 索引表(Index):
    ◦ 使用索引表组织PCB表。
    Use an index table to organize the PCB table.

示例:
Example:

img

1
2
3
4
5
6
7
8
9
10
11
12
PCB Table
├── Ready
│ ├── PCB1
│ ├── PCB2
├── Blocked
│ ├── PCB3
Index Table
├── Ready
│ ├── Index1 -> PCB1
│ ├── Index2 -> PCB2
├── Blocked
│ ├── Index3 -> PCB3


总结

Summary

进程的组成:
Process Image:
• 包括用户程序、用户数据、栈和进程控制块(PCB)。
Includes user program, user data, stack, and process control block (PCB).

进程控制块(PCB):
Process Control Block (PCB):
• 描述进程的属性,不同操作系统的PCB结构可能不同。
Describes the attributes of the process; the architecture of a PCB may vary across operating systems.

进程上下文:
Process Context:
• 包括用户上下文、寄存器上下文和系统上下文。
Includes user context, register context, and system context.

上下文切换:
Context Switch:
• 由调度器执行,保存旧进程状态并加载新进程状态,开销较大。
Performed by the scheduler, saves the state of the old process and loads the state of the new process, with significant overhead.

进程表或PCB表:
Process Table or PCB Table:
• 操作系统维护的PCB表,决定系统并发度,可通过链表或索引表组织。
Maintained by the OS, determines the concurrency degree of the system, and can be organized via linked lists or index tables.

2.2 线程(Thread)


1. 线程的概念

Thread Concept

背景:
Background:
• 如何使多个程序更好地并发执行,同时尽量减少系统开销,成为操作系统设计的重要目标。
How to make multiple programs execute concurrently more efficiently while minimizing system overhead is an important goal in OS design.

解决方案:
Solution:
• 将进程的两个基本属性分开处理:
Separate the two basic attributes of a process:
1. 调度和分派的基本单位: 线程,不拥有资源,轻量级。
Basic unit of scheduling and dispatching: Thread, does not own resources, lightweight.
2. 拥有资源的基本单位: 进程,不频繁切换。
Basic unit of resource allocation: Process, not frequently switched.

线程的定义:
Definition of Thread:
• 线程是进程中的一个顺序执行流,是CPU调度的基本单位。
A thread is a sequential execution stream within a process and the basic unit of CPU utilization.
• 进程是资源分配的基本单位。
A process is the basic unit of resource allocation.


2. 线程的组成

Thread Composition

原进程PCB的拆分:
Splitting of the Original PCB:
• 将进程控制块(PCB)的内容分为两部分:
Split the contents of the Process Control Block (PCB) into two parts:
1. 描述进程资源和空间的部分。
Part describing process resources and space.
2. 描述执行现场、状态及调度的部分。
Part describing execution context, state, and scheduling.
• 第二部分作为线程控制块(TCB)的内容,一个进程内允许多个线程存在。
The second part becomes the content of the Thread Control Block (TCB), and multiple threads are allowed within a process.

新进程的描述:
Description of the New Process:
• 一个独立的进程空间,可装入进程映像。
An independent process space that can load the process image.
• 一个与进程相关联的执行文件。
An executable file associated with the process.
• 进程所用的系统资源。
System resources used by the process.
• 一个或多个线程(进程在创建时一般同时创建第一个线程,其他线程按需创建)。
One or more threads (the first thread is usually created when the process is created, and other threads are created as needed by the user program).

线程的资源:
Thread Resources:
• 线程不拥有系统资源,只有运行所必需的一些数据结构:TCB、程序计数器、寄存器组和栈。
A thread does not own system resources, only some data structures necessary for its execution: TCB, program counter, register set, and stack.
• 线程与同一进程内的其他线程共享进程的全部资源。
A thread shares all resources of the process with other threads in the same process.
堆栈和寄存器: 用于存储线程内的局部变量。
Stack and Registers: Used to store local variables within the thread.
线程控制块(TCB): 说明线程存在的标识,记录线程属性和调度信息(例如,Linux的TCB结构为thread_struct,包含24个字段)。
Thread Control Block (TCB): Identifies the thread and records thread attributes and scheduling information (e.g., Linux’s TCB structure is thread_struct, containing 24 fields).


3. 多线程(Multithreading)

Multithreading

传统进程(单线程):
Traditional (Heavyweight) Process:
• 只有一个控制线程。
Has a single thread of control.

多线程进程:
Multithreaded Process:
• 有多个控制线程,可以同时执行多个任务。
Has multiple threads of control and can perform more than one task at a time.
• 这种情况称为多线程。
This situation is called multithreading.


4. 线程与进程的比较

Thread vs Process

  1. 资源分配:
    Resource Allocation:
    • 进程是资源分配的基本单位,拥有完整的虚拟地址空间。
    A process is the basic unit of resource allocation and has a complete virtual address space.
    • 线程与资源分配无关,属于某个进程,并与该进程内的其他线程共享资源。
    A thread is unrelated to resource allocation, belongs to a process, and shares resources with other threads in the same process.

  2. 地址空间:
    Address Space:
    • 不同进程拥有不同的虚拟地址空间。
    Different processes have different virtual address spaces.
    • 同一进程中的多个线程共享同一地址空间。
    Multiple threads in the same process share the same address space.

  3. 切换开销:
    Switching Overhead:
    • 进程切换涉及资源指针的保存和地址空间的转换,开销较大。
    Process switching involves saving resource pointers and converting address spaces, resulting in high overhead.
    • 线程切换不涉及资源指针的保存和地址空间的变化,开销较小。
    Thread switching does not involve saving resource pointers or changing address spaces, resulting in low overhead.

  4. 调度与切换:
    Scheduling and Switching:
    • 进程的调度与切换由操作系统内核完成。
    Process scheduling and switching are performed by the OS kernel.
    • 线程的调度与切换可由操作系统内核或用户程序完成。
    Thread scheduling and switching can be performed by the OS kernel or user programs.

  5. 创建:
    Creation:
    • 进程可以动态创建子进程。
    A process can dynamically create child processes.
    • 线程也可以创建其他线程。
    A thread can also create other threads.

  6. 生命周期:
    Lifecycle:
    • 进程有创建、执行、消亡的生命周期。
    A process has a lifecycle of creation, execution, and termination.
    • 线程也有类似的生命周期。
    A thread also has a similar lifecycle.


总结

Summary

线程: 进程中的一个顺序执行流,是CPU调度的基本单位,轻量级,不拥有资源。
Thread: A sequential execution stream within a process, the basic unit of CPU utilization, lightweight, and does not own resources.

进程: 资源分配的基本单位,拥有完整的虚拟地址空间。
Process: The basic unit of resource allocation, with a complete virtual address space.

多线程: 一个进程包含多个线程,可以同时执行多个任务。
Multithreading: A process contains multiple threads and can perform multiple tasks simultaneously.

线程与进程的比较:
Thread vs Process:
• 线程切换开销小,共享地址空间;进程切换开销大,拥有独立的地址空间。
Thread switching has low overhead and shares address space; process switching has high overhead and has an independent address space.
• 线程调度可由内核或用户程序完成;进程调度由内核完成。
Thread scheduling can be performed by the kernel or user programs; process scheduling is performed by the kernel.

线程的引入提高了系统的并发性和效率,是现代操作系统的重要特性。
The introduction of threads improves system concurrency and efficiency and is an important feature of modern operating systems.

2.2 线程的优势(Thread Advantage)


1. 线程的创建、退出和切换开销小

Low Overhead for Thread Creation, Exit, and Switching

线程创建(Thread Creation):
• 线程的创建比进程更快,因为线程共享进程的资源,不需要分配新的地址空间或资源。
Thread creation is faster than process creation because threads share process resources and do not require allocating new address space or resources.

线程退出(Thread Exit):
• 线程的退出也比进程更快,因为线程的资源释放和清理工作更简单。
Thread exit is faster than process exit because resource release and cleanup are simpler for threads.

线程切换(Thread Switch):
• 线程切换的开销比进程切换小,因为线程共享地址空间,不需要切换内存映射或资源指针。
Thread switching has lower overhead than process switching because threads share address space and do not require switching memory mappings or resource pointers.


2. 线程间通信简单

Simple Communication Between Threads

共享内存和文件资源:
Shared Memory and File Resources:
• 线程共享所属进程的内存和文件资源,因此线程间通信可以直接通过共享内存进行,无需复杂的机制。
Threads share the memory and file resources of their process, so communication between threads can be done directly through shared memory without complex mechanisms.


总结

Summary

线程的优势主要体现在以下几个方面:
The advantages of threads are mainly reflected in the following aspects:
1. 创建、退出和切换开销小:
Low Overhead for Creation, Exit, and Switching:
• 线程的创建、退出和切换比进程更快,效率更高。
Thread creation, exit, and switching are faster and more efficient than processes.

  1. 线程间通信简单:
    Simple Communication Between Threads:
    • 线程共享进程的内存和文件资源,通信机制简单直接。
    Threads share process memory and file resources, making communication mechanisms simple and direct.

这些优势使得线程在多任务处理和高并发场景中表现出色,是现代操作系统的核心特性之一。
These advantages make threads excel in multitasking and high-concurrency scenarios, making them a core feature of modern operating systems.

2.2 线程模型(The Thread Model)


1. 单线程进程与多线程进程

Single-Threaded vs Multi-Threaded Processes

图a:三个单线程进程(Three Processes Each with One Thread):
• 每个进程只有一个线程,线程之间相互独立。
Each process has only one thread, and the threads are independent of each other.

图b:一个多线程进程(One Process with Three Threads):
• 一个进程包含三个线程,线程之间共享进程的资源。
One process contains three threads, and the threads share the resources of the process.


2. 线程共享与私有的内容

Items Shared and Private to Threads

线程共享的内容(Items Shared by All Threads in a Process):
• 进程的地址空间、全局变量、文件资源等。
Process address space, global variables, file resources, etc.

线程私有的内容(Items Private to Each Thread):
• 线程的栈、程序计数器、寄存器状态等。
Thread stack, program counter, register state, etc.


3. 线程的栈

Each Thread Has Its Own Stack

线程栈的作用:
Role of Thread Stack:
• 用于存储线程的局部变量、函数调用和返回地址。
Used to store thread-local variables, function calls, and return addresses.

线程栈的独立性:
Independence of Thread Stack:
• 每个线程有自己独立的栈,确保线程间的执行上下文互不干扰。
Each thread has its own independent stack, ensuring that the execution contexts of threads do not interfere with each other.

栈相互不干扰

img

总结

Summary

单线程进程与多线程进程:
Single-Threaded vs Multi-Threaded Processes:
• 单线程进程中,每个进程只有一个线程;多线程进程中,一个进程可以包含多个线程。
In a single-threaded process, each process has only one thread; in a multi-threaded process, one process can contain multiple threads.

线程共享与私有的内容:
Items Shared and Private to Threads:
• 线程共享进程的地址空间和资源,但每个线程有自己的栈和寄存器状态。
Threads share the address space and resources of the process, but each thread has its own stack and register state.

线程的栈:
Thread Stack:
• 每个线程有独立的栈,用于存储局部变量和执行上下文。
Each thread has an independent stack for storing local variables and execution context.

线程模型通过共享资源和独立执行上下文,实现了高效的并发执行。
The thread model achieves efficient concurrent execution by sharing resources and maintaining independent execution contexts.

2.2 线程的使用(Thread Usage)


1. 为什么使用线程?

Why Do You Use Threads?

  1. 响应性(Responsiveness):
    • 多个活动可以同时进行,提高应用程序的响应速度。
    Multiple activities can be done at the same time, speeding up the application.

  2. 资源共享(Resource Sharing):
    • 线程共享所属进程的内存和资源,减少资源开销。
    Threads share the memory and resources of the process to which they belong, reducing resource overhead.

  3. 经济性(Economy):
    • 线程的创建和销毁比进程更简单、更高效。
    Threads are easier to create and destroy than processes.

  4. 多处理器架构的利用(Utilization of MP Architectures):
    • 线程在多CPU系统上非常有用,可以充分利用多核处理器的性能。
    Threads are useful on multiple CPU systems, fully utilizing the performance of multi-core processors.

  5. 示例:文字处理器(Word Processor)或电子表格(Spreadsheet):
    一个线程与用户交互。
    One thread interacts with the user.
    一个线程格式化文档(或电子表格)。
    One thread formats the document (or spreadsheet).
    一个线程定期将文件写入磁盘。
    One thread writes the file to disk periodically.
    为什么不使用三个进程?
    Why not three processes?
    ◦ 进程的创建和切换开销更大,且进程间通信更复杂。
    Process creation and switching have higher overhead, and inter-process communication is more complex.


2. 示例:多线程文字处理器

Example: Multithreaded Word Processor

描述:
Description:
• 一个文字处理器包含三个线程,分别负责用户交互、文档格式化和文件写入。
A word processor contains three threads, responsible for user interaction, document formatting, and file writing, respectively.


3. 示例:多线程Web服务器

Example: Multithreaded Web Server

描述:
Description:
调度器线程(Dispatcher Thread): 负责接收请求并将其分发给工作线程。
Dispatcher Thread: Receives requests and distributes them to worker threads.
工作线程(Worker Thread): 负责处理请求,包括查找缓存、从磁盘读取页面并返回页面。
Worker Thread: Handles requests, including looking up the cache, reading pages from disk, and returning pages.

代码框架:
Rough Outline of Code:
调度器线程:
Dispatcher Thread:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  while (TRUE) {
get_next_request(&buf);
handoff_work(&buf);
}
```
• **工作线程:**
**Worker Thread:**
```c
while (TRUE) {
wait_for_work(&buf);
look_for_page_in_cache(&buf, &page);
if (page_not_in_cache(&page))
read_page_from_disk(&buf, &page);
return_page(&page);
}


总结

Summary

线程的优势:
Advantages of Threads:
• 提高响应性、资源共享、经济性,以及充分利用多处理器架构。
Improves responsiveness, resource sharing, economy, and utilization of multiprocessor architectures.

示例:
Examples:
文字处理器: 使用多个线程分别处理用户交互、文档格式化和文件写入。
Word Processor: Uses multiple threads for user interaction, document formatting, and file writing.
Web服务器: 使用调度器线程和工作线程高效处理请求。
Web Server: Uses dispatcher and worker threads to efficiently handle requests.**

线程通过并发执行和资源共享,显著提高了应用程序的性能和效率。
Threads significantly improve application performance and efficiency through concurrent execution and resource sharing.

2.2 POSIX 线程(POSIX Threads, Pthreads)


1. Pthreads 函数调用

Pthreads Function Calls

函数调用 描述
pthread_create 创建一个新线程。
Create a new thread.
pthread_exit 终止调用线程。
Terminate the calling thread.
pthread_join 等待指定线程退出。
Wait for a specific thread to exit.
pthread_yield 释放CPU,让其他线程运行。
Release the CPU to let another thread run.
pthread_attr_init 创建并初始化线程的属性结构。
Create and initialize a thread's attribute structure.
pthread_attr_destroy 删除线程的属性结构。
Remove a thread's attribute structure.

2. 示例程序:使用线程

Example Program Using Threads

代码:
Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUMBER_OF_THREADS 10

void *print_hello_world(void *tid) {
/* 该函数打印线程的标识符并退出。 */
/* This function prints the thread's identifier and then exits. */
printf("Hello World. Greetings from thread %d\n", (int)tid);
pthread_exit(NULL);
}

int main(int argc, char *argv[]) {
/* 主程序创建10个线程并退出。 */
/* The main program creates 10 threads and then exits. */
pthread_t threads[NUMBER_OF_THREADS];
int status, i;

for (i = 0; i < NUMBER_OF_THREADS; i++) {
printf("Main here. Creating thread %d\n", i);
status = pthread_create(&threads[i], NULL, print_hello_world, (void *)i);

if (status != 0) {
printf("Oops. pthread_create returned error code %d\n", status);
exit(-1);
}
}

exit(NULL);
}

功能:
Functionality:
• 主程序创建10个线程,每个线程打印“Hello World”并退出。
The main program creates 10 threads, and each thread prints "Hello World" and exits.

代码说明:
Code Explanation:
1. pthread_create 创建线程,传入线程函数print_hello_world和线程ID。
pthread_create: Creates a thread, passing the thread function print_hello_world and the thread ID.
2. pthread_exit 线程执行完毕后调用pthread_exit退出。
pthread_exit: Called by the thread to exit after execution.
3. 错误处理: 如果pthread_create返回非零值,表示线程创建失败,程序退出。
Error Handling: If pthread_create returns a non-zero value, the thread creation fails, and the program exits.


总结

Summary

POSIX 线程(Pthreads):
POSIX Threads (Pthreads):
• 提供了一组标准的API,用于创建和管理线程。
Provides a set of standard APIs for creating and managing threads.

常用函数:
Common Functions:
pthread_createpthread_exitpthread_joinpthread_yieldpthread_attr_initpthread_attr_destroy
pthread_create, pthread_exit, pthread_join, pthread_yield, pthread_attr_init, pthread_attr_destroy.

示例程序:
Example Program:
• 使用pthread_create创建多个线程,每个线程执行独立的任务。
Uses pthread_create to create multiple threads, each performing an independent task.

POSIX 线程是跨平台的线程实现,广泛应用于多线程编程中。
POSIX Threads is a cross-platform thread implementation widely used in multithreaded programming.

2.2 线程的实现(Implementation of Threads)


1. 三种线程实现机制

Three Mechanisms for Implementing Threads

  1. 在用户空间实现线程(Implementing Threads in User Space):
    描述:
    Description:
    ◦ 线程的管理完全由用户空间的线程库完成,操作系统内核并不知道线程的存在。
    Thread management is entirely handled by a user-space thread library, and the OS kernel is unaware of the threads.
    优点:
    Advantages:
    ◦ 线程切换开销小,不需要内核介入。
    Low overhead for thread switching, no kernel involvement required.
    ◦ 线程库可以针对特定应用进行优化。
    The thread library can be optimized for specific applications.
    缺点:
    Disadvantages:
    ◦ 一个线程的阻塞会导致整个进程阻塞,因为内核并不知道线程的存在。
    Blocking of one thread blocks the entire process because the kernel is unaware of the threads.
    ◦ 无法利用多核处理器的优势,因为内核只看到一个进程。
    Cannot take advantage of multi-core processors because the kernel sees only one process.

  2. 在内核空间实现线程(Implementing Threads in Kernel Space):
    描述:
    Description:
    ◦ 线程的管理由操作系统内核完成,内核知道每个线程的存在。
    Thread management is handled by the OS kernel, and the kernel is aware of each thread.
    优点:
    Advantages:
    ◦ 一个线程的阻塞不会影响其他线程,因为内核可以调度其他线程。
    Blocking of one thread does not affect other threads because the kernel can schedule other threads.
    ◦ 可以充分利用多核处理器的优势。
    Can fully utilize the advantages of multi-core processors.
    缺点:
    Disadvantages:
    ◦ 线程切换开销较大,因为需要内核介入。
    Higher overhead for thread switching due to kernel involvement.
    ◦ 线程管理的复杂性增加。
    Increased complexity in thread management.

  3. 混合实现(Hybrid Implementations):
    描述:
    Description:
    ◦ 结合用户空间和内核空间的优点,将线程分为用户级线程和内核级线程。
    Combines the advantages of user-space and kernel-space implementations by dividing threads into user-level threads and kernel-level threads.
    实现方式:
    Implementation:
    ◦ 用户级线程由用户空间的线程库管理,内核级线程由内核管理。
    User-level threads are managed by a user-space thread library, and kernel-level threads are managed by the kernel.
    ◦ 多个用户级线程映射到一个内核级线程。
    Multiple user-level threads are mapped to one kernel-level thread.
    优点:
    Advantages:
    ◦ 减少线程切换开销,同时支持多核处理器。
    Reduces thread switching overhead while supporting multi-core processors.
    缺点:
    Disadvantages:
    ◦ 实现复杂性较高。
    Higher implementation complexity.


总结

Summary

用户空间实现线程:
Implementing Threads in User Space:
• 线程管理由用户空间的线程库完成,切换开销小,但无法利用多核处理器。
Thread management is handled by a user-space thread library, with low switching overhead but no multi-core processor utilization.

内核空间实现线程:
Implementing Threads in Kernel Space:
• 线程管理由内核完成,支持多核处理器,但切换开销较大。
Thread management is handled by the kernel, supporting multi-core processors but with higher switching overhead.

混合实现:
Hybrid Implementations:
• 结合用户空间和内核空间的优点,减少切换开销并支持多核处理器,但实现复杂性较高。
Combines the advantages of user-space and kernel-space implementations, reducing switching overhead and supporting multi-core processors, but with higher implementation complexity.

线程的实现方式需要根据应用场景和性能需求进行选择。
The implementation of threads should be chosen based on application scenarios and performance requirements.

2.2 线程的实现(续)

Implementation of Threads (Continued)


1. 用户空间实现线程

Implementing Threads in User Space

描述:
Description:
• 线程的管理完全由用户空间的线程库完成,操作系统内核并不知道线程的存在。
Thread management is entirely handled by a user-space thread library, and the OS kernel is unaware of the threads.
示例: 用户级线程包(User-Level Threads Package)。
Example: A user-level threads package.

优点:
Advantages:
• 线程切换开销小,不需要内核介入。
Low overhead for thread switching, no kernel involvement required.
• 线程库可以针对特定应用进行优化。
The thread library can be optimized for specific applications.

缺点:
Disadvantages:
• 一个线程的阻塞会导致整个进程阻塞,因为内核并不知道线程的存在。
Blocking of one thread blocks the entire process because the kernel is unaware of the threads.
• 无法利用多核处理器的优势,因为内核只看到一个进程。
Cannot take advantage of multi-core processors because the kernel sees only one process.


2. 内核线程(Kernel Threads, KLT)

Kernel Threads (KLT)

描述:
Description:
• 线程的创建、调度和管理由内核完成。
The kernel performs thread creation, scheduling, and management in kernel space.
• 不需要线程库,内核提供API。
No thread library is needed; the kernel provides APIs.
• 内核维护进程和线程的上下文。
The kernel maintains the context of processes and threads.
• 线程切换需要内核介入。
Thread switching requires kernel involvement.
• 线程是调度的基本单位。
Thread is the basic unit of scheduler.

缺点:
Disadvantages:
• 线程切换的开销较高。
High cost for thread switching.

示例:
Examples:
• Windows 95/98/NT/2000/XP。
Windows 95/98/NT/2000/XP.


3. 内核空间实现线程

Implementing Threads in the Kernel

描述:
Description:
• 线程的管理由内核完成,内核知道每个线程的存在。
Thread management is handled by the OS kernel, and the kernel is aware of each thread.
示例: 由内核管理的线程包(A Threads Package Managed by the Kernel)。
Example: A threads package managed by the kernel.

优点:
Advantages:
• 一个线程的阻塞不会影响其他线程,因为内核可以调度其他线程。
Blocking of one thread does not affect other threads because the kernel can schedule other threads.
• 可以充分利用多核处理器的优势。
Can fully utilize the advantages of multi-core processors.

缺点:
Disadvantages:
• 线程切换开销较大,因为需要内核介入。
Higher overhead for thread switching due to kernel involvement.
• 线程管理的复杂性增加。
Increased complexity in thread management.


4. 混合实现

Hybrid Implementations

描述:
Description:
• 结合用户空间和内核空间的优点,将线程分为用户级线程和内核级线程。
Combines the advantages of user-space and kernel-space implementations by dividing threads into user-level threads and kernel-level threads.
实现方式:
Implementation:
◦ 多个用户级线程映射到一个内核级线程。
Multiple user-level threads are mapped to one kernel-level thread.
示例: SUN Solaris。
Example: SUN Solaris.

优点:
Advantages:
• 减少线程切换开销,同时支持多核处理器。
Reduces thread switching overhead while supporting multi-core processors.

缺点:
Disadvantages:
• 实现复杂性较高。
Higher implementation complexity.


5. 弹出线程(Pop-Up Threads)

Pop-Up Threads

描述:
Description:
• 当消息到达时创建新线程来处理消息。
Creation of a new thread when a message arrives.
图a: 消息到达前。
Figure a: Before the message arrives.
图b: 消息到达后,创建新线程处理消息。
Figure b: After the message arrives, a new thread is created to handle the message.

优点:
Advantages:
• 动态创建线程,提高响应速度。
Dynamically creates threads to improve responsiveness.


总结

Summary

用户空间实现线程:
Implementing Threads in User Space:
• 线程管理由用户空间的线程库完成,切换开销小,但无法利用多核处理器。
Thread management is handled by a user-space thread library, with low switching overhead but no multi-core processor utilization.

内核线程(KLT):
Kernel Threads (KLT):
• 线程管理由内核完成,支持多核处理器,但切换开销较大。
Thread management is handled by the kernel, supporting multi-core processors but with higher switching overhead.

内核空间实现线程:
Implementing Threads in the Kernel:
• 线程管理由内核完成,支持多核处理器,但切换开销较大。
Thread management is handled by the kernel, supporting multi-core processors but with higher switching overhead.

混合实现:
Hybrid Implementations:
• 结合用户空间和内核空间的优点,减少切换开销并支持多核处理器,但实现复杂性较高。
Combines the advantages of user-space and kernel-space implementations, reducing switching overhead and supporting multi-core processors, but with higher implementation complexity.

弹出线程:
Pop-Up Threads:
• 动态创建线程处理消息,提高响应速度。
Dynamically creates threads to handle messages, improving responsiveness.

线程的实现方式需要根据应用场景和性能需求进行选择。
The implementation of threads should be chosen based on application scenarios and performance requirements.

2.2 线程总结(Summary of Threads)


1. 什么是线程?为什么需要线程?

What is a Thread? Why Do We Need Threads?

线程的定义:
Definition of Thread:
• 线程是进程中的一个顺序执行流,是CPU调度的基本单位。
A thread is a sequential execution stream within a process and the basic unit of CPU utilization.

为什么需要线程:
Why We Need Threads:
• 提高并发性、响应性和资源利用率,同时减少系统开销。
To improve concurrency, responsiveness, and resource utilization while reducing system overhead.


2. 线程与进程的区别

Difference Between Process and Thread

特性 进程 线程
资源分配 资源分配的基本单位。 与资源分配无关,共享进程资源。
地址空间 拥有独立的虚拟地址空间。 与同一进程内的其他线程共享地址空间。
切换开销 切换开销较大。 切换开销较小。
调度与切换 由内核完成。 可由内核或用户程序完成。
创建与销毁 创建和销毁开销较大。 创建和销毁开销较小。

3. 线程的使用

Thread Usage

示例:
Examples:
文字处理器: 使用多个线程分别处理用户交互、文档格式化和文件写入。
Word Processor: Uses multiple threads for user interaction, document formatting, and file writing.
Web服务器: 使用调度器线程和工作线程高效处理请求。
Web Server: Uses dispatcher and worker threads to efficiently handle requests.**


4. 线程的实现及其权衡

Thread Implementations and Their Tradeoffs

  1. 用户级线程(User-Level Threads):
    优点: 切换开销小,无需内核介入。
    Advantages: Low switching overhead, no kernel involvement.
    缺点: 无法利用多核处理器,一个线程的阻塞会导致整个进程阻塞。
    Disadvantages: Cannot utilize multi-core processors, blocking of one thread blocks the entire process.

  2. 内核级线程(Kernel-Level Threads):
    优点: 支持多核处理器,一个线程的阻塞不会影响其他线程。
    Advantages: Supports multi-core processors, blocking of one thread does not affect other threads.
    缺点: 切换开销较大,需要内核介入。
    Disadvantages: Higher switching overhead, requires kernel involvement.

  3. 混合实现(Hybrid Implementations):
    优点: 减少切换开销,同时支持多核处理器。
    Advantages: Reduces switching overhead while supporting multi-core processors.
    缺点: 实现复杂性较高。
    Disadvantages: Higher implementation complexity.

  4. 弹出线程(Pop-Up Threads):
    优点: 动态创建线程处理消息,提高响应速度。
    Advantages: Dynamically creates threads to handle messages, improving responsiveness.


5. 下一讲:进程间通信(Inter-Process Communication, IPC)

Next Lecture: Inter-Process Communication

主题:
Topics:
• 进程间通信的机制和方法,如管道、消息队列、共享内存等。
Mechanisms and methods for inter-process communication, such as pipes, message queues, shared memory, etc.


总结

Summary

线程: 是进程中的一个顺序执行流,用于提高并发性和资源利用率。
Thread: A sequential execution stream within a process, used to improve concurrency and resource utilization.

线程与进程的区别:
Difference Between Process and Thread:
• 线程共享进程资源,切换开销小;进程拥有独立资源,切换开销大。
Threads share process resources and have low switching overhead; processes have independent resources and higher switching overhead.

线程的实现方式:
Thread Implementations:
• 用户级线程、内核级线程、混合实现和弹出线程各有优缺点,需根据应用场景选择。
User-level threads, kernel-level threads, hybrid implementations, and pop-up threads each have their pros and cons, and should be chosen based on application scenarios.

下一讲: 进程间通信(IPC)。
Next Lecture: Inter-Process Communication (IPC).

线程是现代操作系统的核心特性之一,理解其实现和使用方式对于开发高效并发程序至关重要。
Threads are a core feature of modern operating systems, and understanding their implementation and usage is crucial for developing efficient concurrent programs.