Operating Systems: Process & Thread
Processes and threads
Describe Processes or Programs
Usually we use the Dircted Acyclic Graph(DAG) to describe the sequence of execution between processes or programs.
The seconde picture describe the concurrent execution. Such as the I2 and C1 or I3, C2 and P1 all are the concurrent.
Concurrent means execution the process at the same time, we use the virtualization to realize it.
通常使用有向无环图来表示进程的执行顺序
这里给出了两张图片,分别是顺序执行的进程和并发执行的进程
在第二张图片中I2 and C1 或者 I3, C2 and P1 都是并发的。
并发实际上就是在同一时间运行多个进程,但是在实际上十分困难,我们通常使用虚拟化的技术去实现他。
Process And Program
Process is a program in execution. One program can create multiple processes, but one process only by one program.
In short, the process is an abstraction of program.
The Process:
- Is unit of work in mordern Time-Sharing (And Space-Sharing) System.
- Must execution by sequential fashion.
- Need certain resources to accomplish its task.
- The resource including CPU time, memory, file, I/O devices.
- And resource are allocated to process either when it is created or while it is executing.
进程是正在执行的程序,一个程序可以创建多个进程,但是一个进程只对应一个程序。
简而言之,进程是抽象的程序
对与进程有以下几个特点:
- 在现代 时分共享(和空分共享)系统中进程是一个工作单元
- 必须按顺序执行
- 需要确定的资源去完成任务(例如确定的寄存器以及CPU执行时间)
- 这些资源包括 CPU 时间、内存、文件、输入输出设备等等
- 这些资源在他被创建或被执行的时候申请
A process includes:
- Text section
- Program code, instruction
- Date Section
- Global Variable
- Includes BSS Section (Block Started by Symbol). BSS memory the data which is uninitialized.
- Program Counter
- Stack
- Temporary Data, such as function parameters, return addresses, local variables
- Heap
- Dynamically allocated memory during process run time.
Attention, this may be non-unique from OS to OS.
一个进程包括以下几部分:
- 代码段 -> 又称文本段,包含程序代码和指令,也可能包含一些只读数据段,如常量等。(有些立即数会与指令存放在一起)
- 数据段 -> 存储全局变量,包括 BSS 段。BSS 用来存储未初始化的或者是初始化为0的全局变量、静态变量。
- 程序计数器 -> 在计算机组成原理中有涉及,执行一条指令 PC+1
- 栈 -> 存放临时数据,用作暂时存储区,为局部变量提供存储空间
- 堆 -> 存放动态申请的内存空间。
在不同的操作系统中这部分可能会有些不同
With the picture, we can find that the stack is extend to lower addresses and the heap is extend to upper addresses. And Space between the stack and the heap is empty spaces(unmapped).
在图上我们可以看到栈空间是向低地址扩展的,堆空间是向高地址扩展的,在栈和堆之间是空洞(未映射区域)
Process State
State Transition
The process has some State when it executes:
- new: The process is being created;( Use System Call: fork() )
- running: Instruction are being executed;
- waiting: The process is waiting for some event to occur;
- ready: The process is waiting to be assigned to processor;
- terminated: The process has finished execution.
Process will switch from one state to other states, this action was called ‘Process State Transition’.
State transition need sending request or occuring interrupt.
进程在执行时会有以下几种状态:
- new: 进程刚被创建(使用系统调用 fork() )
- running: 指令正在被执行
- waiting: 进程等待响应,从而进行状态切换
- ready: 进程正在等待分配给处理器进行处理
- terminated: 进程执行完毕,终止进程
进程从一个状态切换到另一个状态被称为“状态转换”
状态转换需要发送请求或者发生中断。
Process Control Block
Information associated with each process need a PCB to store:
- Process State
- Program Counter
- CPU Register
- accumulators
- index registers
- stack pointers
- etc.
- CPU Scheduling Information
- process priority
- pointers to scheduling queue
- etc.
- Memory-Management-Information
- Accounting information
- Amount of CPU and real time used
- Time limits
- account numbers
- etc.
- I/O Status Information
When the OS make Context Switch, it will store the PCB first, then to load PCB of other process.
和进程有关的信息需要通过PCB(进程控制块)来存储:
- 进程状态
- 程序计数器
- CPU寄存器
- 累加器
- 索引寄存器
- 栈指针
- etc.
- CPU调度信息
- 程序优先级
- 调度队列的指针
- etc.
- 内存管理信息
- 账户(账单)信息
- 已使用的CPU和实时应用
- 时间限制
- 账号
- etc.
- I/O 状态信息
操作系统进行上下文切换时会首先保存当前程序的 PCB然后加载另一个程序的 PCB
Creation And Termination
Process Creation
Four events cause processes to be created:
- System initialization
- Execution of a process-creation system call by a running process
- A user request to create a new process
- Initiation of a batch job
四种事件可以创建进程:
- 系统初始化
- 正在运行的进程执行了 创建进程 的系统调用( 如 fork()调用 )
- 用户申请创建新进程
- 批处理初始化
Pseudo Code
create(s0 CPU status,m0 memory,pi priority , pid process ID){
p=Get_New_PCB();
pid=Get_New_PID();
p-> ID = pid;
p->CPU_State = s0;
p-> Memory = m0;
p-> Priority = pi;
p-> Status.Type = ‘ready_s’;
p-> Status. List= RL (ready list);
p-> Creation_Tree.Parent= self;
p-> Creation_Tree.Child = NULL;
insert(self->Creation_Tree.Child,p);
insert(RL,p);
Scheduler();
}
Process Termination
Four events can cause the process to terminate:
- Normal exit (voluntary)
- Error exit (voluntary)
- Fatal exit (involuntary)
- Killed by another process (involuntary)
四种情况会导致进程终止:
- 正常退出
- 错误退出
- 致命错误导致的退出
- 被其他进程杀死
Pseudo Code
Kill_Tree(p){
for(each q in p -> Ceation_Tree.Child)
Kill_Tree(q);
if(p->Statue.Type == 'running'){
cpu = p-> Processor_ID;
Interrupt(cpu);
}
Remove(p -> Status.List,p);
Release_all(p -> Memory);
Release_all(p -> Other_Resources);
Close_all (p -> Open_Files);
Delete_PCB(p);
}
System Call
In the programming, we always invoke some system call to create(or terminate) a new process.
- fork()
- exec()
- exit()
Parent And Child Process
Parent process create children processes, which in turn create other processes, forming a tree of processes.
Most OSs identify processes by a pid (process ID)
Resource sharing strategies
- 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
父进程创建子进程,子进程有按顺序创建其他进程,形成一颗流程树
大多数的操作系统通过 PID 识别程序
资源的共享策略:
- 父进程和子进程共享全部资源
- 子进程共享父进程的资源的一个子集
- 父进程和子进程不共享资源
执行的策略:- 父进程和子进程并发执行
- 父进程等待子进程终止
Context Switches
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, so it can be restarted later as if it had never been stopped.
- Time dependent on hardware support.
- A context is the contents of a CPU’s registers and program counter at any point in time
- A context switch is the switching of the CPU from one process or thread to anothe
Context switch is always about scheduling.
- Long-term scheduler
- Short-term scheduler
Thread
1.Benefits of Thread
- Responsiven
- Resource Sharing
- Economy
- Utilization of multiprocessor architectures
- Thread Models
- Many to One
- One to One
- Many to Many
- Lab: Operating Systems: Lab 0 Threads
Review Questions
- Why is the concept of process introduced? What is the impact?
Programs can be executed concurrently in multiprogramming environment, and the concurrent programs can be controlled and described. Enable concurrent execution of programs
- The execution of a process can not be interrupted.
False
- What is a PCB? Why it is the only sign of the existence of a process?
PCB records all the information needed by the operating system, it describes the process and controls the process running. In the whole life cycle of a process, the system always controls the process through its PCB. The system perceives the existence of the process according to its PCB. Therefore, PCB is the only sign of the existence of the process