Download presentation
Presentation is loading. Please wait.
Published byMervin Chambers Modified over 9 years ago
1
Operating System Principles Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2
2Ku-Yaw ChangChapter 5 Process Scheduling CPU scheduling or Process scheduling The basis of multiprogrammed OSs The basis of multiprogrammed OSs Make the computer more productive Make the computer more productive Switch the CPU among processes We introduce The basic scheduling concepts The basic scheduling concepts Several different CPU-scheduling algorithms Several different CPU-scheduling algorithms Select an algorithm for a particular system
3
3Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
4
4Ku-Yaw ChangChapter 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. 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 is one of the primary resources CPU scheduling is central to OS design
5
5Ku-Yaw ChangChapter 5 Process Scheduling 5.1.1 CPU-I/O Burst Cycle An observed property of processes Process execution consists of a cycle of 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 followed by an I/O burst, then another CPU burst, then another I/O burst, and so on
6
6Ku-Yaw ChangChapter 5 Process Scheduling Alternating sequence of CPU and I/O bursts
7
7Ku-Yaw ChangChapter 5 Process Scheduling Histogram of CPU-burst durations
8
8Ku-Yaw ChangChapter 5 Process Scheduling 5.1.1 CPU-I/O Burst Cycle Measure CPU bursts An I/O bound program An I/O bound program Many short CPU bursts A CPU bound program A CPU bound program A few very long CPU bursts Help select an appropriate CPU-scheduling algorithm
9
9Ku-Yaw ChangChapter 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 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 FIFO queue A priority queue A priority queue A tree A tree An unordered linked list An unordered linked list
10
10Ku-Yaw ChangChapter 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 waiting state Switch from running to ready state Switch from running to ready state Switch from waiting to ready state Switch from waiting to ready state Terminate Terminate Scheduling only under 1 and 4 is nonpreemptive (or cooperative) Otherwise is preemptive Otherwise is preemptive Incur a cost
11
11Ku-Yaw ChangChapter 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 context switching to user mode switching to user mode jumping to the proper location in the user program to restart that program 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. time it takes for the dispatcher to stop one process and start another running.
12
12Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
13
13Ku-Yaw ChangChapter 5 Process Scheduling 5.2 Scheduling Criteria CPU utilization (max) keep the CPU as busy as possible keep the CPU as busy as possible Throughput (max) number of processes that complete their execution per time unit number of processes that complete their execution per time unit Turnaround time (min) amount of time to execute a particular process amount of time to execute a particular process Waiting time (min) amount of time a process has been waiting in the ready queue 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) amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)
14
14Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
15
15Ku-Yaw ChangChapter 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 First-Come, First-Served Scheduling Shortest-Job-First Scheduling Shortest-Job-First Scheduling Priority Scheduling Priority Scheduling Round-Robin Scheduling Round-Robin Scheduling Multilevel Queue Scheduling Multilevel Queue Scheduling Multilevel Feedback-Queue Scheduling Multilevel Feedback-Queue Scheduling
16
16Ku-Yaw ChangChapter 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 The simplest CPU-scheduling algorithm Implementation with a FIFO queue 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 The code is simple to write and understand Average waiting time is often quite long.
17
17Ku-Yaw ChangChapter 5 Process Scheduling ProcessBurst Time P 1 24 P 2 3 P 2 3 P 3 3 P 3 3 Suppose that the processes arrive in the order: P 1, P 2, P 3 The Gantt Chart for the schedule is: Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27) / 3 = 17 P1P1 P2P2 P3P3 2427300 5.3.1 First-Come, First-Served Scheduling
18
18Ku-Yaw ChangChapter 5 Process Scheduling 5.3.1 First-Come, First-Served Scheduling Suppose that the processes arrive in the order Suppose that the processes arrive in the order P 2, P 3, P 1 P 2, P 3, P 1 The Gantt chart for the schedule is: Waiting time for P 1 = 6; P 2 = 0; P 3 = 3 Average waiting time: (6 + 0 + 3) / 3 = 3 Much better than previous case Much better than previous case P1P1 P3P3 P2P2 63300
19
19Ku-Yaw ChangChapter 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 All the other processes wait for one big process to get off the CPU Results in lower CPU and device utilization 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. 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 Particular troublesome in time-sharing system Each user needs to get a share of the CPU at regular intervals
20
20Ku-Yaw ChangChapter 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 Associate each process with the length of the latter’s next CPU burst Not its total length Another term – shortest next CPU burst Another term – shortest next CPU burst FCFS scheduling is used to break the tie FCFS scheduling is used to break the tie Provably optimal Minimum average waiting time for a given set of processes Minimum average waiting time for a given set of processes Real difficulty Knowing the length of the next CPU burst Knowing the length of the next CPU burst Used frequently in long-term scheduling Used frequently in long-term scheduling
21
21Ku-Yaw ChangChapter 5 Process Scheduling ProcessBurst Time P 1 6 P 2 8 P 2 8 P 3 7 P 3 7 P 4 3 P 4 3 The Gantt Chart for the schedule is: The Gantt Chart for the schedule is: Waiting time for P 1 = 3; P 2 = 16; P 3 = 9; P 4 = 0 Average waiting time: (3 + 16 + 9 + 0) / 4 = 7 Using FCFS scheme : ( 0 + 6 + 14 + 21) / 4 = 10.25 5.3.2 Shortest-Job-First Scheduling P4P4 P1P1 P3P3 316240 P2P2 9
22
22Ku-Yaw ChangChapter 5 Process Scheduling 5.3.2 Shortest-Job-First Scheduling To approximate SJF scheduling To predict its value To predict its value Use the length of previous CPU bursts Use the length of previous CPU bursts exponential average
23
23Ku-Yaw ChangChapter 5 Process Scheduling Prediction of the length of the next CPU burst
24
24Ku-Yaw ChangChapter 5 Process Scheduling Examples of Exponential Averaging = 0 n+1 = n n+1 = n Recent history does not count. Recent history does not count. = 1 n+1 = t n n+1 = t n Only the actual last CPU burst counts. Only the actual last CPU burst counts. If we expand the formula, we get: n+1 = t n +(1 - ) t n-1 + … +(1 - ) j t n-j + … +(1 - ) j t n-j + … +(1 - ) n+1 0 +(1 - ) n+1 0 Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor.
25
25Ku-Yaw ChangChapter 5 Process Scheduling 5.3.2 Shortest-Job-First Scheduling Two schemes Nonpreemptive Nonpreemptive Allow the current running process to finish its CPU burst Preemptive 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) Called as Shortest-Remaining-Time-First (SRTF)
26
26Ku-Yaw ChangChapter 5 Process Scheduling ProcessArrival TimeBurst Time P 1 08 P 2 14 P 2 14 P 3 29 P 3 29 P 4 35 P 4 35 Preemptive SJF Average waiting time = ( (10-1) + (1-1) + (17-2) + (5-3)) / 4 = 6.5 Example of Preemptive SJF P1P1 P3P3 P2P2 51260 P4P4 1017 P1P1
27
27Ku-Yaw ChangChapter 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 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 Equal-priority processes are scheduled in FCFS order SJF is a special case of the general priority- scheduling algorithm SJF is a special case of the general priority- scheduling algorithm The priority is the inverse of the predicted next CPU burst
28
28Ku-Yaw ChangChapter 5 Process Scheduling ProcessBurst TimePriority P 1 103 P 1 103 P 2 11 P 2 11 P 3 24 P 3 24 P 4 15 P 4 15 P 5 52 P 5 52 Priority scheduling Average waiting time = ( 6 + 0 + 16 + 18 + 1 ) / 5 = 8.2 Example of priority scheduling P3P3 P4P4 P5P5 61190 P1P1 1618 P2P2
29
29Ku-Yaw ChangChapter 5 Process Scheduling 5.3.3 Priority Scheduling Priorities can be defined Internally Internally Use some measurable quantity Time limits, memory requirements… Time limits, memory requirements… Externally Externally Set by criteria external to OS importance, political factors importance, political factors Priority scheduling can be Preemptive Preemptive Nonpreemptive Nonpreemptive Major problem Indefinite blocking or starvation Indefinite blocking or starvation Solution: aging Solution: aging Gradually increase the priority of processes that wait for a long time
30
30Ku-Yaw ChangChapter 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 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. Allocate the CPU to each process for a time interval of up to 1 time quantum. Designed especially for time-sharing systems Designed especially for time-sharing systems Two cases CPU burst less than 1 time quantum CPU burst less than 1 time quantum The process release the CPU voluntarily CPU burst longer than 1 time quantum CPU burst longer than 1 time quantum Context switch will be executed
31
31Ku-Yaw ChangChapter 5 Process Scheduling ProcessBurst Time P 1 24 P 1 24 P 2 3 P 2 3 P 3 3 P 3 3 Round-robin scheduling A time-quantum of 4 milliseconds A time-quantum of 4 milliseconds Average waiting time = ( 6 + 4 + 7 ) / 3 = 5.66 Often quite long Often quite long RR scheduling is preemptive. Example of round-robin scheduling P3P3 P1P1 P2P2 1440 P1P1 2630 P1P1 P1P1 P1P1 P1P1 7101822
32
32Ku-Yaw ChangChapter 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 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 No process waits more than (n-1)q time unitsPerformance Depend heavily on the size of the time quantum 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. q must be large with respect to context switch, otherwise overhead is too high.
33
33Ku-Yaw ChangChapter 5 Process Scheduling A smaller time quantum increases context switches
34
34Ku-Yaw ChangChapter 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 should be shorter than the time quantum 80 percent of the CPU burst should be shorter than the time quantum
35
35Ku-Yaw ChangChapter 5 Process Scheduling 5.3.5 Multilevel Queue Scheduling Processes are classified into different groups Foreground (or interactive) processes Foreground (or interactive) processes Background (or batch) processes Background (or batch) processes Multilevel queue-scheduling algorithm Partition the ready queue into several separate groups Partition the ready queue into several separate groups Each group has its own scheduling algorithm Each group has its own scheduling algorithm Scheduling among the queues Scheduling among the queues Fixed-priority preemptive scheduling Possibility of starvation Possibility of starvation Time-slice between the queues A certain portion of the CPU time A certain portion of the CPU time
36
36Ku-Yaw ChangChapter 5 Process Scheduling Multilevel Queue Scheduling
37
37Ku-Yaw ChangChapter 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 Use too much CPU time : move to a lower-priority queue Wait too long : move to a higher-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 number of queues scheduling algorithms for each queue scheduling algorithms for each queue method used to determine when to upgrade a process method used to determine when to upgrade a process method used to determine when to demote 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 method used to determine which queue a process will enter when that process needs service
38
38Ku-Yaw ChangChapter 5 Process Scheduling Three queues: Q 0 – time quantum 8 milliseconds Q 0 – time quantum 8 milliseconds Q 1 – time quantum 16 milliseconds Q 1 – time quantum 16 milliseconds Q 2 – FCFS Q 2 – FCFSScheduling A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. A new job enters queue Q 0 which is served FCFS. When it gains CPU, job receives 8 milliseconds. If it does not finish in 8 milliseconds, job is moved to queue Q 1. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. At Q 1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q 2. Example of Multilevel Feedback Queue
39
39Ku-Yaw ChangChapter 5 Process Scheduling Multilevel feedback queues The most general and complex scheme Q0Q0 Q1Q1 Q2Q2
40
40Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
41
41Ku-Yaw ChangChapter 5 Process Scheduling Be skipped
42
42Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
43
43Ku-Yaw ChangChapter 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 Competition for the CPU takes place among threads belonging to the same process Many-to-one and many-to-many models Many-to-one and many-to-many models System-contention scope (SCS) Competition for the CPU takes place among all threads in the system Competition for the CPU takes place among all threads in the system One-to-one model One-to-one model
44
44Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
45
45Ku-Yaw ChangChapter 5 Process Scheduling Be skipped
46
46Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
47
47Ku-Yaw ChangChapter 5 Process Scheduling Summary P.181 to 182
48
48Ku-Yaw ChangChapter 5 Process Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Thread Scheduling 6.Operating System Examples 7.Algorithm Evaluation 8.Summary 9.Exercises
49
49Ku-Yaw ChangChapter 5 Process Scheduling Exercises 5.25.35.45.55.7
50
The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.