期末复习
Chpter 3 Process
- 操作系统进行任务调度和资源分配的基本单位
- Process include:
- Program code
- text section
- program counter and processors’ registers
- Stack
- Function parameters
- Return address
- Local variables
- data section
- Global variables
- Heap
- Dynamically allocated memory
- Process State
- 五态模型
- new
- ready:waiting to be assigned to a processor
- waiting: waiting for some event to occur
- running
- terminated
- 五态模型
- Process control block(PCB)
- 包含信息有:
- Process number
- Process state
- Program counter
- 下条指令的地址
- CPU registers
- CPU scheduling information
- Memory-management information
- Accounting information
- I/O status information
- 包含信息有:
Process Scheduling
Scheduling queues
- Job queue
- set of all processes in the system
- As processes/job enter the system,they are put into the job queue
- Ready queue
- set of all processes residing in “(main) memory”, ready and “waiting” to execute
- Device queues
- set of processes waiting for an I/O device
Scheduler(调度器)
- Long-term scheduler or job scheduler
- job queue -> ready queue
- may be absent on Time-sharing system such as UNIX and Windows
- They put every new process in memory for the short-term scheduler
- Short-term scheduler Or CPU scheduler: 进程调度
- selects which process should be executed next and allocates CPU
- 因为其执行十分频繁,所以每次选择不能耗时太长,否则就overhead
- Medium-term scheduler Or swapping
- swap out: removes processes from memory to disk and reduces the degree of multiprogramming
- swap in: introduce process into memory
Context Switch
- CPU switches to another process
Operations on Processes
- fork()
- The new process consists of a copy of the address space of the original process
- The return code for the fork() is zero for the child process
- 子进程会复制父进程的地址空间和资源,但并不会复制父进程的线程
Interprocess Communication
- Shared memory
- Message passing
名词解释:
- multiprogramming: is to have some process running at all times, to maximize CPU utilization
- time sharing : is to switch the CPU among processes so frequently that users can interact with each process
Chapter 4 Thread
- 进程 vs 线程
- 线程是CPU的分布单位
- 进程是资源的分布单位
- 线程是进程中的执行单元
- 一个进程可以包含多个线程,它们共享相同的地址空间和系统资源,如open files, signals。
- 每个线程有自己的栈空间和执行上下文,但它们在同一个进程内共享代码段、数据段和堆等资源。
- benefits of multithreaded programming
- Responsiveness
- Resource sharing
- Economy
- utilization of multiprocessor architectures
Multithreading Models
两种线程
- User Threads
- Provided by a thread library at the user level
- Kernel Threads
- Provided and managed by the OS directly
- User Threads
Relationship between kernel threads and user threads
- Many-to-one model
- One-to-one model-
- Many-to-many model
- Two-level Model
- 主体是Many-to-many model
- A user thread (important task) can be bound to a kernel thread
Two versions of fork() in UNIX systems
- To duplicate all the threads
- If exec() is not called after forking, then to duplicate all threads
- To only duplicate the thread that invoked the fork() system call
- If exec() is called immediately after forking, then only to duplicate the calling threads
- To duplicate all the threads
Chapter 5 CPU Scheduling
Basic Concepts
- CPU scheduling decisions may take place when a process:
- Switches from running to waiting state
- The result of an I/O request
- An invocation of wait for the termination of one of the child processes (e.g. wait(NULL);)
- Switches from running to ready state
- When a interrupt occurs
- Switches from waiting to ready
- Completion of I/O
- Terminates
- Switches from running to waiting state
- Non-preemptive (非剥夺)
- Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU
- 调度只可能发生在情况 1. 和 4.
- 简单,硬件要求低
- Preemptive(剥夺)
Scheduling Criteria
- CPU utilization
- CPU throughout
- number of processes that complete their execution per time unit.
- Process turnaround time
- From the time of submission of a process to the time of completion, include
- Waiting to get into memory
- Waiting in the ready queue
- Executing on the CPU
- Doing I/O
- From the time of submission of a process to the time of completion, include
- Process waiting time (等待时间)
- amount of time that a process spent waiting in the ready queue.
- Process response time (响应时间)
- amount of the time from the submission of a request until the first response/result is produced
Scheduling Algorithms
- First come first served (FCFS)
- non-preemptive
- Convoy effect (护航效应)
- Shortest job first (SJF)
- minimum average waiting time
- 种类
- Preemptive SJF allows to preempt the currently executing process
- Non-preemptive
- 比较适用于长程调度
- Priority scheduling
- 问题:starvation
- 解决: Aging (时效) – as time progresses increase the priority of the process
- Round robin (RR)
- Is designed for especially for time-sharing systems
- preemptive
- time quantum
- 需要保证 80%的cpu bursts < the time quantum
- Response time = 2*(n-1)*q
- Multilevel queue algorithm
- Multilevel feedback queue algorithm
- the most general scheduling algorithm
Multiple-Processor Scheduling
- homogeneous vs. heterogeneous CPUs
- homogeneous: 各处理器都一样
- Approaches to multiple-processor scheduling
- Asymmetric multiprocessing
- only one processor (the master server) has all scheduling decision, I/O processing
- Symmetric multiprocessing
- each processor is self-scheduling
- Asymmetric multiprocessing
Chapter 6 Process synchronization
- Race condition
- The situation where several processes access and manipulate shared data concurrently.
- The final value of the shared data depends upon which process finishes last.
The Critical-Section Problem
critical section
- Each process has a code segment, called critical section, in which the shared data is accessed
- 有几个共享变量就有几个临界区
Criteria for the critical section problem solution
- Mutual exclusion 互斥
- progress 空闲让进
- Bounded waiting 有限等待
Peterson’s Solution
- 举手+令牌
hardware-based solution
- 关中断
- 多处理机不适合
- 原子操作
- 关中断
Semaphores
A Semaphore S – integer variable
- may be initialized via a non-negative value
- Can only be accessed via two indivisible (atomic) operations: P() and V()
P(): the wait() operation
wait (S) { while (S.value <= 0) ; // no-op S.value--; }
V() The signal() operation
signal(S){ S.value++; }
main problem : busy waiting (spinlock)
- advantages
- No context switch is required when a process must wait on a lock
- If locks are expected to be held for short times, the spinlocks are useful
- disadvantages
- wastes the CPU cycles that can be used by other processes productively
- 解决:modify the definition of the wait() and signal() 适用于 multiprocessor system
- Wait(): the process can block() itself rather than engaging in busy waiting
- Signal(): change the blocking process from the waiting state to the ready state
- advantages
implementation
- In a single-processor environment
- Disable interrupt
- In a multi-processor environment
- Critical section can be applied
Chapter 7 Deadlocks
- Necessary conditions
- Mutual exclusion
- Hold and wait
- No preemption
- Circular wait
Methods for Handling Deadlocks
Prevention
- Provides a set of methods for ensuring that at least one of necessary conditions cannot be held
- 针对条件2: all or nothing; 没有资源的时候才去申请
- 针对条件3: 谦让; 抢夺
- 针对条件4: 顺序执行
- 缺点: low device utilization and reduce system throughput.
Avoidance
- using the addition information,decide whether the current request can be satisfied or must be delay
- 方法:
- Resource-allocation graph
- 有环:处于unsafe state;可能处于死锁状态
- Banker’s algorithm
- Resource-allocation graph
Detection and recovery
- 方法:
- Wait-for graph
- not appilcable to a resource-allocation system with multiple instances of each resource type
- Banker’s Algorithm
- Wait-for graph
- When, and how often, to invoke detection algorithm. it depends on:
- How often a deadlock is likely to occur?
- How many processes will need to be rolled back?
- 方法:
Ignorance
Chapter 8
Address may be represented in
- symbolic address
- re-locatable address
- absolute address
address binding
- 转换:
- symbolic address -> re-locatable address : compiler
- re-locatable address -> absolute address : linkage editor or loader
- 发生的时期
- Compile time
- If memory location known at compile time, absolute code can be generated
- If memory location is not known at compile time, Must generate re-locatable code
- Load time (+linkage time)
- if memory location is known at load time, absolute code can be generated at this time
- Execution time
- If memory location is not known at compile time and load time, Binding is delayed until run time
- absolute code must be generated at run time
- Compile time
- 转换:
Logical vs. Physical Address Space
Logical address :CPU
- also referred to as virtual address
- 重定位地址和逻辑地址没有直接关系
Physical address :memory unit
logical (virtual) and physical addresses differ in execution-time address-binding scheme
- re-locatable code are seen by CPU
- absolute code are seen by the memory unit
Memory-Management Unit (MMU)
- Hardware device that maps virtual address to physical address in the run-time
Dynamic load
- not loaded the entire program and data of a process be in physical memory for the process to execute until it is called
- 好处:
- Better memory-space utilization
- No special support is required from the operating system
Dynamic linking
- Linking is postponed until execution time
- requires help from the OS
Contiguous Memory Allocation
- Fixed-Sized Contiguous Partition
- Strengths (advantages)
- Simple to implement
- little overhead
- Weaknesses(drawbacks)
- internal fragmentation
- allocated memory may be larger than requested memory
- fixed number of processes
- internal fragmentation
- Strengths (advantages)
- Dynamic Contiguous partition(可变分区)
- Hole – block of available memory
- Allocation algorithms
- first fit:
- 从头开始,或者是从当前位置开始
- best-fit
- Need to search all entire list, unless the list is ordered by size
- produces the smallest leftover hole that may be wasted
- worst-fit
- Need to search all entire list, unless the list is ordered by size
- 小进程多的 效果好
- first fit:
- 问题:
- External Fragmentation
Solutions to fragmentation
- Compaction(紧凑)
- To reduce external fragmentation
- Shuffle memory contents to place all free memory together in one large block
- It is done at execution time, it’s possible only if relocation is dynamic
- May be expensive in moving the processes and the holes
- paging
- segmentation
- Compaction(紧凑)
Disadvantage of Contiguous Memory Allocation
- Fragmentation in main memory
- Compaction is impossible on the disk
paging
frame: Divide physical memory into fixed-sized blocks
page : Divide logical memory into fixed-sized blocks
- page size is equal to frame size
- Finding n free frames for loading a program of size n pages
Translating logical address to physical address If the address space is 2^m and the page size is 2^n
- Every logical address generated by CPU is divided into:
- Page number (p: 页号)
- used as an index into a page table
- 页表中包含每一页在physical memory 的 base address (f:块号)
- p =address/2^n is equal to m-n bit of the address
- Page offset (d: 偏移)
- combined with base address (f:块号) to define the physical memory address that is sent to the memory unit
- d =address%2n is equal to n bit of the address
- Page number (p: 页号)
- Physical address
- frame number(f: 帧号、块号)
- page offset (d:页偏移、块偏移)
- Every logical address generated by CPU is divided into:
page size的选择
- 越大:
- Disk I/O is more efficient
- page table size 越小
- 越小:
- internal fragmentation 越小
- 越大:
Frame table (主存分块表)
- Has one entry for each physical page frame
Indicating
- whether the frame is free or it is allocated to which process
- Has one entry for each physical page frame
Indicating
page table
- each process must maintain a copy of the page table
- 计算
- if page-table entry is 4 bytes long
- Can point to one of 2^32 physical page frames (1比特=8字节)
- If frame size(= page size) is 4KB, the system can address 2^44 bytes(2^32×2^12=16TB) of physical memory
- 对于32位cpu
- page size: 4k (=2^12)
- Table size:2^32/2^12=1M
- each entry’s size : 4 bytes
- page table 的大小为: 4 MB
- if page-table entry is 4 bytes long
- 位置
- 直接存放在寄存器中:
- Efficient and expensive
- 当page table is reasonable small 时可以
- 存放在main memory ,然后用Page-table base register (PTBR:页表基址寄存器)存放其位置
- 进程切换时,加载页表只需要改变PTBR
- every data/instruction access requires two memory accesses
- One for the page table
- One for the data/instruction
- Translation Look-aside Buffer (TLB) also called Associate Memory(联想寄存器)
- 并行查找
- contains only a few of page-table entries
- 直接存放在寄存器中:
Structure of the Page Table
- 问题: The page table can be excessively large
- Solution: Divide the page table into smaller pieces
- Hierarchical Paging (分层页表)
- Hashed Page Tables(哈希页表)
- Inverted Page Tables(反置页表)
- Hierarchical Paging (分层页表)
- 缺陷:
- 需遍历,进程太多
- 可能有共享,而进程号只能填一个
- 不适用于64位
- 好:
- 需要的空间小
- Hashed Page Tables
- hash table -> 在链表中遍历匹配
- Inverted Page Table(反置页表/主存分块表)
- Only a page table in the system
- One entry for each real page (or physical frame) of memory
- 缺点:
- increases time needed to search the table when a page reference occurs
- Lead to memory share difficulty
segmentation
- User’s View of a Program: A program is a collection of segments,a segment is a logical unit such as:
- main program
- procedure ,function,method,object
- local variables, global variables
- common block
- stack
- symbol table
- arrays
- main program
Chapter 9 Virtual Memory
the entire program is not needed to be in physical memory.这样的好处有:
- 程序的大小不再受内存所限制
- 更多程序可以同时运行
- Less I/O would be needed to load or swap each user program into memory, so program would start to run faster
Virtual memory management
- a term used to describe a technique whereby the computer appears to have much more memory than it actually does
Virtual memory can be implemented via:
- Demand paging
- Demand segmentation
Demand paging
思想: Bring a page into memory only when it is needed
- Be similar to a paging system with swapping
Hardware
- Page table. 需要加一位valid–invalid bit
- v -》 The page is legal and in memory
- Secondary memory
- A high-speed disk, Swap space
- Hold those page that are not present in memory
- Page table. 需要加一位valid–invalid bit
Page Fault
- Access to a page marked invalid causes a page-fault trap
- handle
- Operating system looks at another table (PCB) to decide:
- Invalid reference -> abort
- Just not in memory (go on to 2))
- Get empty frame
- Swap the desired page into the frame
- modify the page table, Set validation bit = v
- Restart the instruction that caused the page fault
- Operating system looks at another table (PCB) to decide:
- 特殊:
- 一条指令可产生多个缺页中断
- 指令复执
- 在指令执行时中断。
- 对比普通中断:
- 一条指令在执行完后,检查是否有中断请求
- 有:执行中断
- 无:执行下一条指令
- 一条指令在执行完后,检查是否有中断请求
Page Replacement
替换算法
FIFO page Replacement
- Belady’s Anomaly : more frames -> more page faults
Optimal Page Replacement (OPT)
- 替换最晚才用的页 或 后面最长时间用不到的页
Least Recently Used (LRU) Algorithm
- 思想: The recent past as an approximation of the near future
- 实现:
- counters
- stack
LRU Approximation Algorithms
- Additional-reference-bits algorithm
- Second chance (clock)
- Enhanced second-chance algorithm
Counting-Based Page Replacement
- Least Frequently used
- Most Frequently used
Page-Buffering Algorithm
- Assistant procedure to a page-replacement algorithm
Allocation of Frames
Two major allocation schemes
- fixed allocation
- Equal allocation
- Proportional allocation
- priority allocation
- Use a proportional allocation scheme using priorities rather than size
- fixed allocation
Global vs. Local Allocation
- Local replacement
- To allow a process to select from only its own set of allocated frames.
- Cannot increase the number of frames allocated
- Not affected by external circumstances
- Global replacement
- To allow a process to select a replacement frame from the set of all frames, even if that frame is currently allocated to some other process
- Can increase the number of frames allocated
- Cannot control its page-fault rate.
- In general, global replacement is better.
- Local replacement
Thrashing
- A process is thrashing (颠簸)if it is spending more time paging than executing
- approach
- Using a local replacement algorithm
- Working-set strategy
- To compute the working-set size for each process in the system
- Page-Fault Frequency (PFF) Scheme (水多了加面,面多了加水)
- If actual rate too low, remove a frame from the process
- If actual rate too high, allocate another frame to the process
- If no frames are free, suspend it
Other Considerations
- page size 大小的选择要考虑到:
- 内碎片
- 页表的大小
- I/O overhead (seek time, latency time, transfer time)
- Locality
- Page fault rate
- 顺序访问: page size越大,则缺页中断率越小
- 随机访问: page size越大,则more paging action could ensue because fewer pages can be kept in memory and more data is transferred per page fault.
- Install a faster hard disk, or multiple controllers with multiple hard disks
- for as the disk bottleneck is removed by faster response and more throughput to the disks, the CPU will get more data more quickly
Chapter 10 File-System Interface
- File
- A file is named collection of related information that is recorded on secondary storage
- Six basic operations
- create
- read/write/seek
- delete
- truncate: to erase the contents of a file but keep its attributes except for it’s length
- Assistant operations
- open(F):
- search the directory structure on disk for entry F
- copy the directory entry into the open-file table
- allocate a file descriptor
- close(F):
- copy the directory entry in the open-file table to the directory structure on disk
- free the file descriptor
- open(F):
Access Methods
- The information in the file can be accessed in
- sequentical access
- direct access
- other access
- involve the construction of an index for the file
Directory Structure
symbol table
- The directory can be viewed as a symbol table that translates file names into their directory entries
Criteria
- efficiency
- naming
- grouping
shemes
- Single-Level Directory
- Two-Level Directory
- Positive
- Efficient searching
- Negative
- No grouping capability
- Difficult to share file among different users
- Positive
- Tree-Structured Directories
- Positive
- Efficient searching
- Grouping Capability
- Negative
- Difficult to share file among different users
- Positive
- Acyclic-Graph Directories
- Tree-structured directory + shared subdirectories or files
- Created a new directory entry called a link to implement sharing
- The difficulty is to avoid cycles as new links are added
- General Graph Directory
- Add the links to an existing tree-structure directory
- Acyclic-Graph Directories更好
硬(hard)链接
ln /usr/local/python3 python
- 目录中仅存储指向文件数据的指针
- 允许一个文件被多个目录引用.
- 无法用来链接目录,也不能跨文件系统
- 通过
ls -i
查看是否为硬链接
软 (symbolic) 链接
- “快捷方式”
- 软链接也是一个文件
ln -s ../p24 p24
- 目录从“树”变为了“图”,还是有环图
ACL: access-control list
- Each file or directory has an ACL
File-System Implementation
- File system organized into layers
- application program
- logical file system
- FCB: file control blocks
- file-organizational module
- basic file system
- I/O control
- devices
Allocation Methods
An allocation method refers to how disk blocks are allocated for files
Contiguous allocation
- Each file occupies a set of contiguous blocks on the disk
- Supports both sequential access and direct access (Random access)
- 问题:
- External fragmentation
- Files cannot grow
Linked allocation
- Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk
- 优点
- 容易实现
- 无外碎片
- 文件增长方便
- 缺点:
- No random access
- Poor reliability
- 慢(链表是保存在磁盘上的,所以需要多次查询)
- 改进: File-allocation table (FAT)
- 把链表信息放到了一个单独的FAT表中,而不是各个数据块中,且进行备份
Indexed allocation
Bringing all the pointers together into one location: index block
Solutions to large files
- Linked sheme
- Link blocks of index table
- Multilevel index
- Combined scheme
- 一部分是 direct pointers ,一部分是multi-indirect block
- Linked sheme
Criteria
- storage utilization efficiency
- data block access time
- Contiguous allocation: Good for known-size file
- Linked allocation: Good for storage utilization
- Indexed allocation: Access time depends on index structure, file size, block position
Free-Space Management
- The free-space list 的实现
- Bit vector
- 优点
- Simple to implement
- Efficient to find the first free block
- 缺点
- Bit map requires extra space
- Inefficient unless the entire vector is kept in main memory
- 优点
- Linked Lists (free list)
- 优点
- No waste of space
- 缺点
- Inefficient when traversing the list
- 优点
- Grouping
- The first free block store the addresses of n free blocks
- Easier to find a large number of free blocks
- Bit vector
Mass-Storage Systems
Magnetic disk’s structure
- Disk platter
- track
- sector
- each track is subdivided into several sectors
- cylinder
- is the set of tracks that are at one arm position
CLV vs. CAV
- ClV : constant linear velocity
- CD-ROM, DVD-ROM
- Tracks in the outermost zone hold more sectors
- CAV : constant angular velocity
- Magnetic disk
- The density of bits decreases from inner tracks to outer tracks to keep the data rate constant
- ClV : constant linear velocity
Disk Scheduling
- Access time
- Seek time is the time for the disk are to move the heads to the cylinder containing the desired sector
- Seek time seek distance
- Rotational latency
- waiting for the disk to rotate the desired sector to the disk head
- Seek time is the time for the disk are to move the heads to the cylinder containing the desired sector
- Disk bandwidth
- The total number of bytes transferred / the total time between the first request for service and the completion of the last transfer
- FCFS Scheduling
- SSTF:Shortest-seek-time-first (SSTF)
- 最短寻道时间优先
- 问题:
- 往返跑—距离很短,但速度不一定很快
- may cause starvation of some requests
- SCAN
- Sometimes called the elevator algorithm
- C-SCAN (Circular SCAN)
- The head moves from one end of the disk to the other, servicing requests as it goes
- When it reaches the other end, however, it immediately returns to the beginning of the disk, without servicing any requests on the return trip
- 回途不载客
- LOOK / C-LOOK
Similar to SCAN/C-SCAN
Arm only goes as far as the last request in each direction, then reverses direction immediately, without first going all the way to the end of the disk.
选择 Performance depends on the number and types of requests
- SCAN and C-SCAN perform better for systems that place a heavy load on the disk
- Either SSTF or LOOK is a reasonable choice for the default algorithm
Disk Management
- Disk formatting
- Low-Level Formatting (physical formatting )
- Dividing a disk into sectors that the disk controller can read and write
- logical Formatting
- Creation of a file system
- Build the metadata structures for a file system
- Low-Level Formatting (physical formatting )
RAID Structure
Redundant Array of Inexpensive Disks (past)
Redundant Array of Independent Disks (now)
- Used for their higher reliability and higher data-transfer rate(performance)
levels
- RAID 0
- Disk arrays with data striping at the level of blocks but without any redundancy
- RAID 1
- Disk mirroring
- RAID 2
- Bit-level striping or Byte-level striping
- Memory-style error-correcting-code (ECC)
- RAID 3
- Bit-interleaved parity
- RAID 4
- Block-interleaved parity organization
- RAID 5
- Block-interleaved distributed parity
- RAID 0
常用单词
- simultaneously : 同时地
- idle : 空闲,懒
- reside : 位于,居住
- uni-processor : 单处理器
- interleave: 交织
- allocation : 分配
- dashed line : 虚线
- minuscule : 微小的
- concrete : 具体的
- mandatory: 强制的
- mediate : 调解
- strip : 脱掉;条