Transcript Ch07-2.ppt

Scheduling
 Key to multi-programming
多道程序设计的关键是调度
 Four types of scheduling 调度有4种类型
Long term
长项调度
Medium term 中项调度
Short term
短项调度
I/O
I/O调度
1
Scheduling
Process: 进程定义
A program in execution
一个运行的程序
The “animated spirit” of a program
程序的“活灵魂”
That entity to which a processor is assigned
处理器分配的实体
2
Long Term Scheduling
 Determines which programs are submitted for
processing
确定哪一些程序被提交到系统处理
i.e. controls the degree of multi-programming
它控制多道程序设计的度(内存中的进程数)
 Once submitted, a job becomes a process for the
short term scheduler
一旦提交,作业就成为进程加入到短项调度程序的队列中等
待短项调度
(or it becomes a swapped out job for the medium
term scheduler)
加入到中项调度程序的队列中
3
Long Term Scheduling
 Batch system 在批处理系统中
newly submitted jobs are held in a batch queue
新提交的作业保存在批处理队列中
Long term scheduler creates processes from the
queue
长项调度程序从队列中选择任务为其创建进程
Criteria include priority, expected execution time,
I/O requirements
选择采用的标准有:优先级、期望的执行时间和I/O需求
4
Long Term Scheduling
 Time sharing system 分时系统中
When a user attempts to connect to the system, a
process request is generated
当用户希望连接系统时,产生一个进程请求
Operate system will accept all authorized comers
分时用户不是简单地排队等待,而是操作系统接收所有允
许的请求
“the system is full and user should try again later”
when system is saturated
当系统饱和时连接请求会遇到“系统已满和以后再试”的信
息
5
Medium Term Scheduling
 Part of the swapping function (later…)
中项调度是7.3节内存管理将介绍的换入裁决的一部分
 Usually based on the need to manage multiprogramming
一般,换入裁决是基于多道程序“度”的管理需要
 With or without virtual memory, memory
management is an issue
无论有没有虚拟存储系统,存储管理都是一个重要问题,
6
Short Term Scheduler
 Dispatcher (也称派遣程序)
 Execute frequently 执行频繁
 Fine grained decisions of which job to execute next
并细粒度地判决下次执行哪个作业
i.e. which job actually gets to use the processor in the
next time slot
在下一个时间片哪个作业将获得处理器的使用权
 High-level scheduler 高级调度程序
 Execute relatively infrequently 执行相对较少
 Coarse grained decision of whether or not to take on a new
process 粗粒度地决定是否接受一个新进程
7
Process States
8
Process Control Block 进程控制块(PCB)
Identifier
标识符
State
状态
Priority
优先级
Program counter 程序计数器
Memory pointers 存储器指针
Context data
现场数据
I/O status
I/O状态信息
Accounting information 统计信息
9
PCB Diagram
When the scheduler accepts a new job, it
creates a blank process control block and
places the associated process in the new
state.After the system has properly filled
in the process control block, the process is
transferred to the ready state.
当调度程序接收到一个新的作业时,它
产生一个空的进程控制块,并使相关进
程处于新的状态。当系统填完PCB后,
进程进入就绪状态。
10
Process Scheduling
11
Memory Management
 Uni-program 在单道程序设计系统中
Memory split into two 主存划分成两大部分
One for Operating System (monitor)
一部分给操作系统(常驻监控程序)
One for currently executing program
另一部分结当前正在执行的程序
 Multi-program 在多道程序设汁系统中
“User” part is sub-divided and shared among
active processes
存储器的“用户”部分必须进一步划分以适应多个进程
12
Swapping
 Problem: I/O is so slow compared with CPU that even
in multi-programming system, CPU can be idle most of
the time
由于处理器速度比I/o快得多,因此,即使在多道程序设计中处
理器仍可能有许多时间处于空闲状态。
 Solution
Increase the degree of multiproramming 增加多道程
序度
 Approaches 解决办法
Increase main memory 扩充主存
Expensive 主存昂贵
Leads to larger programs程序对主存容量需求增长更
快
Swapping 交换技术
13
What is Swapping?
 Long term queue of processes stored on disk, Processes are
brought in as space becomes available, As a process completes
it is moved out of main memory
有一个进程请求的长项队列通常存储在磁盘上,当主存有空间时,进程被
调入,每次调入一个。当进程完成时,移出主存。
 If none of the processes in memory are ready (i.e. all I/O
blocked)
当存储器中无进程处于就绪状态时 (如:I/O阻塞)
Swap out a blocked process to intermediate queue
处理器把这些进程中的一个调回磁盘,排人中间队列
Swap in a ready process or a new process
然后从中间队列中调入另一个进程执行
But swapping is an I/O process...
然而,交换是—种I/O操作
14
Swapping
Disk
Storage
Disk
Storage
Main
Memory
Operating
System
Long-Term
Queue
Simple Job Scheduling
Intermediate
Queue
Completed
Job
Main
Memory
Operating
System
Completed
Job
Long-Term
Queue
Swapping
15
Partitioning
 Splitting memory into sections to allocate to processes
(including Operating System)
将内存分成几部分,分配给多个进程,包括操作系统
 Fixed-sized partitions 定长分区
May not be equal size
分区是固定长度,但是大小可以不等
Process is fitted into smallest hole that will take it (best
fit)
当一个进程调入主存时,分配给它一个能容纳它的最小的分区
Some wasted memory 但是主存仍然有浪费
Leads to variable-sized partitions 产生了变长分区
16
Fixed
Partitioning
17
Variable Sized Partitions (1)
 Allocate exactly the required memory to a process
当—个进程调入主存时,分配的分区大小与进程所需大小一样
 This leads to a hole at the end of memory, too small
to use
Only one small hole - less waste
开始所有程序按顺序连续分配内存,只在存储器末端留下一
空块,浪费少
 When all processes are blocked, swap out a process
and bring in another
当所有进程都阻塞后,换出一个进程,然后换进一个新的
 New process may be smaller than swapped out
process
新换进来进程的大小可能小于换出后留下的空间
 Another hole 这样就又形成另一个空块
18
Effect of Dynamic Partitioning
19
Variable Sized Partitions (2)
Eventually have lots of holes
(fragmentation)
最后内存中会留下很多空块(碎片)
Solutions:
Compaction - From time to time go
through memory and move all hole into
one free block (c.f. disk defragmentation)
为了解决这个问题.采用一种紧缩技术。
操作系统一次次移动存储器中的进程,把所有空闲的空
间放在起,组成新的块。如计算机磁盘碎片整理
20
Relocation 重定位
 No guarantee that process will load into the same
place in memory当进程它换入时一般都分配到不同的
分区
 Instructions contain addresses 指令中包含地址
Locations of data 数据地址
Addresses for instructions (branching) 指令地址
(分支指令)
 Logical address - relative to beginning of program
逻辑地址表示相对于程序起始的单元
 Physical address - actual location in memory (this
time)
物理地址是主存中的一个实际单元地址
 Automatic conversion using base address
当处理器执行进程时,通过把当前进程的起始单元地址(
称为基地址)加到每个逻辑地址上,自动地把逻辑地址转
21
换成物理地址。
Paging 分页
 Split memory into equal sized, small chunks -page
frames
把存储器分成相等的固定长且比较小的存储块(页帧)
 Split programs (processes) into equal sized small
chunks – pages 把每个进程也划分成小的固定长的程序块
(页)
 Allocate the required number page frames to a
process
为每个程序分配必须数量的页帧
 Operating System maintains list of free frames
操作系统负责维护空闲帧列表
 A process does not require contiguous page frames
一个进程并不需要连续的页帧
 Use page table to keep track 它通过页表进行维护 22
Logical and Physical Addresses
frame
0
1
2
3
23
Paging Example
The size of a physical main memory is 128 Page Table
bytes, A process has 8 pages, and each
0 2
page has 4 bytes
1 4
2 8
a) What is the length of a logical address?
3 30
b) What is the length of a physical address? 4 5
5 15
c) Given the following page table, what is the 6 16
7 27
physical address of logical address 13?
24
Paging Example
Ans:
a) page# offset
page#: 3 bits, offset: 2 bits  5 bits
b) frame# offset
frame#: 5 bits, offset: 2 bits  7 bits
Page Table
0
1
2
3
4
5
6
7
2
4
8
30
5
15
16
27
c) logical address 13 = 011 01
 physical address = 11110 01 = 121
25
Virtual Memory 虚拟存储器
 Demand paging (请求分页)
Do not require all pages of a process in memory, bring in
pages as required
一个进程的所有页不需要都装入内存,每个页只在需要时调人即可
 Page fault 缺页处理
Required page is not in memory
当所需要的页不在内存时
Operating System must swap in required page
OS需要将其调入
May need to swap out a page to make space
这时可能因为没有空的页帧而需要调出一页
Select page to throw out based on recent history
要调出的页可以根据最近使用的原则进行选择
26
Thrashing (系统抖动)
 Too many processes in too little memory, Operating
System spends all its time swapping, Little or no real
work is done, Disk light is on all the time
当有大量进程在内存时,OS将忙于进行页交换,真正用于工
作的时间就会减少,硬盘也会不停的工作
 Solutions
Good page replacement algorithms 好的页替代算法
Reduce number of processes running
减少运行的进程数
More memory 增大内存
27
Bonus of Virtual Memory 虚存的优点
 We do not need all of a process in memory for it to
run. We can swap in pages as required
一个进程不需要全部装入主存就可以运行。可以在需要时换入
 So - we can now run processes that are bigger than
total memory available!
可以运行大于整个主存的进程
 Main memory is called real (or physical) memory
主存称为实(物理)存储器
 User/programmer sees much bigger memory - virtual
memory
程序员/用户看到一个分配在磁盘上的太得多的存储器称为虚拟
存储器
28
Page Table Structure 页表结构
Problem
Each process needs one page table
一个进程需要一张页表
Page tables need to be stored in main memory
页表必须驻留主存
E.g., 231 virtual memory per process
page size = 29 bytes
 231/ 29 = 222 page table entries
 too big
29
Page Table Structure
Solutions
Page page tables (将页表分页)
Multi-level scheme (多级分页)
Inverted page table (倒置分页)
30
Page Table Structure
31
Translation Lookaside Buffer
(转换后备缓冲)
 Each virtual memory reference causes two physical memory
accesses (why?)
每次虚拟存储器的访问要产生两次物理存储器存取
Fetch page table entry 一次是获取相应的页表项
Fetch data 另一次是获取所需的数据
 Solution: TLB
Special cache for page table entries recently used
用来存储最近使用的页表项的特殊cache
 For a memory access
CPU checks the TLB first 首先CPU查询TLB
If not found then access page table (in memory)
如果不出现,则从内存页表中取项
Then access what we want 然后查找我们所需要的数据
32
P257
TLB and Cache Operation
34
Segmentation 分段
 Paging is not (usually) visible to the programmer
分页对程序员来说是不可见的
 Segmentation is visible to the programmer 分段可见
 Segment: variable, dynamic size
段的长度是可变的动态的
 Usually different segments allocated to program and data
通常把程序和数据分配给不同的段
 May be a number of program and data segments
各种类型的程序可以有许多程序段和数据段
 Some systems combine segmentation with paging
有些系统装有提供分页和分段两种方法
35
Pentium II
 Hardware for segmentation and paging
包含用于分段和分页的硬件
 Unsegmented unpaged 未分段未分页存储器
 Unsegmented paged 分页未分段存储器
 Segmented unpaged 分段未分页存储器
 Segmented paged 分段分页存储器
36
Pentium II Address Translation
Mechanism
16
32
10
10
12
37
Pentium II Segmentation(1)
Each virtual address is 16-bit segment and 32-bit
offset
每个虚拟地址由一个16位段引用和一个32位偏移
量组成
2 bits of segment are protection mechanism
段中两位用于保护机制
14 bits specify segment
余下的14位表示一个具体的段
38
Pentium II Segmentation(2)
 Unsegmented virtual memory 232 = 4Gbytes
对无分段的存储器,用户的虚拟地址是232 = 4G字节
 Segmented 246=64 terabytes
对有分段的存储器,用户虚拟空间为246=64T字节
Can be larger – depends on which process is active
根据当前哪个进程是活动的, 虚拟存储器容量实际能大于
64T字节, 并被分成两部分
Half is global
一半是全局的 ,被所有进程共享
Half is local and distinct for each process,
另一半是局部的.每个进程都不同。
39
Pentium II Segmentation
全局共享虚拟空间
物理存储器
处理器P1的
私用虚拟空间
物理存储器
处理器P2的
私用虚拟空间
P1
空间
P2
空间
Synonym problem
Half (8K segments of 4Gbytes) is global
Half is local and distinct for each process
40
Pentium II Protection
Each segment has two forms of protection:
与每段有关的两种保护方式是
Privilege level 优先级
Access attribute 存取属性
41
Pentium II Protection – privilege(1)
Privilege level
Protection bits give 4 levels of privilege from 0 most
protected to 3 least ;
保护优先级有4种,从最高(0级)到最低(3级)
An executing program may only access data segments
for which its privilege level is ≤ the privilege level of
the data segment.
当正在执行的程序许可级低于(更高特权)或等于(相同特
权)数据段的优先级时,它可存取此数据段。
42
Pentium II Protection – privilege(2)
 Privilege level
Usually level 3 for applications, level 1 for O/S and
level 0 for kernel (level 2 not used)
通常3级用于应用程序,优先级1用于操作系统,0级用
于小部分专用于存储管理、保护和存取控制的操作系统
内核,2级未用
Level 2 may be used for apps that have internal
security
e.g. database
2级可以用于要实现自己的安全机构的特殊且必须受保
护的应用子系统
Except for regulating access to data segments,the
privilege mechanism limits the use of certain
instructions.
优先级机制除了控制对数据段的访问权限外.它还用来
限制某些指令的使用
43
Pentium II Protection – access
 Access attribute
Specifies a data segment whether read-write or
read-only accesses are permitted; and specifies a
program segment read-execute or read-only access.
一个数据段的存取属性表明该数据段是否允许读/写存
取或只读存取。对于程序段,存取属性表示该程序段
读执行或只读存取。
44
Pentium II Paging
 Segmentation may be disabled 分段可以被禁止
In which case linear address space is used 程序中使用线性
地址
 Two level page table lookup 二级页表检索
First, page directory
第1级是页目录
1024 entries max
包含可达1024个项
Splits 4G linear memory into 1024 page groups of
4Mbyte
把4G字节的线性存储空间分隔成1024个长度为4M字节的页组
Each page table has 1024 entries corresponding to
4Kbyte pages
每个页表包含达1024个项,每项对应单个的4K字节页
Page directory for current process always in memory
当前任务的页目录总是在主存中
45
Pentium II Address Translation
Mechanism
16bits
32bits
10bits 10bits 12bits
46
作业
7.1
7.5
7.10
7.12
47