Operating Systems: Process & Thread

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.

SequentialExecution1

ConcurrentExecution1

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:

  1. Is unit of work in mordern Time-Sharing (And Space-Sharing) System.
  2. Must execution by sequential fashion.
  3. 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.

进程是正在执行的程序,一个程序可以创建多个进程,但是一个进程只对应一个程序。
简而言之,进程是抽象的程序
对与进程有以下几个特点:

  1. 在现代 时分共享(和空分共享)系统中进程是一个工作单元
  2. 必须按顺序执行
  3. 需要确定的资源去完成任务(例如确定的寄存器以及CPU执行时间)
    • 这些资源包括 CPU 时间、内存、文件、输入输出设备等等
    • 这些资源在他被创建或被执行的时候申请

A process includes:

  1. Text section
    • Program code, instruction
  2. Date Section
    • Global Variable
    • Includes BSS Section (Block Started by Symbol). BSS memory the data which is uninitialized.
  3. Program Counter
  4. Stack
    • Temporary Data, such as function parameters, return addresses, local variables
  5. Heap
    • Dynamically allocated memory during process run time.

Attention, this may be non-unique from OS to OS.

一个进程包括以下几部分:

  1. 代码段 -> 又称文本段,包含程序代码和指令,也可能包含一些只读数据段,如常量等。(有些立即数会与指令存放在一起)
  2. 数据段 -> 存储全局变量,包括 BSS 段。BSS 用来存储未初始化的或者是初始化为0的全局变量、静态变量。
  3. 程序计数器 -> 在计算机组成原理中有涉及,执行一条指令 PC+1
  4. 栈 -> 存放临时数据,用作暂时存储区,为局部变量提供存储空间
  5. 堆 -> 存放动态申请的内存空间。
    在不同的操作系统中这部分可能会有些不同

ProcessInMemory

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).

在图上我们可以看到栈空间是向低地址扩展的,堆空间是向高地址扩展的,在栈和堆之间是空洞(未映射区域)

程序的内存空间 – CSDN

Process State

State Transition

The process has some State when it executes:

  1. new: The process is being created;( Use System Call: fork() )
  2. running: Instruction are being executed;
  3. waiting: The process is waiting for some event to occur;
  4. ready: The process is waiting to be assigned to processor;
  5. 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.

ProcessStateTransitionDiagram

RepresentationOfProcessScheduling

进程在执行时会有以下几种状态:

  1. new: 进程刚被创建(使用系统调用 fork() )
  2. running: 指令正在被执行
  3. waiting: 进程等待响应,从而进行状态切换
  4. ready: 进程正在等待分配给处理器进行处理
  5. terminated: 进程执行完毕,终止进程

进程从一个状态切换到另一个状态被称为“状态转换”
状态转换需要发送请求或者发生中断。

Process Control Block

Information associated with each process need a PCB to store:

  1. Process State
  2. Program Counter
  3. CPU Register
    • accumulators
    • index registers
    • stack pointers
    • etc.
  4. CPU Scheduling Information
    • process priority
    • pointers to scheduling queue
    • etc.
  5. Memory-Management-Information
  6. Accounting information
    • Amount of CPU and real time used
    • Time limits
    • account numbers
    • etc.
  7. I/O Status Information

ProcessControlBlock

When the OS make Context Switch, it will store the PCB first, then to load PCB of other process.

和进程有关的信息需要通过PCB(进程控制块)来存储:

  1. 进程状态
  2. 程序计数器
  3. CPU寄存器
  • 累加器
  • 索引寄存器
  • 栈指针
  • etc.
  1. CPU调度信息
  • 程序优先级
  • 调度队列的指针
  • etc.
  1. 内存管理信息
  2. 账户(账单)信息
  • 已使用的CPU和实时应用
  • 时间限制
  • 账号
  • etc.
  1. I/O 状态信息

操作系统进行上下文切换时会首先保存当前程序的 PCB然后加载另一个程序的 PCB

Creation And Termination

Process Creation

Four events cause processes to be created:

  1. System initialization
  2. Execution of a process-creation system call by a running process
  3. A user request to create a new process
  4. Initiation of a batch job

四种事件可以创建进程:

  1. 系统初始化
  2. 正在运行的进程执行了 创建进程 的系统调用( 如 fork()调用 )
  3. 用户申请创建新进程
  4. 批处理初始化

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:

  1. Normal exit (voluntary)
  2. Error exit (voluntary)
  3. Fatal exit (involuntary)
  4. Killed by another process (involuntary)

四种情况会导致进程终止:

  1. 正常退出
  2. 错误退出
  3. 致命错误导致的退出
  4. 被其他进程杀死

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.

  1. fork()
  2. exec()
  3. 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.

  1. Long-term scheduler
  2. Short-term scheduler

Thread

1.Benefits of Thread

  1. Responsiven
  2. Resource Sharing
  3. Economy
  4. Utilization of multiprocessor architectures
  5. Thread Models
    1. Many to One
    2. One to One
    3. Many to Many
  6. Lab: Operating Systems: Lab 0 Threads

Review Questions

  1. 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

  1. The execution of a process can not be interrupted.

False

  1. 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

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇