作业的基本概念:用户再一次计算过程中或一次事务处理过程中,要求计算机系统所做的工作的集合。
包含多个程序、多个数据、作业控制说明书
系统调用时操作系统提供给编程人员的唯一接口。
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