500 likes | 527 Views
Learn about CPU scheduling, key concepts, criteria, various algorithms, and system examples to enhance productivity and efficiency. Dive into First-Come, First-Served, Shortest-Job-First, Priority, Round-Robin, and more. Understand preemptive scheduling, dispatcher modules, and evaluation strategies.
E N D
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
Chapter 5 Process Scheduling • CPU scheduling or Process scheduling • The basis of multiprogrammed OSs • Make the computer more productive • Switch the CPU among processes • We introduce • The basic scheduling concepts • Several different CPU-scheduling algorithms • Select an algorithm for a particular system Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
5.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. • CPU is one of the primary resources • CPU scheduling is central to OS design Chapter 5 Process Scheduling
5.1.1 CPU-I/O Burst Cycle • An observed property of processes • Process execution consists of a cycle of • CPU execution • I/O wait • Process execution begins with a CPU burst. • followed by an I/O burst, then another CPU burst, then another I/O burst, and so on Chapter 5 Process Scheduling
Alternating sequence ofCPU and I/O bursts Chapter 5 Process Scheduling
Histogram of CPU-burst durations Chapter 5 Process Scheduling
5.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 5 Process Scheduling
5.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 5 Process Scheduling
5.1.3 Preemptive Scheduling • CPU scheduling decisions may take place when a process • Switch from running to waiting state • Switch from running to ready state • Switch from waiting to ready state • Terminate • Scheduling only under 1 and 4 is nonpreemptive (or cooperative) • Otherwise is preemptive • Incur a cost Chapter 5 Process Scheduling
5.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 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
5.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 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
5.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 5 Process Scheduling
5.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 5 Process Scheduling
P1 P2 P3 0 24 27 30 5.3.1 First-Come, First-Served Scheduling ProcessBurst Time P1 24 P2 3 P33 • Suppose that the processes arrive in the order: P1 , P2 , P3The Gantt Chart for the schedule is: • Waiting time for P1 = 0; P2 = 24; P3= 27 • Average waiting time: (0 + 24 + 27) / 3 = 17 Chapter 5 Process Scheduling
P2 P3 P1 0 3 6 30 5.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 5 Process Scheduling
5.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 5 Process Scheduling
5.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 5 Process Scheduling
P4 P1 P3 P2 0 3 9 16 24 5.3.2 Shortest-Job-First Scheduling ProcessBurst Time P1 6 P2 8 P37 P43 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 5 Process Scheduling
5.3.2 Shortest-Job-First Scheduling • To approximate SJF scheduling • To predict its value • Use the length of previous CPU bursts • exponential average Chapter 5 Process Scheduling
Prediction of the length of the next CPU burst Chapter 5 Process 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-j+ … +(1 - )n+1 0 • Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor. Chapter 5 Process Scheduling
5.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 5 Process Scheduling
P1 P2 P4 P1 P3 0 1 5 10 17 26 Example of Preemptive SJF Process Arrival TimeBurst Time P1 0 8 P21 4 P3 2 9 P4 3 5 • Preemptive SJF • Average waiting time = ( (10-1) + (1-1) + (17-2) + (5-3)) / 4 = 6.5 Chapter 5 Process Scheduling
5.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 5 Process Scheduling
P2 P5 P1 P3 P4 0 1 6 16 18 19 Example of priority scheduling ProcessBurst TimePriority P1 10 3 P21 1 P3 2 4 P4 1 5 P5 5 2 • Priority scheduling • Average waiting time = ( 6 + 0 + 16 + 18 + 1 ) / 5 = 8.2 Chapter 5 Process Scheduling
5.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 5 Process Scheduling
5.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 5 Process 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 P23 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 5 Process Scheduling
5.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 5 Process Scheduling
A smaller time quantumincreases context switches Chapter 5 Process Scheduling
5.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 5 Process Scheduling
5.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 5 Process Scheduling
Multilevel Queue Scheduling Chapter 5 Process Scheduling
5.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 5 Process 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 5 Process Scheduling
Multilevel feedback queues • The most general and complex scheme Q0 Q1 Q2 Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
Be skipped Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
5.5.1 Contention Scope • Process-contention scope (PCS) • Competition for the CPU takes place among threads belonging to the same process • Many-to-one and many-to-many models • System-contention scope (SCS) • Competition for the CPU takes place among all threads in the system • One-to-one model Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
Be skipped Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
Summary • P.181 to 182 Chapter 5 Process Scheduling
Basic Concepts Scheduling Criteria Scheduling Algorithms Multiple-Processor Scheduling Thread Scheduling Operating System Examples Algorithm Evaluation Summary Exercises Chapter 5 Process Scheduling Chapter 5 Process Scheduling
Exercises • 5.2 • 5.3 • 5.4 • 5.5 • 5.7 Chapter 5 Process Scheduling