操作系统L3
Process
Multiprogramming
- CPU快速地在进程和进程间切换,每一个运行几十或几百毫秒
- 在任何时候,CPU只运行一个进程
- when the system is booted, many processes are running simultaneously
Process Creation 进程创建
事件会造成进程创建
1.System initialization
2.Created by a running process.
3.User request to create a new process
4.Initiation of a batch job
Foreground processes:与用户交互并为其执行工作的流程
Background processes :处理一些传入请求,称为守护进程 daemons。
Process Termination 进程中止
- 造成进程中止地条件
- 正常退出(自愿的)
- 出错退出(自愿的)
- 严重错误(非自愿)
- 被其他进程杀死(非自愿)

Process state 进程的状态

进程从阻塞状态出来后,会进入等待队列的末尾,等待进入运行状态
Process implement 进程的实现
- 进程表--process table
- 操作系统为每个进程维护一个进程表,其中包含一个条目(称为进程控制块
(PCB))。
- process control block
- 包含了进程状态的重要信息
- 操作系统为每个进程维护一个进程表,其中包含一个条目(称为进程控制块
(PCB))。
- Context switch 上下文切换
- 从P1切换到P2的时候,进程P1(Running)会保存在进程P1的PCB中,P2(Ready)会从P2的PCB中重新读取到CPU寄存器中
- 伪并行:进程之间的快速切换产生了真正并行的假象,称为伪并行。
- Pseudo-parallelism
Multiprogramming 多道程序设计模型
- n个进程,p概率等待 \[ CPU利用率=1 - p^n \]
Thread 线程
- 执行线程是可由调度程序独立管理的最小编程指令序列,调度程序通常是操作系统的一部分
- can be managed independently by a scheduler
- 线程和进程的实现在操作系统之间有所不同,但在大多数情况下,线程是进程的一个组件。
- a thread is a component of a process
- 一个进程中可以存在多个线程,并发执行并共享内存等资源,而不同的进程不共享这些资源。特别是,进程的线程在任何给定时间共享其可执行代码及其变量的值。
- can exist within one process, executing concurrently and sharing resources
需要线程的原因
响应能力:可以同时完成多个活动。
资源共享:线程共享进程的内存和资源。
经济性:线程很容易创建和销毁。
MP(多处理器)体系结构的利用:线程在多CPU系统上很有用。
单线程进程和多线程进程

Thread Model 线程模型
在一个进程中,一些项目被所有线程共享,一些项目对于每个线程来说是私密的

进程和线程的比较
- 线程
- 是轻量级的进程,是CPU利用的基本单位, 一个基本的CPU执行单元
- 线程包含一个线程ID、一个程序计数器、一组寄存器和一个栈
- 线程是进程中的一个实体,是被系统独立调度和分派的基本单位
- 线程自己不拥有系统资源,只拥有一点在运行中必不可少的资源,但它可与同属一个进程的其他线程共享进程所拥有的全部资源。
- 进程
- 传统的进程可控制单线程
- 如果进程有多线程的控制,可以在同一时间进行多个任务
- 线程是调度执行的最小单位,进程是资源分配的最小单位(如虚拟内存资源)
- Process:used to group resources together;
- Thread:the entity scheduled for execution on the CPU
- 引入进程的目的,是为了使多道程序并发执行,以提高资源利用率和系统吞吐量;
- 而引入线程,则是为了减小程序在并发执行时所付出的时空开销,提高操作系统的并发性能。
POSIX thread
POSIX (Portable Operating System Interface) 是一组基于Unix操作系统的标准操作系统接口

在用户空间中实现线程
优点:
- 快速,灵活的,可扩展
- 用户级线程包可以在不支持线程的操作系统上实现
- 允许每个进程有自己定制的调度算法
缺点:
- 阻塞进程中的所有线程;
- 多处理器上没有线程级并行。

在内核空间中实现线程
优点:
- 只阻塞进程中的适当线程;
- 支持多处理器上的线程级并行
缺点:
- 线程管理速度慢;
- 庞大的线程表

混合使用
- 在内核线程上多路复用用户级线程。
- 内核只知道内核级线程,并对其进行调度。
- 更灵活,但更复杂。
System overhead: 系统开销
Cpu utilization: Cpu利用率
System throughput: 系统吞吐量
Check points
- How many states can a process have?
- How to create and terminate a process in Windows?
- What is PCB?
- What is Context Switch?
- What are the differences between process and thread?
Solution
there are three states: running, block, ready
PCB is process control block, which includes the neccessary information that process needs and the process.
从P1切换到P2的时候,进程P1(Running)会保存在进程P1的PCB中,P2(Ready)会从P2的PCB中重新读取到CPU寄存器中
- Thread
- is the basic unit of CPU utilization and scheduler execution
- is a part of the process, is the light process
- can share some resourse of the process with the other threads in the process, but some items are private to each thread
- Process
- is the basic unit of resourse ditribution
- can control single thread or multi thread
- Thread





本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Yeの博客!