操作系统(Operating System,OS)是指控制和管理整个计算机系统的硬件和软件资源,并合理地组织调度计算机的工作和资源的分配;以提供给用户和其他软件方便的接口和环境;它是计算机系统中最基本的系统软件。
OS roles
1.系统资源管理者Resource manager
Track and record resource usage
Determine resource allocation strategy------algorithm
Allocate and recover resources
Improve resource utilization
Coordinate conflicts between multiple processes for resource requests
five basic functions
1.Process/Thread management (CPU management)
2.Memory management
3.File management
4.I/O device management
5.User interface
2.向上层提供服务Provide users with various services(services of OS)
给用户使用的:GUI、命令接口(联机命令接口、脱机命令接口)
给软件使用的:程序接口
Typical services
1.流程创建和执行process creation and execution
2.file and directory operation
3.I/O device handling
4.various statistics
3.接近硬件Extensions to hardware
需要实现对硬件机器的拓展
没有任何软件支持的计算机成为裸机。在裸机上安装的操作系统,可以提供资源管理功能和方便用户的服务功能,将裸机改造成功能更强、使用更方便的机器
通常把覆盖了软件的机器成为扩充机器,又称之为虚拟机
OS establishes an equivalent extension machine between applications and hardware. (virtual machine)
abstract hardware and improve portability; program more easily than hardware
Main characteristics of OS
并发concurrency
并发是指两个任务可以在重叠的时间段内启动、运行和完成。concurrency is when two tasks can start, run, and complete in overlapping time periods.
并行是指任务实际上同时运行,例如在多核处理器上。parallelism is when tasks literally run at the same time, e.g. on a multi-core processor.
sharing
OS shares the limited system resources with multi-users' programs OS must allocate and use the system resources reasonably. Resources are alternately used by multiple processes in a time period sharing type exclusive sharing (i.e. printer) inclusive sharing (i.e. file)
虚拟化virtualization
虚拟机(virtual machine)是在操作系统中模拟硬件设备,然后运行另一个操作系统,
Virtualization A physical entity is mapped to several corresponding logical entities time-sharing or space-sharing Virtualization is an important means for OS to manage system resources. High resource utilization CPU----virtual processor of each process Memory---each process has a separate virtual address space(code+data+stack) Display device-multi-window or virtual terminal
asynchronization (异步)(random)
Asynchronous means that you can execute multiple things at a time and you don't have to finish executing the current thing in order to move on to next one.
Random: OS must respond to the events which occur in an unpredictable order. Reason: unpredicatable process speed------multiple processes execute concurrently difficult to reproduce the state of the system (including running error)
synchronization(同步) Synchronous basically means that you can only execute one thing at a time
Classification of OS
Batch operating system Time-sharing operating system Real-time operating system Network operating system Distributed operating system Embedded operating system
Central Processing Unit (CPU) mode
CPU generally composes of control unit(CU), arithmethic-logic unit(ALU), a series of registers and cache. Types of registers General purpose registers
Special purpose registers
Program Counter (PC):It contains the memory address of the next instruction to be fetched它包含要获取的下一条指令的内存地址 Instruction Register (IR):It holds the instruction which is currently been executed. Flag register: It is a collection of status flag bits for a processor Program Status Word(PSW): It is an IBM System/360 architecture and successors. It refers as a flag register. It keeps track of the current state of the system, such as, mode Memory Address Register (MAR):holds the memory location of data that needs to be accessed Memory Buffer Register (MBR): also known as memory data register (MDR)stores the data being transferred to and from the immediate access storage也称为内存数据寄存器 (MDR),用于存储传入和传出即时访问存储的数据
Two CPU modes in OS (dual mode)
Kernel mode(Privileged mode):Run operating system programs
Privileged instructions that can only be used by OS and cannot be used by the user (in the kernel mode)
User mode (Non privileged mode):Run user program
Non-privileged instructions that user program can use
Switching between CPU modes
Interrupt /Exception handling
It is one of the core mechanisms of modern computer systems. In order for a computer to effectively manipulate interrupt/exception, its hardware and software must work together.
Interrupt / exception mechanism is one of the core mechanisms of modern computer systems. Hardware and software work together to make the computer system fully Exert ability.
Interrupt response
There is an Interrupt control register (ICR) in processor The process of discovering interrupts and receiving interrupts, implemented completed by interrupt hardware components
Interrupt Vector Table
Interrupt vectors are addresses that inform the interrupt handler as to where to find the ISR (interrupt service routine, also called interrupt service procedure). All interrupts are assigned a number from 0 to 255, with each of these interrupts being associated with a specific interrupt vector.
Interrupt Handler
While designing OS, it programs a corresponding handler for each type of interrupt / exception event, and sets the interrupt vector table. If the system responds to an interrupt while the system is running, the interrupt hardware component transfers CPU control to the interrupt handler
Save related registers' information Analyze the specific cause of the interruption / exception Perform the corresponding interrupt handler Restore the context and return to the program interrupted by the event
system call
OS service that users can call while programming
Role of system calls
System calls are the only interface provided by OS to programmers Make the CPU mode from the user mode into the kernel mode
Design of system call
Select a special instruction: trap instruction
System call number and parameters
System call table
Parameter passing process
Parameters by trap instruction Passing parameters through general purpose registers Create a dedicated stack area in memory to pass parameters
System call execution
When a call is made in user mode that starts a system call, CPU executes a special trap instruction, which switches CPU mode from user to kernel . System call routine Save the context; save the parameters in the kernel stack; transfer control to the corresponding system call routine by checking the system call table Execute system call routines Restore the context and return to the user program
Process
A process is basically a program in execution It is the unit of system resource allocation and scheduling by OS Transform one CPU into multiple virtual CPUs System resources are allocated in units of processes, such as memory, files, ... Each process has an independent address space OS dispatches the CPU to the required processes Multiple processes can be related to the same program.
Process types
System process, user process Foreground process, background process I/O bound process, CPU bound process
Process hierarchy
UNIX: Process family tree, init as the root WINDOWS: Same status
multiprogramming
In a multiprogramming system there are one or more programs loaded in main memory which are ready to execute. Only one program at a time is able to get the CPU for executing its instructions (i.e., there is at most one process running on the system) while all the others are waiting their turn. The main idea of multiprogramming is to maximize the use of CPU time.
Concurrent environment
There are two or more programs begin to run, but not ended, at the same time period with a single CPU. The order is not predetermined
Process states and state transitons
In a multitasking computer system, processes may occupy a variety of states When a process executes, it passes through different states. These stages may differ in different OS, and the names of these states are also not standardized. Three states ready, running, waiting/blocked Five states new, ready, running, waiting/blocked, terminated Seven states new, ready, running, waiting/blocked, blocked suspend, ready suspend, terminated
PCB
A process handles the operating system activities through PCB (Process control Block) A PCB is a data structure used by OS to store all the information about a process. It is also known as a process descriptor. When a process is created (initialized or installed), the operating system creates a corresponding PCB. There is a one-to-one correspondence between the process and the PCB The process table is an array of PCBs, that means logically contains PCBs for all the current processes in the system.
Components of PCB
Naming the process State of the process Resources allocated to the process Memory allocated to the process Scheduling information Input / output devices associated with process
Prcess ID: In computer system there are various process running simultaneously and each process has its unique ID. Process State: It refers the states of the processes. Program counter: It is used to point to the address of the next instruction to be executed in any process Register Information: It is comprising with the various registers, such as index and stack that are associated with the process. Scheduling information: It is used to set the priority of different processes. Accounting information: It stores the details relate to central processing unit (CPU) utilization and execution time of a process. I/O information:It stores the details pertaining to resource utilization and file opened during the process execution Memory related information: It comprises of page and segment tables. It also stores the data contained in base and limit registers.
process scheduling
CPU switch
Context Switching involves storing the context or state of a process so that it can be reloaded when required and execution can be resumed from the same point as earlier. This is a feature of a multitasking operating system and allows a single CPU to be shared by multiple processes. Context Switching Triggers Multitasking(多任务处理): In a multitasking environment, a process is switched out of the CPU so another process can be run. Interrupt Handling User and Kernel Mode Switching
context switching
When a process is running: 1.its hardware state is stored in registers on the CPU. 2.Registers 2.1.program counter, PSW, stack pointer, values of general-purpose registers and other control registers 3.When the process is not running, the values of these registers are stored in PCB; 4.when the operating system wants to run a new process, the relevant values in the PCB are sent to the corresponding registers
process queue
OS creates one or more queues for each type of process Element of queue is PCB With the state transitions, its PCB goes from one queue to another
Events in multiple waiting queues are different The ready queue can also be multiple Only one process in the running queue with single CPU
process control
Process management functions are usually placed in the OS kernel The OS kernel contains two aspects: 1.Supporting functions:interrupt processing, clock management, primitive operation 2.Resource management functions:process management, memory management, device management
ps:A primitive is the smallest 'unit of processing' available to a programmer of a given machine, or can be an atomic element of an expression in a language. Primitives are units with a meaning, i.e., a semantic value in the language.
process creation
process termination
process blocking
A process in the running state expects an event to occur during its running, such as waiting for the keyboard input, waiting for the disk data transfer completing, waiting for other processes to send messages. When the waited event does not occur, it is executed by the process itself Blocking primitives, changing themselves from running to blocking
Thread
concept
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set and a stack. It shares with other threads belong to the same process its code section, data section, and other OS resources, such as open files and signals. A thread is a path of execution within a process. It is also known as lightweight process. A process can contain multiple threads. The program starts as a single-threaded process The idea is to achieve parallelism by dividing a process into multiple threads. The primary difference is that threads within the same process run in a shared memory space, while processes run in separate memory spaces.
Advantages:
Responsiveness: If the process is divided into multiple threads, if one thread completes its execution, then its output can be immediately returned. Faster context switch: Context switch time between threads is lower compared to process context switch. Process context switching requires more overhead from the CPU. Effective utilization of multiprocessor system: If we have multiple threads in a single process, then we can schedule multiple threads on multiple processor. This will make process execution faster.
Application needs Cost considerations
Performance considerations
implementation
user level thread
Create a thread library in user space:Provide a set of management Run time system: completion of thread management (operation,thread table) Process manages the kernel, and does not know the existence of the threads Thread switching does not need the kernel privileges. Example: UNIX
kernel level thread
The kernel manages all threads, and provide API to the application The kernel maintains the context of processes and threads Thread switching requires kernel support Schedule based on thread Example: Windows
hybrid model
Thread creation is done in user space Thread scheduling is completed in kernel mode Example: Solaris
CPU scheduling
criteria
CPU utilization---high is good; the system works best when the CPU is kept as busy as possible Throughput -- the number of processes that complete their execution per time unit. Turnaround time -- amount of time to execute a particular process Waiting time --- amount of time a process has been waiting in the ready queue Response time ---amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing systme)
It makes sense to look at averages of these matrices
Scheduler types
Long term scheduler (high-level scheduler, Job scheduler )
Job Less frequently (every few minutes) Tasks: 1.It determines which programs are admitted to the system for processing确定哪些进程被允许进入系统进行处理 2.It selects processes from the job queue and loads them into memory for execution.It 从作业队列中选择进程,并将其加载到内存中执行。 3.Process loads into the memory for CPU scheduling进程加载到内存中用于CPU调度 Primary aim of the Job Scheduler is to maintain a good degree of Multiprogramming作业调度进程的主要目的是保持良好的进程并发水平
Short term scheduler (low-level scheduler, CPU scheduler)
Process in memory Runs very frequently (every 10-100 ms) Tasks:decides which of the ready, in-memory processes is to be executed (allocated a CPU)决定哪个准备好了, 将执行内存中进程(分配一个 CPU)
Medium term scheduler (Swapping)
It removes the processes from the memory and later swaps in.它从内存中删除进程,稍后交换。 It reduces the degree of multiprogramming.它降低了进程并发的程度。 It is in-charge of handling the swapped out-processes.它负责处理换出的流程。
goals of scheduling
CPU scheduler It selects a process among the processes that are ready to execute and allocates CPU to one of them Known as dispatchers, make the decision of which process to execute next. If there is no process in the ready queue, the system will schedule an idle process Scene N process in ready queue, waiting for CPU M CPU, M>= 1 (multicore system) Decide: assign which CPU to which process
procedure of CPU scheduler
1.Preserve the processor information 2.Choose a process in the ready queue according to an scheduling algorithm 3.Assign the CPU to the process
Parts of the scheduler Enqueuer It adds a pointer or reference to the process’ PCB, which is usually a collection of linked lists.它添加了一个指向进程PCB的指针或引用,PCB通常是链表的集合。 Dispatcher The part that implements the scheduling algorithm to pick the next process to run实现调度算法以选取要运行的下一个进程的部分 Context Switcher Loads the selected process onto the CPU as the running process.将所选进程作为正在运行的进程加载到 CPU 上。
Three issues that need to be addressed in CPU scheduling What: On what principles to choose the next process to be executed Scheduling algorithm When: When to schedule Scheduling circumtances How: How to make the selected process run on the CPU Scheduling process (context switching of processes)
CPU scheduling circumstances
CPU scheduling decisions may take place under the following four circumstances: (preemptive, non-preemptive ) When a process switches from the running state to the waiting state(for I/O request or invocation of wait for the termination of one of the child processes). ( A new process must be selected) When a process switches from the running state to the ready state (for example, when an interrupt occurs). (To either continue running the current process, or select a different one.) When a process switches from the waiting state to the ready state(for example, completion of I/O or a return from wait()). (To either continue running the current process, or select a different one.) When a process terminates. ( A new process must be selected)
Scheduling---Process switch
Process switch refers to the procedure in which one process gives up the processor and another process takes up the processors Two tasks Switch global page directory to load a new address space切换全局页目录以加载新的地址空间 Switch kernel stack and hardware context, which contains all the information the kernel needs to execute a new process, such as CPU related registers切换内核堆栈和硬件上下文,其中包含内核执行新进程所需的所有信息,例如与 CPU 相关的寄存器
Process context switch
Scene: process A is taken off and process B takes up CPU Save the context of process A (program counter, Program Status Word, other registers ...) Update process A's PCB with new status and other relevant information Move process A to an appropriate queue (ready, blocked ...) Set the state of process B to running Restore context from the PCB of process B (program counter, Program Status Word, other registers ...)
Cost of process context switch
Direct cost : CPU time used by the kernel to complete the switch Save and restore registers ... Switching address space (relevant instructions are more expensive) Indirect cost: Invalidate Cache, Buffer Cache and TLB(Translation Look-aside Buffer)
Scheduling algorithms
Process priorities
Priority level & Priority number ◉ Static & dynamic ◉ Static priority (fixed priority) 1.It is allocated during creation without changing during runtime.
◉ Dynamic priority 1.It is allocated during creation while it can be changed dynamically.
2.For example: a process with a long waiting time can increa
Preemptive and non-preemptive schedulin
Preemptive scheduling: Preemptive scheduling allows a running process to be interrupted by a high priority process Non-preemptive scheduling:Any new process has to wait until the running process finishes its C
I/O bound and CPU bound processes
I/O bound-----In computer science, I/O bound refers to a condition in which the time it takes to complete a computation is determined principally by the period spent waiting for input/output operations to be completed.
◉ CPU bound----When the time for it to complete a task is determined principally by the speed of the CPU .
Time slice
Time slice, time quantum or quantum ◉ The period of time for which a process is allowed to run is generally called the time slice or quantum.
◉ The scheduler is run once every time slice to choose the next process to run How to choose a time slice size:
1.Process switching cost 2.response time requirement 3.number of ready processes 4.CPU capability
FCFS (First Come First Serve)
First In First Out (FIFO) It simply queues processes in the order that they arrive in the ready queue.
Non- preemptive Pros ◉ fairness----Every process will get a chance to run, so starvation doesn't occur.
◉ pretty simple and easy to implement Cons ◉ If a process executes for a long time, the processes in the back of the queue will have to wait for a long time before they get a chance to be execute
SJF (Shorted Job First) and SRTF(Shorted Remaining Time First)
Shortest job first (SJF) or Shortest Job Next(SJN) It is a scheduling policy that selects the waiting process with the smallest execution time to execute next. Preemptive:Jobs are put into the ready queue as they come. Although a process with short burst time begins, the current process is removed or preempted from execution, and the job which is shorter is executed first.
Userally used in long-term scheduling Shortest Remaining Time First (SRTF): The processor is allocated to the job closest to completion.
SJF
Pros Maximizes “task throughput” : By running tasks which take less time to complete first, we can complete more tasks in a given amount of time Cons Unfair to larger tasks:
- – Larger tasks do not get an opportunity to run if smaller tasks keep entering the system 2.starvation(饿死现象) Computers are not psychic:It is not feasible to accurately predict how long a task’s CPU burst-time could be.
HPF (Highest Priority First)
Highest Priority First ----HPF ◉ Select the highest priority process to run ◉ In general: 1.system process has higher priority than user process 2. The foreground process has higher priority than the background process ◉ Preemptive and non-preemptive ◉ Priority can be static or dynamic.
◉ Process priority basis-----Process type, resource requirements, user requirements ◉ Ready queues can be organized by priority ◉ Pros----Easy to implement ◉ Cons-----Not fair, starvatio
Dynamic priority level(动态优先级别): The priorities are calculated during the execution of the system, i.e.
◉ The longer waiting time, the higher priority ◉ In preemptive scheduling, the longer running time, the lower priority, which prevent the long-term jobs to take up CPU for a
HRRN (Highest Response Ratio Next)
the scheduling is done on the basis of an extra parameter called Response Ratio. 其中调度是根据称为响应比率的额外参数完成的。
◉ A Response Ratio is calculated for each of the available jobs and the Job with the highest response ratio is given priority over the others.
◉ Non-preemptive ◉ It was developed as an improvement of SJF ◉ Response Ratio is calculated by the given formula
RR (Round Robin) in time sharing system
Run process for one time slice, then move to back of queue. Each process gets equal share of the CPU. 运行一个时间片的进程,然后移动到队列的后面。每个进程获得相等的 CPU 份额。
Aim: Improve average response time for short tasks 缩短短任务的平均响应时间
Ideas :1.Periodic switching 2. Each process is assigned a time slice 3. Clock interrupt1.周期性切换 2.每个进程分配一个时间片 3。时钟中断
◉ Pros Fair to small tasks Facilitates interactive calculation and fast response time ◉ Cons: Context-Switch Overhead No gain for tasks with equal run time:If we have ten tasks in the system which take ten seconds to run, then we will not have a single completed task until 100 seconds have passed. In the FIFO case, we would have our first task complete in 10 seconds, the second task complete in 20 seconds, etc. As a result, the “task throughput” is potentially lower如果我们系统中有十个任务需要十秒钟才能运行,那幺在 100 秒过去之前,我们不会有一个完成的任务。在 FIFO 案例中,我们将在 10 秒内完成第一个任务,在 20 秒内完成第二个任务,依此类推。因此,“任务吞吐量”可能较低
Multi-level queue scheduling algorithm多级队列调度算法
It partitions the ready queue into several separate queues. 它将就绪队列分区为几个单独的队列。
Pros ◉ Flexible implementation ◉ Enable short CPU-bound jobs to be processed quickly Cons ◉ Queues require monitoring, which a costly activity
The processes are permanently assigned to one queue, generally based on some property of the process, such as memory size, process priority, or process type. 进程永久分配给一个队列,通常基于进程的某些属性,如内存大小、进程优先级或进程类型。
Each queue has its own scheduling algorithm. 每个队列都有自己的调度算法。
Preemptive or non-preemptive For example: 1.A common division is made between foreground(or interactive) processes and background (or batch) processes.
- Foreground queue----- Round Robin; Background queue ---- FCF
Let us consider an example of a multilevel queue-scheduling algorithm with five queues:System Processes, Interactive Processes, Interactive Editing Processes, Batch Processes, Student Processes Each queue has absolute priority over lower-priority queues.
No process in the batch queue, for example, could run unless the queues for system processes, interactive processes, and interactive editing processes were all empty.
If an interactive editing process entered the ready queue while a batch process was running, the batch process will be preempted.
1.前台(或交互式)进程和后台(或批处理)进程之间有一个共同的划分。
2.前台队列-----循环赛;---- FCF 的后台队列
让我们考虑一个具有五个队列的多级队列调度算法的示例:系统进程、交互式进程、交互式编辑进程、批处理进程、学生进程 每个队列都比低优先级队列具有绝对优先级。
例如,批处理队列中的任何进程都无法运行,除非系统进程、交互式进程和交互式编辑进程的队列全部为空。
如果交互式编辑进程在批处理运行时进入就绪队列,则该批处理将被抢占。
Multi-level feedback scheduling algorithm多级反馈调度算法
In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on entry to the system. Processes do not move between queues. This setup has the advantage of low scheduling overhead, but the disadvantage of being inflexible.
Multilevel feedback queue scheduling, however, allows a process to move between queues.
If a process uses too much CPU time, it will be moved to a lower-priority queue. Similarly, a process that waits too long in a lower-priority queue may be moved to a higher-priority queue. This form of aging prevents starvat
In general, a multilevel feedback queue scheduler is defined by the following parameters: ◉ The number of queues.
◉ The scheduling algorithm for each queue.
◉ The method used to determine when to upgrade a process to a higher-priority queue.
◉ The method used to determine when to demote a process to a lower-priority queue.
◉ The method used to determine which queue a process will enter when that process needs service.
Result approximates SRTF ◉ CPU bound jobs rapidly to lower queues ◉ Short-running I/O bound jobs stay near the top Scheduling must be done between the queues ◉ Fixed priority scheduling: serve all from the highest priority, then the next priority, etc.
◉ Time slice: each queue gets a certain amout of CPU time (e.g.. 70% to the highest, 20% next, 10% lowest)
Process Synchronization
Concurrent execution of processes
concurrency(并发) is the foundation of all problemsand operating system design.并发是所有问题和操作系统设计的基础。
Process precedence graph
◉ Concurrent access to shared data may result in data inconsistency并发访问共享数据可能会导致数据不一致 ◉ Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes维护数据一致性需要机制来保证有序 执行合作流程 ◉ A precedence graph is a directed acyclic graph whose nodes correspond to some processes, which is used in the context of concurrent control优先图是有向无环图,其节点对应于 一些进程,用于并发控制的上下文
Mutual exclusion--indirect constrain
Suppose the producer-consumer problem (生产者-消费者问题/缓存区问题,also known as bounded-bufferproblem) ○ It is a classic example of a multi-process synchronization problem.
○ The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue
three variables
-
integer count ---- keep track of the number of full buffers ◉ Initially, count is set to 0 ◉ after producer produces a new buffer, count ++ ◉ after consumer consumes a buffer, count - -
-
integer in ----index of next available buffer of producer ◉ in = 0, initial value 3.integer out---- index of next available buffer of consumer ◉ out = 0, initial
Race Condition
◉ A situation, where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place, is called a race condition. 多个进程同时访问和操作相同的数据,并且执行的结果取决于访问发生的特定顺序,这种情况称为争用条件。
◉ To guard against it, we need to ensure that only one process at a time can be manipulating the variable count. 为了防止它,我们需要确保一次只有一个进程可以操作变量计数。
◉ We require that the processes be synchronized in some way.我们要求以某种方式同步这些过程。
Critical resource and critical section
◉ Critical resource(关键资源): A resource that can only be in use by at most one process at any one time. 在任何时候最多只能由一个进程使用的资源。
◉ Critical section(关键区): It is a code segment that accesses shared variables and has to be executed as an atomic action. It means that in a group of cooperating processes, at a given point of time, only one process must be executing its critical section它是一个访问共享变量的代码段,必须作为原子操作执行。这意味着在一组协作流程中,在给定的时间点,只有一个流程必须执行其关键部分
Mutual exclusion(互斥)
◉ Consider a system consisting of n processes {P0, P1, ....., Pn-1}.
◉ Each process has a critical section, in which the process may be changing common variables, updating a table, writing a file, and so on.
◉ General structure of process Pi. (entry section, exit section, remainder section)
◉ A solution to the critical-section problem must satisfy the following four requirements ○ Mutual Exclusion (互斥)- If process Pi is executing in its critical section, then no other processes can be executing in their critical sections ○ Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely 如果在其关键部分没有进程正在执行,并且存在一些希望进入其关键部分的进程,则不能无限期推迟选择接下来将进入关键部分的进程 ○ Bounded Waiting(有限等待) - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted在进程发出进入其关键部分的请求之后和授予该请求之前,允许其他进程进入其关键部分的次数必须存在绑定 ○ Give-up waiting(让权等待) --When the process cannot enter its own critical section, the processor should be released immediately to avoid "busy waiting”
Solution to Critical-Section Problem
Peterson's algorithm
◉ A classic software-based solution to the critical-section problem ◉ Two process solution ◉ Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted.
◉ The two processes share two variables: ○ int turn; ○ Boolean flag[2] ◉ The variable turn indicates whose turn it is to enter the critical section.
◉ The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready
Process synchronization-----direct constraint
进程同步 -----直接约束
◉ It refers to the cooperation of multiple processes to complete a task in the system. There is an timing relationship which can be illustrated by a precedence graph.
◉ When a process runs to a certain point, it asks another process to provide a message. Then the process involves in blocking state before getting a message, and wakes up to transit to the ready state after getting a message
Semaphore
◉ It is an effective synchronization tool proposed by Dijkstra in 1965 ◉ Types ○ counting semaphore ○ semaphore record ○ semaphore with AND conditio
◉ Counting semaphore is defined as an integer that represents the number of resources.
◉ An non-negative integer value is used to pass information between processes
◉ To avoid the "busy waiting" of counting semaphores, it introduces semaphore record ◉ With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: ○ value (of type integer) ○ pointer to next record in the list ◉ Note: the value of semaphore can be negative. If it is a negative number, the absolute value of it represents the number of waiting processes
Semaphore with AND condition
◉ For multiple shared resources ◉ Basic idea ○ Atomic operations are performed on several critical resource allocations ○ All requested resources are allocated to the process, or no one is allocated ○ to avoid deadlock ◉ Add an AND condition to the
Monitor
◉ A high-level abstraction that provides a convenient and effective mechanism for process synchronization ◉ Only one process may be active within the monitor a
Condition Variables
◉ condition x, y; ◉ Two operations on a condition variable: ○ x.wait () – a process that invokes the operation is suspended.
○ x.signal () – resumes one of processes (if any) that invoked x.wait (
Bounded-buffer problem(有界缓冲区)
◉ The bounded-buffer problems is a classic example of concurrent access to a shared resource.
◉ A bounded buffer lets multiple producers and multiple consumers share a single buffer.
◉ Producers write data to the buffer and consumers read data from the buffer.
○ Producers must block if the buffer is full.
○ Consumers must block if the buffer is empty.N buffers, each can hold one item
◉ A binary semaphor(充当互斥信号量)e can be used to protect access to the critical sections.
○ All updates to the buffer state must be done in a critical section.
○ Initialize this semaphore to 1 ◉ Use one semaphore named empty to count the empty slots in the buffer.
○ Initialize this semaphore to N(总进程).
○ A producer must wait on this semaphore before writing to the buffer.
○ A consumer will signal this semaphore after reading from the buffer.
◉ Use one semaphore named full to count the number of data items in the buffer.
○ Initialize this semaphore to 0.
○ A consumer must wait on this semaphore before reading from the buffer.
○ A producer will signal this semaphore after writing to the buffer
Readers and Writers Problem
◉ The readers-writers problem relates to an object such as a file that is shared between multiple processes.
◉ A data set is shared among a number of concurrent processes ○ Readers – only read the data set; they do not perform any updates ○ Writers – can both read and write ◉ Requirements ○ Allow multiple readers to perform read operations simultaneously ○ Do not allow multiple writers to operate at the same time ○ Readers and writers are not allowed to operate at the same time
Reader first
◉ If a reader is going to read ○ The reader can read, if no readers and no writers ○ The reader can read, if some readers can read and a writer is waiting(Reader first) ○ The reader cannot read if a writer is writing ◉ If a writer is going to write ○ The writer can write, if no readers and no writers ○ The writer needs to wait, if some readers are reading ○ The writer needs to wait, if some writer is writing
◉ Variables ○ Semaphore mutex initialized to 1.
○ Semaphore w initialized to 1.
○ Integer rc initialized to 0
Deadlock and starvation
◉ Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes ◉ Let S and Q be two semaphores initialized to 1 P0 P1 S.wait ( ); Q.wait( ); Q.wait( ); S.wait ( ); ... ... S.signal ( ); Q.signal ( ); Q.signal ( ); S.signal ( ); ◉ Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended
Dining-Philosophers Problem
◉ Five philosophers; One plate of rice in the middle; One empty bowl before each philosopher; One chopstick between two philosophers
◉ Each philosopher: thinking, eating
◉ Each philosopher can eat with the left chopstick and the right chopsticks (mutual exclusion, no deadlock)
◉ Simulate the concurrency processes to share multiple resources
◉ Shared data
○ Semaphore chopstick = new Semaphore [5];
○ initialized to 1
◉ How to avoid the deadlock? ○ Allow 4 philosophers at most to sit around the table (with 5 chopsticks) ○ Only when a philosopher can use chopsticks on both sides, allow him to take chopsticks ○ Number all philosophers. Philosophers with odd numbers must first take the chopsticks on the left, and philosophers with even numbers will take the right one
The deadlock problem
◉ Deadlock is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process in the set.
◉ If a deadlock occurs, it will waste a lot of system resources and even cause the system to crash Ø All processes participating in the deadlock are waiting for resources Ø The processes participating in the deadlock are a subset of all processes in the current system
Why does deadlock occour
◉ Limited resources, incorrect use of locks(资源少) and semaphores(信号量错误) ◉ Types of computer resource ○ Reusable resource ■ used multiple times by multiple processes ■ Preemptible resources and non-preemptable resources ■ Processor, I / O component, memory, file, database, semaphore ○ Consumable resources ■ Resources that can only be used once, can be created and destroyed ■ Signals, interrupts, messages ◉ Reasons ○ Compete not-preemptive resources ○ Compete consumable resources ○ Improper process order
System Model
◉ Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices ◉ Each resource type Ri has Wi instances. ◉ Each process utilizes a resource as follows: ○ request ○ use ○ release
◉ Deadlock can arise if four conditions hold simultaneously.
○ Mutual exclusion: only one process at a time can use a resource.
○ Hold and wait(持久并等待): a process holding at least one resource is waiting to acquire additional resources held by other processes.
○ No preemption:(非抢占) a resource can be released only voluntarily by the process holding it, after that process has completed its task.
○ Circular wait: there exists a set {P0, P1, …, Pn} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0
Resource-Allocation Graph (RAG)
◉ A set of vertices V and a set of edges E ○ V is partitioned into two types: ■ P = {P1, P2, …, Pn}, the set consisting of all the processes in the system ■ R = {R1, R2, …, Rm}, the set consisting of all resource types in the system ○ request edge – directed edge Pi Rj ○ assignment edge – directed edge Rj Pi
◉ If graph contains no cycles = no deadlock.
◉ If graph contains a cycle = ○ if only one instance per resource type, then deadlock.
○ if several instances per resource type, possibility of deadlock
Methods for Handling Deadlocks
◉ Ensure that the system will never enter a deadlock state. ○ Deadlock prevention ■ Static strategy: design an appropriate resource allocation algorithm to prevent deadlock occur ○ Deadlock avoidance ■ Dynamic strategy: tracking and evaluating the resource allocation process, it decides whether to allocate ◉ Allow the system to enter a deadlock state and then recover.
◉ Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX
Deadlock Prevention
◉ Restrain the ways request can be made.
○ Mutual Exclusion – not required for sharable resources; must hold for nonsharable resources.
○ Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources.
■ Require process to request and be allocated all its resources before it begins execution, or allow process to request resources only when the process has none.
■ Low resource utilization; starvation possible
◉ No Preemption ○ If a process that is holding some resources requests another resource that cannot be immediately allocated to it, then all resources currently being held are released.
○ Preempted resources are added to the list of resources for which the process is waiting.
○ Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.
○ Suitable for resources whose state is easy to save and restore, such as main memory, CPU ◉ Circular Wait – impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration
Deadlock Avoidance
◉ Requires that the system has some additional a priori information available.
○ The deadlock-avoidance algorithm dynamically examines the resource-allocation state to ensure that there can never be a circular-wait condition ○ Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes.
○ Simplest and most useful model requires that each process declare the maximum number of resources of each type that it may need
Safe State
◉ When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state.
◉ System is in safe state ○ if there exists a sequence <P1 , P2 , …, Pn> of ALL the processes, such that for eachPi, the resources thatPi can still request can be satisfied by ■ currently available resources + resources held by all the Pj, with j < i.
◉ That is: ○ IfPi resource needs are not immediately available, then Pican wait until all Pj have finished.
○ When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate.
○ When Pi terminates,Pi+1 can obtain its needed resources, and so on
◉If a system is in safe state no deadlocks.
◉ If a system is in unsafe state possibility of deadlock.
◉ Avoidance ensure that a system will never enter an unsafe state
Resource-Allocation Graph Scheme
◉ Claim edge Pi Rj indicated that process Pj may request resource Rj; represented by a dashed line. ◉ Claim edge converts to request edge when a process requests a resource. ◉ Request edge converted to an assignment edge when the resource is allocated to the process. ◉ When a resource is released by a process, assignment edge reconverts to a claim edge. ◉ Resources must be claimed a priori in the system
Banker’s Algorithm
◉ Multiple instances.
◉ Each process must a priori claim maximum use.
◉ When a process requests a resource it may have to wait.
◉ When a process gets all its resources it must return them in a finite amount of time
◉ Let n = number of processes, and m = number of resources types.
◉ Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.
◉ Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.
◉ Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.
◉ Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of Rj to complete its task. Need [i,j] = Max[i,j] – Allocation [i,j]
Deadlock detection and recovery
◉ Deadlocks are allowed, but the OS will constantly monitor the progress of the system to determine whether the deadlock has actually occurred允许死锁,但操作系统将不断监视系统的进度,以确定死锁是否实际发生 ◉ Once a deadlock occurs, OS will remove the deadlock and resume OS operation at a minimal cost.
◉ When to detect ○ Detect deadlock when the process is waiting because the resource request is not satisfied ■ Disadvantages: heavy burden ○ Detect periodically ○ Detect deadlock when system resource utilization drops
Detection Algorithm
• Single instance of each resource type------use a variant of the resource-allocation graph, that is wait-for graph.
• Several instances of a resource type
Recovery from Deadlock: Process Termination
◉ Abort all deadlocked processes.
◉ Abort one process at a time until the deadlock cycle is eliminated.
◉ In which order should we choose to abort? ○ Priority of the process.
○ How long process has computed, and how much longer to completion.
○ Resources the process has used.
○ Resources process needs to complete.
○ How many processes will need to be terminated.
○ Is process interactive or batch?
Recovery from Deadlock: Resource Preemption
◉ Selecting a victim – minimize cost.
◉ Rollback – return to some safe state, restart process for that state.
◉ Starvation – same process may always be picked as victim, include number of rollback in cost factor
I/O Systems
I/O Hardware
◉ Incredible variety of I/O devices
◉ Common concepts
○ Port
○ Bus
○ Controller (host adapter)
◉ I/O instructions control devices
◉ Devices have addresses, used by
○ Direct I/O instructions
○ Memory-mapped I/O
Polling & Interrupt
◉ Determines state of device
○ command-ready
○ busy
○ Error
◉ Busy-wait cycle to wait for I/O from device
◉ CPU Interrupt-request line triggered by I/O device
Direct Memory Access
◉ Used to avoid programmed I/O for large data movement
◉ Requires DMA controller
◉ Bypasses CPU to transfer data directly between I/O device and memory绕过 CPU,直接在 I/O 设备和内存之间传输数据
Application I/O Interface
◉ I/O system calls encapsulate device behaviors in generic classesI/O 系统调用将设备行为封装在泛型类中
◉ Device-driver layer hides differences among I/O controllers from kernel设备驱动进程层隐藏了内核中 I/O 控制器之间的差异
◉ Devices vary in many dimensions
○ Character-stream or block
○ Sequential or random-access
○ Sharable or dedicated
○ Speed of operation
○ read-write, read only, or write only
Block and Character Devices
◉ Block devices include disk drives
○ Commands include read, write, seek
○ Raw I/O or file-system access
○ Memory-mapped file access possible
◉ Character devices include keyboards, mice, serial ports
○ Commands include get, put
○ Libraries layered on top allow line editing
Blocking and Nonblocking I/O阻塞和非阻塞 I/O
◉ Blocking - process suspended until I/O completed
○ Easy to use and understand
○ Insufficient for some needs满足某些需求不足
◉ Nonblocking - I/O call returns as much as available非阻塞 - I/O 调用返回尽可能多的可用值
○ User interface, data copy (buffered I/O)
○ Implemented via multi-threading
○ Returns quickly with count of bytes read or written
◉ Asynchronous- process runs while I/O executes异步 - 进程在 I/O 执行时运行
○ Difficult to use
○ I/O subsystem signals process when I/O completed
Kernel I/O Subsystem
◉ Scheduling
○ Some I/O request ordering via per-device queue
○ Some OSs try fairness
◉ Buffering - store data in memory while transferring between devices
○ To cope with device speed mismatch
○ To cope with device transfer size mismatch
○ To maintain “copy semantics“
Kernel I/O Subsystem
◉ Caching - fast memory holding copy of data
○Always just a copy
○Key to performance
◉ Spooling - SPOOL is an acronym for simultaneous peripheral operations on-line.
○ It is a kind of buffering mechanism or a process in which data is temporarily held to be used and executed by a device,program or the system.
○ Data is sent to and stored in memory or other volatile storage until the program or computer requests it for execution.
○ i.e.Printing
◉ Device reservation - provides exclusive access to a device
○System calls for allocation and deallocation
○ Watch out for deadlock
Disk Scheduling
◉ The operating system is responsible for using hardware efficiently — for
the disk drives, this means having a fast access time and disk bandwidth.
◉ Access time has two major components
○Seek time is the time for the disk are to move the heads to the cylinder containing the desired sector.
○ Rotational latency is the additional time waiting for the disk to rotate the desired sector to the disk head旋转延迟是等待磁盘将所需扇区旋转到磁盘头的额外时间
◉ Minimize seek time
◉ Seek time seek distance
◉ Disk bandwidth is the total number of bytes transferred, divided by the
total time between the first request for service and the completion of the
last transfer.
Disk Scheduling
◉ Several algorithms exist to schedule the servicing of disk I/O requests.有几种算法可以安排磁盘 I/O 请求的服务。
◉ We illustrate them with a request queue (0-199).
◉98, 183, 37, 122, 14, 124, 65, 67
Head pointer 53
Selecting a Disk-Scheduling Algorithm
◉ SSTF is common and has a natural appeal
◉ SCAN and C-SCAN perform better for systems that place a heavy load on the disk.
◉ Performance depends on the number and types of requests.
◉ Requests for disk service can be influenced by the file-allocation method.
◉ The disk-scheduling algorithm should be written as a separate module of the operating system, allowing it to be replaced with a different algorithm if necessary.
◉ Either SSTF or LOOK is a reasonable choice for the default algorithm.
File Interface
File Concept
◉ A file is a named collection of relate information that is recorded on secondary storage.文档是记录在辅助存储上的相关信息的命名集合。
File Attributes
◉ Name – only information kept in human-readable form
◉ Identifier – unique tag (number) identifies file within file system
◉ Type – needed for systems that support different types
◉ Location – pointer to file location on device
◉ Size – current file size
◉ Protection – controls who can do reading, writing, executing
◉ Time, date, and user identification – data for protection, security, and
usage monitoring
◉ Information about files are kept in the directory structure, which is
maintained on the disk
File Operations
◉ File is an abstract data type
◉ Create
◉ Write
◉ Read
◉ Reposition within file
◉ Delete
◉ Truncate: erase the contents but keep its attributes
◉ Open(Fi) – search the directory structure on disk for entry Fi, and move the content
of entry to memory
◉ Close (Fi) – move the content of entry Fi in memory to directory structure on disk
Open Files
◉ Several pieces of data are needed to manage open files:
○ File pointer: pointer to last read/write location, per process that has the file open
○ File-open count: counter of number of times a file is open – to allow removal of
data from open-file table when last processes closes it
○ Disk location of the file: cache of data access information
○ Access rights: per-process access mode information
Access Methods
◉ Sequential Access
○ Simplest
○ Information in the file is processed in order, one record after the record.
○ This mode of access is by far the most common;
○ for example, editor and compiler usually access the file in this fashion.
◉ Direct Access
○ known as relative access
○ For direct access, the file is viewed as a numbered sequence of block or record.
○ Thus, we may read block 14 then block 59 and then we can write block 17.
○ There is no restriction on the order of reading and writing for a direct access
file
Directory Structure
◉ A collection of nodes containing
information about all files
◉ The directory structure has the entries
of the all the files related to that
directory.
◉ We can consider that a directory is a file, that contains information about the other files.
Operations Performed on Directory
◉ Search for a file
◉ Create a file
◉ Delete a file
◉ List a directory
◉ Rename a file
◉ Traverse the file system
Organize the Directory (Logically) to Obtain
◉ Efficiency – locating a file quickly
◉ Naming – convenient to users
○ Two users can have same name for different files
○ The same file can have several different names
◉ Grouping – logical grouping of files by properties, (e.g., all Java programs,
all games, …)
File Sharing
◉ Sharing of files on multi-user systems is desirable
◉ Sharing may be done through a protection scheme
◉ On distributed systems, files may be shared across a network
◉ Network File System (NFS) is a common distributed file-sharing method
◉ User IDs identify users, allowing permissions and protections to be per-user
◉ Group IDs allow users to be in groups, permitting group access rights
Protection
◉ File owner/creator should be able to control:
○ what can be done
○ by whom
◉ Types of access
○ Read
○ Write
○ Execute
○ Append
○ Delete
○ List
File Implementation
File-System Structure
◉ File structure
○ Logical storage unit
○ Collection of related information
◉ File system resides on secondary storage (disks)
◉ File system organized into layers
◉ File control block – storage structure consisting of information about a file
Virtual File Systems
◉ A virtual file system (VFS) is an abstraction layer that resides above a file system and provides an interface between the kernel and file system. Through a VFS, client applications can access different file systems.虚拟文档系统 (VFS) 是驻留在文档系统之上的抽象层,在内核和文档系统之间提供接口。通过 VFS,客户端应用进程可以访问不同的文档系统。
◉ VFS provide an object-oriented way of implementing file systems.
◉ VFS allows the same system call interface (the API) to be used for different types of file systems.
◉ The API is to the VFS interface, rather than any specific type of file system.
Directory Implementation
◉ Linear list of file names with pointer to the data blocks.
○ simple to program
○ time-consuming to execute
◉ Hash Table – linear list with hash data structure.
○ decreases directory search time
○ collisions – situations where two file names hash to the same location
○ fixed size
Allocation Methods
◉ An allocation method refers to how disk blocks are allocated for files:
○ Contiguous allocation
○ Linked allocation
○ Indexed allocation
Contiguous Allocation连续分配
◉ Each file occupies a set of contiguous blocks on the disk
◉ Simple – only starting location (block #) and length (number of blocks) are
required
◉ Random access
◉ Wasteful of space (dynamic storage-allocation problem)
◉ Files cannot grow
Extent-Based Systems
◉ Many newer file systems (i.e. Linux File System) use a modified contiguous
allocation scheme
◉ Extent-based file systems allocate disk blocks in extents
◉ An extent is a contiguous block of disks
○ Extents are allocated for file allocation
○ A file consists of one or more extents.
◉ Extents can be added to an existing file that needs space to grow.
◉ A block can be found given by the location of the first block in the file and the block
count, plus a link to the first extent.
Linked Allocation
◉ Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk
Indexed Allocation
◉ Brings all pointers together into the index block.
◉ Logical view
额外术语
sequential Sequential access is a term describing a group of elements being accessed in a predetermined, ordered sequence.顺序访问是一个术语,描述以预定的有序顺序访问的一组元素。
multiprogramming While a multiprogramming operating system allows more than one program to run simultaneously using a single CPU
multitasking Multitasking means concurrent execution of multiple processes by one user on the same computer utilizing multiple CPUs.