630 likes | 794 Views
Operating System Concepts. Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University. Chapter 6 CPU Scheduling. CPU scheduling The basis of multiprogrammed OSs Make the computer more production
E N D
Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 6 CPU Scheduling • CPU scheduling • The basis of multiprogrammed OSs • Make the computer more production • Switch the CPU among processes • We introduce • The basic scheduling concepts • Several different CPU-scheduling algorithms • Selecting an algorithm for a particular system Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.1 Basic Concepts • Several processes are kept in memory at one time • When a process has to wait, the OS takes the CPU away from that process, and gives the CPU to another process. • Almost all computer resources are scheduled before use. Chapter 6 CPU Scheduling
6.1.1 CPU-I/O Burst Cycle • Observed property of processes • Process execution consists of a cycle of • CPU execution • I/O wait • Process execution begins with a CPU burst. • That is followed by an I/O burst, then another CPU burst, then another I/O burst, and so on. Chapter 6 CPU Scheduling
Alternating sequence ofCPU and I/O bursts Chapter 6 CPU Scheduling
Histogram of CPU-burst times Chapter 6 CPU Scheduling
6.1.1 CPU-I/O Burst Cycle • Measure CPU bursts • An I/O bound program • Many short CPU bursts • A CPU bound program • A few very long CPU bursts • Help select an appropriate CPU-scheduling algorithm Chapter 6 CPU Scheduling
6.1.2 CPU Scheduler • Whenever the CPU becomes idle • Select one of the processes in the ready queue to be executed • Carried out by the short-term scheduler, also called CPU scheduler • A ready queue may be implemented as • A FIFO queue • A priority queue • A tree • An unordered linked list Chapter 6 CPU Scheduling
6.1.3 Preemptive Scheduling • CPU scheduling decisions take place when a process • Switches from running to waiting state • Switches from running to ready state • Switches from waiting to ready • Terminates • Scheduling only under 1 and 4 is nonpreemptive • Otherwise is preemptive • Incur a cost Chapter 6 CPU Scheduling
6.1.4 Dispatcher • Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; • switching context • switching to user mode • jumping to the proper location in the user program to restart that program • Dispatch latency • time it takes for the dispatcher to stop one process and start another running. Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.2 Scheduling Criteria • CPU utilization (max) • keep the CPU as busy as possible • Throughput (max) • number of processes that complete their execution per time unit • Turnaround time(min) • amount of time to execute a particular process • Waiting time(min) • amount of time a process has been waiting in the ready queue • Response time(min) • amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment) Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.3 Scheduling Algorithms • Dealing the problem of deciding which of the processes in the ready queue to be allocated the CPU • First-Come, First-Served Scheduling • Shortest-Job-First Scheduling • Priority Scheduling • Round-Robin Scheduling • Multilevel Queue Scheduling • Multilevel Feedback Queue Scheduling Chapter 6 CPU Scheduling
6.3.1 First-Come, First-Served Scheduling • The process that requests the CPU first is allocated the CPU first. • The simplest CPU-scheduling algorithm • Implementation with a FIFO queue • Add to the tail of the queue • Remove from the head to the queue • The code is simple to write and understand • Average waiting time is often quite long. Chapter 6 CPU Scheduling
P1 P2 P3 0 24 27 30 6.3.1 First-Come, First-Served Scheduling ProcessBurst Time P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 Chapter 6 CPU Scheduling
P2 P3 P1 0 3 6 30 6.3.1 First-Come, First-Served Scheduling Suppose that the processes arrive in the order P2 , P3 , P1 • The Gantt chart for the schedule is: • Waiting time for P1 = 6; P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Much better than previous case. Chapter 6 CPU Scheduling
6.3.1 First-Come, First-Served Scheduling • Convoy effect • All the other processes wait for one big process to get off the CPU • Results in lower CPU and device utilization • If the shorter processes were allowed to go first • FCFS is non-preemptive • Once the CPU has been allocated to a process, that process keeps the CPU until it releases the CPU. • Particular troublesome in time-sharing system • Each user needs to get a share of the CPU at regular intervals Chapter 6 CPU Scheduling
6.3.2 Shortest-Job-First Scheduling • When the CPU is available, it is assigned to the process that has the smallest next CPU burst. • Associate each process with the length of the latter’s next CPU burst • Not its total length • Another term – shortest next CPU burst • FCFS scheduling is used to break the tie • Provably optimal • Minimum average waiting time for a given set of processes • Real difficulty • Knowing the length of the next CPU burst • Used frequently in long-term scheduling Chapter 6 CPU Scheduling
P4 P1 P3 P2 0 3 9 16 24 6.3.2 Shortest-Job-First Scheduling ProcessBurst Time P1 6 P2 8 P3 7 P4 3 The Gantt Chart for the schedule is: • Waiting time for P1 = 3; P2 = 16; P3 = 9; P4 = 0 • Average waiting time: (3 + 16 + 9 + 0) / 4 = 7 • Using FCFS scheme : ( 0 + 6 + 14 + 21) / 4 = 10.25 Chapter 6 CPU Scheduling
6.3.2 Shortest-Job-First Scheduling • To approximate SJF scheduling • To predict its value • Use the length of previous CPU bursts • exponential average Chapter 6 CPU Scheduling
Prediction of the length of the next CPU burst Chapter 6 CPU Scheduling
Examples ofExponential Averaging • = 0 • n+1 = n • Recent history does not count. • = 1 • n+1 = tn • Only the actual last CPU burst counts. • If we expand the formula, we get: n+1 = tn+(1 - ) tn -1 + … +(1 - )j tn -1 + … +(1 - )n+1 tn 0 • Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor. Chapter 6 CPU Scheduling
6.3.2 Shortest-Job-First Scheduling • Two schemes • Nonpreemptive • Allow the current running process to finish its CPU burst • Preemptive • If a new process arrives with CPU burst length less than remaining time of current executing process, preempt. • Called as Shortest-Remaining-Time-First (SRTF). Chapter 6 CPU Scheduling
P1 P2 P4 P1 P3 0 1 5 10 17 26 Example of Preemptive SJF Process Arrival TimeBurst Time P1 0 8 P2 1 4 P3 2 9 P4 3 5 • Preemptive SJF • Average waiting time = ( (10-1) + (1-1) + (17-2) + (5-3)) / 4 = 6.5 Chapter 6 CPU Scheduling
6.3.3 Priority Scheduling • The CPU is allocated to the process with the highest priority. • A priority is associated with each process • Fixed range of number, such as 0 to 7 • We use low numbers to represent high priority • Equal-priority processes are scheduled in FCFS order • SJF is a special case of the general priority-scheduling algorithm • The priority is the inverse of the predicted next CPU burst Chapter 6 CPU Scheduling
P2 P5 P1 P3 P4 0 1 6 16 18 19 Example of priority scheduling ProcessBurst TimePriority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 • Priority scheduling • Average waiting time = ( 6 + 0 + 16 + 18 + 1 ) / 5 = 8.2 Chapter 6 CPU Scheduling
6.3.3 Priority Scheduling • Priorities can be defined • Internally • Use some measurable quantity • Time limits, memory requirements… • Externally • Set by criteria external to OS • importance, political factors • Priority scheduling can be • Preemptive • Nonpreemptive • Major problem • Indefinite blocking or starvation • Solution: aging • Gradually increase the priority of processes that wait for a long time Chapter 6 CPU Scheduling
6.3.4 Round-Robin Scheduling • A small unit of time, called a time quantum ( or time slice) is defined. • Generally from 10 to 100 milliseconds • The ready queue is treated as a circular, FIFO queue. • The CPU scheduler goes around the ready queue • Allocate the CPU to each process for a time interval of up to 1 time quantum. • Designed especially for time-sharing systems • Two cases • CPU burst less than 1 time quantum • The process release the CPU voluntarily • CPU burst longer than 1 time quantum • Context switch will be executed Chapter 6 CPU Scheduling
P1 P1 P2 P3 P1 P1 P1 P1 0 4 7 10 14 18 22 26 30 Example of round-robin scheduling ProcessBurst Time P1 24 P2 3 P3 3 • Round-robin scheduling • A time-quantum of 4 milliseconds • Average waiting time = ( 6 + 4 + 7 ) / 3 = 5.66 • Often quite long • RR scheduling is preemptive. Chapter 6 CPU Scheduling
6.3.4 Round-Robin Scheduling • n processes in the ready queue and the time quantum is q • Each process gets 1/n of the CPU time in chunks of at most q time units at once • No process waits more than (n-1)q time units • Performance • Depend heavily on the size of the time quantum • q is very large : the same as the FCFS policy • q is very small : called processor sharing • q must be large with respect to context switch, otherwise overhead is too high. Chapter 6 CPU Scheduling
A smaller time quantumincreases context switches Chapter 6 CPU Scheduling
6.3.4 Round-Robin Scheduling • Turnaround time also depends on the size of the time quantum. • Rule of thumb • 80 percent of the CPU burst shouldbe shorter thanthe time quantum Chapter 6 CPU Scheduling
6.3.5 Multilevel Queue Scheduling • Processes are classified into different groups • Foreground (or interactive) processes • Background (or batch) processes • Multilevel queue-scheduling algorithm • Partition the ready queue into several separate groups • Each group has its own scheduling algorithm • Scheduling among the queues • Fixed-priority preemptive scheduling • Possibility of starvation • Time-slice between the queues • A certain portion of the CPU time Chapter 6 CPU Scheduling
Multilevel Queue Scheduling Chapter 6 CPU Scheduling
6.3.6 Multilevel Feedback Queue • A process can move between the various queues • Use too much CPU time : move to a lower-priority queue • Wait too long : move to a higher-priority queue • This form of aging prevents starvation • Multilevel-feedback-queue scheduler defined by : • number of queues • scheduling algorithms for each queue • method used to determine when to upgrade a process • method used to determine when to demote a process • method used to determine which queue a process will enter when that process needs service Chapter 6 CPU Scheduling
Example ofMultilevel Feedback Queue • Three queues: • Q0– time quantum 8 milliseconds • Q1– time quantum 16 milliseconds • Q2– FCFS • Scheduling • A new job enters queue Q0which is servedFCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q1. • At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2. Chapter 6 CPU Scheduling
Multilevel feedback queues • The most general and complex scheme Q0 Q1 Q2 Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.4 Multiple-Processor Scheduling • CPU scheduling is more complex when multiple CPUs are available • Homogeneous system • Identical processors • Load sharing can occur • Separate queue • Common queue • Self-scheduling • Master-slave structure • Heterogeneous system • Different processors Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.5 Real-Time Scheduling • Hard real-time systems • complete a critical task within a guaranteed amount of time. • Special-purpose software running on dedicated hardware • Lack the full functionality of modern computers and operating systems • Soft real-time computing • Critical processes receive priority over less fortunate ones • May cause unfair allocation of resources • A general-purpose system can also support special tasks Chapter 6 CPU Scheduling
6.5 Real-Time Scheduling • Soft real-time computing • Priority scheduling • Real-time process must have the highest priority • The dispatch latency must be small • OSs are forced to wait for a system call to complete or an I/O block to take place before doing a context switch • Solution: system calls must be preemptible • Insert preemption points • Make the entire kernel preemptible • Effective and complex method • Solaris 2 • Over 100 ms v.s. 2 ms Chapter 6 CPU Scheduling
Dispatch Latency Chapter 6 CPU Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Real-Time Scheduling Algorithm Evaluation Process Scheduling Models Summary Exercises Chapter 6 CPU Scheduling Chapter 6 CPU Scheduling
6.6 Algorithm Evaluation • How to select a CPU-scheduling algorithm for a particular system? • Define the criteria • CPU utilization • Response time • Throughput • A criteria may include several measures • Maximize CPU utilization under the constraint that the maximum response time is 1 second. • Maximize throughput such that turnaround time is linearly proportional to total execution time. Chapter 6 CPU Scheduling
6.6.1 Deterministic Modeling • Take a particular predetermined workload and defines the performance of each algorithm for that workload • Example • Assume five processes arrive at time 0, in the order given, with the length of the CPU-burst time given in milliseconds: ProcessBurst TimePriority P1 10 3 P2 1 1 P3 2 4 P4 1 5 P5 5 2 • Consider the FCFS, SJF, and RR(quantum=10 milliseconds) scheduling algorithms for this set of processes. • Which gives the minimum average waiting time? Chapter 6 CPU Scheduling
6.6.1 Deterministic Modeling • Advantage • Simple and fast • Exact numbers • Disadvantage • Too specific, and require too much exact knowledge, to be useful • Main use • Describing scheduling algorithms and providing examples Chapter 6 CPU Scheduling
6.6.2 Queueing Models • No static set of processes (and times) • What can be determined • The distribution of CPU and I/O bursts • Measured and approximated by mathematical formulas • Queueing-network analysis • CPU is a server with its ready queue • I/O system is a server with its device queue • Knowing arrival rates and service rates • Utilization, average queue length, average wait time Chapter 6 CPU Scheduling