Download presentation
Presentation is loading. Please wait.
Published byDorothy Joseph Modified over 9 years ago
1
Operating System Concepts Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University
2
2Ku-Yaw ChangChapter 6 CPU Scheduling CPU scheduling The basis of multiprogrammed OSs The basis of multiprogrammed OSs Make the computer more production Make the computer more production 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 Selecting an algorithm for a particular system
3
3Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
4
4Ku-Yaw ChangChapter 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. 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.
5
5Ku-Yaw ChangChapter 6 CPU Scheduling 6.1.1 CPU-I/O Burst Cycle 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. That is followed by an I/O burst, then another CPU burst, then another I/O burst, and so on. That is followed by an I/O burst, then another CPU burst, then another I/O burst, and so on.
6
6Ku-Yaw ChangChapter 6 CPU Scheduling Alternating sequence of CPU and I/O bursts
7
7Ku-Yaw ChangChapter 6 CPU Scheduling Histogram of CPU-burst times
8
8Ku-Yaw ChangChapter 6 CPU Scheduling 6.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 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 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 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 waiting state Switches from running to ready state Switches from running to ready state Switches from waiting to ready Switches from waiting to ready Terminates Terminates Scheduling only under 1 and 4 is nonpreemptive Otherwise is preemptive Otherwise is preemptive Incur a cost
11
11Ku-Yaw ChangChapter 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 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 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
13
13Ku-Yaw ChangChapter 6 CPU Scheduling 6.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 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
15
15Ku-Yaw ChangChapter 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 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 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 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 6 CPU 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 6.3.1 First-Come, First-Served Scheduling
18
18Ku-Yaw ChangChapter 6 CPU Scheduling 6.3.1 First-Come, First-Served Scheduling Suppose that the processes arrive in the order Suppose that the processes arrive in the order P2, P3, P1 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. P1P1 P3P3 P2P2 63300
19
19Ku-Yaw ChangChapter 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 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 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 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 6 CPU 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 6.3.2 Shortest-Job-First Scheduling P4P4 P1P1 P3P3 316240 P2P2 9
22
22Ku-Yaw ChangChapter 6 CPU Scheduling 6.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 6 CPU Scheduling Prediction of the length of the next CPU burst
24
24Ku-Yaw ChangChapter 6 CPU 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 -1 + … +(1 - ) j t n -1 + … +(1 - ) n+1 t n 0 +(1 - ) n+1 t n 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 6 CPU Scheduling 6.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).
26
26Ku-Yaw ChangChapter 6 CPU 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 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 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 6 CPU 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 6 CPU Scheduling 6.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 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 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 6 CPU 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 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 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 6 CPU Scheduling A smaller time quantum increases context switches
34
34Ku-Yaw ChangChapter 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 should be shorter than the time quantum 80 percent of the CPU burst should be shorter than the time quantum
35
35Ku-Yaw ChangChapter 6 CPU Scheduling 6.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 6 CPU Scheduling Multilevel Queue Scheduling
37
37Ku-Yaw ChangChapter 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 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 6 CPU 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 6 CPU Scheduling Multilevel feedback queues The most general and complex scheme Q0Q0 Q1Q1 Q2Q2
40
40Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
41
41Ku-Yaw ChangChapter 6 CPU Scheduling 6.4 Multiple-Processor Scheduling CPU scheduling is more complex when multiple CPUs are available Homogeneous system Identical processors Identical processors Load sharing can occur Load sharing can occur Separate queue Common queue Self-scheduling Self-scheduling Master-slave structure Master-slave structure Heterogeneous system Different processors Different processors
42
42Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
43
43Ku-Yaw ChangChapter 6 CPU Scheduling 6.5 Real-Time Scheduling Hard real-time systems complete a critical task within a guaranteed amount of time. complete a critical task within a guaranteed amount of time. Special-purpose software running on dedicated hardware Special-purpose software running on dedicated hardware Lack the full functionality of modern computers and operating systems Lack the full functionality of modern computers and operating systems Soft real-time computing Critical processes receive priority over less fortunate ones Critical processes receive priority over less fortunate ones May cause unfair allocation of resources May cause unfair allocation of resources A general-purpose system can also support special tasks A general-purpose system can also support special tasks
44
44Ku-Yaw ChangChapter 6 CPU Scheduling 6.5 Real-Time Scheduling Soft real-time computing Priority scheduling Priority scheduling Real-time process must have the highest priority The dispatch latency must be small 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 Insert preemption points Make the entire kernel preemptible Make the entire kernel preemptible Effective and complex method Solaris 2 Over 100 ms v.s. 2 ms Over 100 ms v.s. 2 ms
45
45Ku-Yaw ChangChapter 6 CPU Scheduling Dispatch Latency
46
46Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
47
47Ku-Yaw ChangChapter 6 CPU Scheduling 6.6 Algorithm Evaluation How to select a CPU-scheduling algorithm for a particular system? Define the criteria Define the criteria CPU utilization Response time Throughput A criteria may include several measures 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.
48
48Ku-Yaw ChangChapter 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: Assume five processes arrive at time 0, in the order given, with the length of the CPU-burst time given in milliseconds: ProcessBurst TimePriority P 1 10 3 P 1 10 3 P 2 1 1 P 2 1 1 P 3 2 4 P 3 2 4 P 4 1 5 P 4 1 5 P 5 5 2 P 5 5 2 Consider the FCFS, SJF, and RR(quantum=10 milliseconds) scheduling algorithms for this set of processes. Consider the FCFS, SJF, and RR(quantum=10 milliseconds) scheduling algorithms for this set of processes. Which gives the minimum average waiting time? Which gives the minimum average waiting time?
49
49Ku-Yaw ChangChapter 6 CPU Scheduling 6.6.1 Deterministic Modeling Advantage Simple and fast Simple and fast Exact numbers Disadvantage Too specific, and require too much exact knowledge, to be useful Too specific, and require too much exact knowledge, to be useful Main use Describing scheduling algorithms and providing examples Describing scheduling algorithms and providing examples
50
50Ku-Yaw ChangChapter 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 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 CPU is a server with its ready queue I/O system is a server with its device queue I/O system is a server with its device queue Knowing arrival rates and service rates Knowing arrival rates and service rates Utilization, average queue length, average wait time
51
51Ku-Yaw ChangChapter 6 CPU Scheduling 6.6.2 Queueing Models Little’s formula : n = * W n: average queue length n: average queue length W : average waiting time W : average waiting time : average arrival rate for new processes : average arrival rate for new processesAdvantage Useful in comparing scheduling algorithms Useful in comparing scheduling algorithmsDisadvantage Approximation only Approximation only Fairly limited applicability Fairly limited applicability
52
52Ku-Yaw ChangChapter 6 CPU Scheduling 6.6.3 Simulations Programming a model of the computer system Simulator A variable representing a clock A variable representing a clock Modify the system state to reflect the activities of the devices, the processes, and the scheduler Modify the system state to reflect the activities of the devices, the processes, and the scheduler Data generation Data generationRandom-number Probability distribution Probability distribution Empirically Empirically Trace tapes Monitor the real system Monitor the real system Expensive Expensive More detailed simulation provides more accurate results
53
53Ku-Yaw ChangChapter 6 CPU Scheduling Evaluation of CPU schedulers by simulation
54
54Ku-Yaw ChangChapter 6 CPU Scheduling 6.6.4 Implementation A simulation is of limited accuracy. The only completely accurate way code it code it put it in the OS put it in the OS see how it work see how it work The cost is high In coding In coding In the reaction of users In the reaction of users
55
55Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
56
56Ku-Yaw ChangChapter 6 CPU Scheduling 6.7 Process Scheduling Models User-level threads Managed by a thread library Managed by a thread library Kernel is unaware of them Kernel is unaware of them Process local scheduling Thread scheduling is done local to the application Thread scheduling is done local to the application System global scheduling Decide which kernel thread to schedule Decide which kernel thread to schedule Case study Solaris 2 Solaris 2 Windows 2000 Windows 2000 Linux Linux
57
57Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
58
58Ku-Yaw ChangChapter 6 CPU Scheduling Summary P.184 to 185
59
59Ku-Yaw ChangChapter 6 CPU Scheduling 1.Basic Concepts 2.Scheduling Criteria 3.Scheduling Algorithms 4.Multiple-Processor Scheduling 5.Real-Time Scheduling 6.Algorithm Evaluation 7.Process Scheduling Models 8.Summary 9.Exercises
60
60Ku-Yaw ChangChapter 6 CPU Scheduling Exercises 6.26.36.46.76.10
61
The End
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.