操作系统02进程管理Process_Description_and_Control
作業的基本概念:用戶再一次計算過程中或一次事務處理過程中,要求計算機系統所做的工作的集合。
包含多個程序、多個數據、作業控制說明書
系統調用時操作系統提供給編程人員的唯一接口。
1、文件操作類;
2、進程控制類;
3、資源申請類;
4、進程通信類;
5、信息維護類 ?用戶程序和操作系統間傳遞信息,如獲得時間。
作業調度算法:
先來先服務
最短作業優先
響應比高優先 ? ?響應比=(等待時間+計算時間)/ 計算時間
優先級
分類調度算法 ? ?根據系統運行情況和作業屬性將作業分類,如CPU tense / IO tense
作業調度 --> 進程 --> 處理機調度
————————————————————————————————————————————————————————————
OS:
>進程管理子系統 CPU
>存儲管理子系統 Memory
>IO設備子系統 IO Devices
>文件管理子系統 File
?
進程是一個程序對某個數據集的一次運行活動;資源分配單位。
特征:動態、并發、獨立、異步
結構:
程序段 Code Segment
數據段 Data Segment
進程控制塊 Process Control Block PCB
?
線程是進程內一個相對獨立的,可調度的執行單元;不再分配資源。
?
————————————————————————————————————————————————————————————
Process and Scheduling 進程與調度
基礎:進程描述及控制
實現:同步與互斥
避免:死鎖與饑餓
解決:幾個經典問題:生產者--消費者 ?讀者--寫者 ?哲學家進餐
關于:進程通信
策略:進程調度
?
Process Description and Control
PCB Process Control Block 一種數據結構
OS目標:高利用率、高效率。
程序的順序執行:順序性 封閉性 可再現性
程序的并發執行:間斷性 非封閉性 不可再現性
程序并發執行的條件(Bernstein條件):
進程是程序在一個數據集合上的運行過程,是系統進行資源分配和調度的一個獨立單位。
進程是可并發執行的程序在一個數據集合上的運行過程。
特點:動態性、并發性、獨立性、異步性。
?
Process Structure:
·Programs
·Data
·PCB ?(Process Control Block)
?
Dispatcher = Scheduler
調度程序
?
進程狀態轉換:
Two-State Process Model
>Running
>Not-running
--Ready
--Blocked
?
??
Five-State Model
Running:占用處理機
Ready:準備執行
Blocked:等待某事件發生才能執行
New:進程已經創建,但OS未接納
Exit:因停止或取消,被OS釋放
?
Using Two Queues
Seven-State Model
Swapping 對換技術,交換技術
將進程的數據、程序
內存 Swapping Out 外存 ???Suspended Processes(掛起)
外存 Swapping In 內存
?
·Processor is faster than IO so all processes could be waiting for IO.
·Swap these processes to disk to free up more memory.
·Blocked state becomes suspend state when swapped to disk.
?
四種狀態組合:
Ready
Blocked
Ready, suspend
Blocked, suspend
?
處理機可調度兩種進程:
新創建的進程 <--- 其次
換入一個以前掛起的進程 <-- 優先
?
One suspend state
??
Two?suspend state
?
Process Description
OS 如何感知進程、控制進程及其所用的系統資源?
圖:
?
?
?
Operating System Control Structures
·Information about the current states of each process and resource.
·Tables are constructed for each entity the operating system manages.
---Memory Tables
---IO Tables
---File Tables
---Process Table
Memory Tables:
·Allocation of main memory to processes
·Allocation of secondary memory to processes
·Protection attributes for access to shared memory regions
·Information needed to manage virtual memory
IO Tables:
·IO device is available or assigned
·Location in main memory being used as the source or destination of the IO transfer
?
File Table:
·Existence of files
·Location on secondary memory
·Current states
·Attributes
·Sometimes this information is maintained by a file-management system
?
Process Table:
·Where process is located
·Attributes necessary for its management
---Process ID
---Process state
---Location in memory
?
Process Location:
·Process includes set of programs to be executed
? ? ---Data location for local and global variables
? ? ---Any defined constants
? ? ---Stack
·Process Control Block (PCB)
? ? ---Collection of attributes
·Process Image (進程映像)
? ? ---Collection of programs, data, stack and attributes
?
Process Image:
·User Data
·User Programs
·System Stack 存放系統及進程調用地址參數
·Process Control Block (PCB) ?OS感知進程,控制進程的數據結構
?
Process Control Block
PCB:是OS控制和管理進程時所用的基本數據結構
作用:PCB是相關進程存在于系統中的唯一標志,系統根據PCB而感知相關進程的存在。
包括:
Process Identification
Processor State Information
Process Controlled Information
?
Process Identification:
---Identifiers
·Identifier of this process (進程ID)
·Identifier of the process that created this process (parent process 父進程ID)
·User Identifier (用戶ID)
Processor State Information:
---User-Visible Registers
·A user-visible register is one that be referenced by means of the machine language that the processor executes.
·Typically, there are from 8 to 32 of these registers, although some RISC(reduce instruction set computer) implementations have over 100.
---Control and Status Registers
There are a variety of processor registers that are employed to control the operation of processor.
These include
·Program Counter: contains the address of the next instruction to be fetched.
·Condition Codes: result of he most recent arithmetic or logical operation
(e.g. Sign, zero, carry, equal, overflow)
·Status Information: Includes interrupt enable/disable flags, execution mode.
---Stack Pointers
·Each process has one or more last-in-first-out(LIFO) system stacks associated with it. A stack is used to store parameters and calling addresses for procedure and system call.
?
Process Controlled Information
---Scheduling and State Information
·Process state: defines the readiness of the process to be scheduled for execution (e.g. Running Ready Waiting Halted).
·Priority: one or more fields may be used to describe the scheduling priority of the process.
·Scheduling-related information: this will depend on the scheduling algorithm used.
·Event: Identity of event the process is awaiting before it can be resumed.
---Data Structuring
·A process may be linked to other process in a queue, or some other structures.
·A process may exhibit a parent-child (creator-created) relationship with another process.
·The process control block may contain pointers to other process to support the structures.
---Inter-Process Communication
·Various flags, signals, and messages may be associated with communication between two independent processes.
---Process Privileges
·Processes are granted privileges in terms of the memory that may be accessed and the types of instructions that may be executed.
---Memory Management
·This section may include pointers to segments and/or page tables that describe the virtual memory assigned.
---Resource Ownership and Utilization
·Resource controlled by the process may be indicated, such as opened files.
·A history of utilization of the processor or other resources may also be includes: this information may be needed by the scheduler.
PCB組成若干鏈表,存放在OS中專門開辟的PCB區內。
PCB可以被操作系統多個模塊讀或修改。
如調度程序、資源分配程序、中斷處理程序、監督分析程序。
?
——————————————————————————————————————————————————————————————————————————
內核與硬件相關,是在硬件上的第一層擴充。
Typical Function of an OS kernel
資源管理功能
·Process Management:進程創建和終止、調度、狀態轉換、同步和通信、管理PCB
·Memory Management:為進程分配地址空間、對象、段頁管理
·IO Management:緩存管理、為進程分配IO通道和設備
支撐功能
·Interrupt Handling (中斷處理)
·Timing (時鐘管理)
·Primitive (原語) ?Atomic Operation ? ? ? ?若干條機器指令構成,要么不做,要么全做。
·Accounting (統計)
·Monitoring (監測)
?
Process Control Primitive (原語)
·Process Switch 進程切換
·Create and Terminate 創建與終止
·Block and Wake-up 阻塞與喚醒
·Suspend and Activate 掛起與激活
?
When to switch a process
·clock interrupt
---process has executed for the maximum allowable time slice.
·IO interrupt
·Memory fault (存儲訪問失效--基于虛擬存儲)
---Memory address is in virtual memory so it must be brought into main memory.
·Trap (陷阱)
---error occurred.
---may cause process to be moved to Exit State
·Supervisor Call (管理程序調入)
---such as file open.
?
?
Change of Process State
·Save context of processor including program counter and other registers.
·Update the PCB of the process that is currently running.
·Move PCB to appropriate queue---ready, blocked,...
·Select another process for execution.
·Update the PCB of the process selected.
·Update memory--management data structures.
·Restore context of the selected process.
?
Process Switching V.S. Mode Switching
·Process Switching:作用于進程之間的一種操作,當分派程序收回當前進程的CPU并準備把它分派給某個就緒進程時,該操作將被引用。
·Mode Switching:進程內部所引用的一種操作,當進程映像所包含的程序引用核心子系統所提供的系統調用時,該操作將被引用。 用戶態?與 核心態?的相互轉換。
?
Process Switching 一定有 Mode Switching
Mode Switching 不一定有 Process Switching
?
?
Process Creation
·Submission of a batch job.
·User logs on.
·Created to provide a service such as printing.
·Process create another process.
?
Create()原語,步驟
1、為進程分配一個唯一標識號ID:主進程表中增加一個新的表項。
2、為進程分配空間:用戶地址空間、用戶棧空間、PCB空間,若共享已有空間,則應建立相應的鏈接。
3、初始化PCB:進程標識、處理機狀態信息、進程狀態。
4、建立鏈接:若調度隊列是鏈表,則將新進程插入到就緒或就緒、掛起鏈表。
5、建立或擴展其他數據結構
?
Destroy()原語,步驟
1、根據被終止的標識符ID,找到PCB,讀出該進程的狀態。
2、若該進程為執行狀態,則終止其執行,調度新進程執行。
3、若該進程有子孫進程,則立即終止其所有子孫進程。
4、將該進程的所有資源,或歸還給其父進程,或歸還給系統。
5、將被終止進程的PCB從所在隊列中移出,等待其他程序來搜集信息。
?
Process Termination
·Batch job issues Halt instruction.
·User logs off.
·Quit an application.
·Error and fault conditions:
·Normal completion 正常結束
·Time limit exceeded 超時終止,執行時間超過預計時間
·Memory unavailable 內存不足,無法為進程分配所需大的內存空間
·Bounds violation 越界訪問
·Protection error 企圖使用未允許使用的數據,或操作方式錯誤
·Arithmetic error 計算錯誤,如除零,或企圖存儲硬件允許的最大值
·Time overflow 超時等待某事件發生
·IO failure 如 找不到文件 或 多次重試仍無法讀寫文件 或 無效操作
·Invalid instruction 企圖執行不存在的指令
·Privileged instruction 企圖執行特權指令
·Data misuse 數據類型不符 或 未初始化
·Operating System intervention 操作員或OS干預,如死鎖時
·Parent terminates so child processes terminate
·Parent request
?
?
Process Block and Wake-up
阻塞原因
·請求系統服務
·啟動某種操作,如IO
·新數據尚未到達 ?A產生數據,B處理數據
·無新工作可做
?
阻塞原語 block()
當出現阻塞事件,進程調用阻塞原語將自己阻塞(高風亮節),狀態變為“阻塞狀態”,并進入相應事件的阻塞隊列。
?
喚醒原語 wake-up()
當阻塞進程期待的事件發生,有關進程調用喚醒原語,將等待該事件的進程喚醒,狀態變為ready,插入就緒隊列。
?
?
Process Suspend and Active
掛起原語 suspend()
當出現掛起事件,系統利用掛起原語將指定進程或阻塞狀態進程掛起,進程從內存換到外存,插入相應隊列。
Ready --> Ready, Suspend
Blocked --> Blocked, Suspend
激活原語 active()
當激活事件發生,系統利用激活原語將指定進程激活,進程從外存換到內存,插入相應隊列。
Ready, Suspend --> Ready
Blocked, Suspend --> Blocked
?———————————————————————————————————————————————————————————————————————————
?
進程:多個程序并發以提高資源利用率及系統吞吐量
線程:減少程序并發時的時間開銷,使OS具有更好的并發性。
Thread (線程)
·An execution state (running, ready, etc.).
·Saved thread context when not running.
·Has an execution stack.
·Some per-thread static storage for local variables.
·Access to the memory and resources of its process--all threads of a process share this.
?
Benefits of Threads
·Take less time to create a new thread than a process.
·Less time to terminate a thread than a process.
·Less time to switch between two threads within the same process.
·Since threads with the same process share memory and files, they can communicate with each other without invoking the kernel.
·Suspending a process involves suspending all threads of the process--since all threads share the same address space.
·Termination of a process, terminates all threads within the process.
?
Multi-threading
·Operating system supports multiple threads of execution with in a single process.
·MS-DOS support a single thread.
·UNIX supports multiple user processes, but only supports one thread per process.
主要用于服務器,多用戶,多進程系統。
·Windows Linux ?PC機,多用戶,多任務(線程),多進程多線程系統。
·Windows Solaris Linux Mach OS/2 support multiple threads.
Tread States
·Key states for a thread: Running, Ready, Blocked.
·Operations associated with a change in thread state
--Spawn(派生),spawn another thread
--Block
--Unblock
--Finish
?
進程:資源擁有的基本單位
線程:調度和控制的基本單位
線程切換只需保存和設置少量寄存器的內容。
User--Level Threads
·All thread management is done by application.
·The kernel is not aware of the existence of threads.
描述此類線程的數據結構以及控制此類線程的原語在核外子系統中實現。
Kernel--Level Threads
·W2K, Linux and OS/2 are examples of this approach.
·Kernel maintains context information for the process and the threads.
·Scheduling is done on a thread basis.
描述此類線程的數據結構以及控制此類線程的原語在核心子系統中實現。
?
Combined Approaches
·Example is Solaris --> Sun 工作站
·Thread creation done in the user space.
·Bulk of scheduling and synchronization of threads done in the user space.
Relationship between Threads and Processes
?
用戶級線程切換速度快,內核級線程,系統調用時,可運行同進程內的其他線程。
Thread:Process?= 1:1
Description:?Each thread of execution is a unique process with its own address space and resources.
Example Systems:?Traditional UNIX implementations.
?
Thread:Process?= M:1
Description:?A process defines an address space and dynamic resource ownership.
??Multiple threads may be created and executed within that process.
Example Systems:?Windows NT ?Linux ?Solaris ?OS/2 ?OS/390 ?MACH
?
Thread:Process?= 1:M
Description:?A thread may migrate from one process environment in another.
??This allows thread to be easily moved among distinct system.
Example Systems:?Ra(clouds) ?Emerald
?
Thread:Process?= M:M
Description:?Combines attributes of M:1 and 1:M cases.
Example Systems:?TRIX
總結
以上是生活随笔為你收集整理的操作系统02进程管理Process_Description_and_Control的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 操作系统01概述
- 下一篇: 操作系统03进程管理Process_Sc