Post on 19-Oct-2021
transcript
Chapter 6: Scheduling ���1
Overzealous autoconfig
Chapter 6: Scheduling
Start of Lecture: February 7, 2014
���2
Chapter 6: Scheduling
Reminders
• Thought Question for Exercise 2: should be from different parts of readings (unless they are high-level)
• if your question corresponds to a section, then both questions should not be from that section
• Check out telnet for testing a server
• e.g. telnet localhost 9800
• Remember to kill your daemons
���3
ps -e | grep select_server
kill -9 pid_of_your_server
Chapter 6: Scheduling
High-level view of Assignment 2
• You implement the server; no need to implement the client, let telnet be the client for testing
• You’re implementing a HTTP protocol. Even though its a subset of what is used when you type a url into a browser, its the same idea and should help you understand your task
• URL is location of a file (e.g. index.html); entering into browser requests that file from server; server writes that file to the socket so that the client can read it and display it (the browser handles displaying it)
���4
Chapter 6: Scheduling
Back to scheduling
• Want to (optimally) schedule processes to maximize some measure that you have decided on, such as
• CPU utilization — percent time CPU busy
• Throughput — number processes completed
• Turnaround time — total time to complete process, including time waiting, time executing on CPU and time doing I/O
• Waiting time — time spent waiting in ready queue
• Response time — time it takes to start responding, e.g. a process may quickly compute something, return that response, and then continue
���5
Chapter 6: Scheduling
Scheduling policies
• Scheduling policies may be preemptive or non-preemptive
• In non-preemptive systems, short-term scheduler allows current processes to run until it blocks (waiting for an event or a resource) or it terminates
• still needs to decide which process to run next after CPU released
• Preemptive policies force the currently active process to release the CPU on certain events, such as clock interrupt, I/O interrupts or system call interrupts
• much more control over what processes are run than non-preemptive
���6
Chapter 6: Scheduling
Exercise: brainstorm a scheduling algorithm• Assume processes added to your list of ready processes
and you can select them from anywhere in the list
• For now, assume also that you will not pre-empt processes: once you select a process, it runs to completion or blocks (e.g. due to a wait for I/O devices); simple algorithm is fine
• Can you think of any ways that you might schedule? Decide what assumptions you want to make to start, such as
• knowing the runtime length of each process
• starting with the below list of processes, with given lengths
���7
P1 P3 P4 P5P2
Chapter 6: Scheduling
Scheduling algorithms
The following are some common scheduling algorithms
���8
Non-preemptive
• First-Come, First-Served (FCFS) scheduling
• Shortest Job First (SJF)
Preemptive
• Round-Robin scheduling
• Priority-based scheduling
Good for background, batch jobs
Good for foreground, interactive jobs
Chapter 6: Scheduling
Simplest scheduling algorithm: FCFS
• First-come, first-served is agnostic to any information about processes; its a very simple heuristic
• The implementation is easily managed with a FIFO queue
• as new processes arrive, they are added to the queue
• when a new process is ready to run, the head is removed from the queue
• Easy to implement and understand
���9
Chapter 6: Scheduling
FCFS: First-Come First-Served
���10
6.10 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition
First- Come, First-Served (FCFS) Scheduling
Process Burst 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
P P P1 2 3
0 24 3027
Chapter 6: Scheduling
FCFS: First-Come First-Served
���11
6.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition
FCFS Scheduling (Cont.)
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 � Convoy effect - short process behind long process
z Consider one CPU-bound and many I/O-bound processes
P1
0 3 6 30
P2 P3
Chapter 6: Scheduling
Disadvantages of FCFS
• Okay for longer jobs, but short jobs really suffer
• A long CPU bound job may hog the CPU and force shorter (possibly I/O bound) jobs to wait for long periods
• Can lead to a lengthy queue of ready jobs, waiting for the long job, hence the “convoy effect”
• Not an inherent problem with long jobs, since even just by changing the order and running each to completion, managed to significantly improve performance
• is a problem with not pre-empting
���12
Chapter 6: Scheduling
Round-Robin: FCFS with preemption
• Can still use a FIFO queue, but can interrupt a process after it has run for a fixed time quantum
• e.g a time quantum could be 10 milliseconds
• If a process blocks/terminates before time quantum is used up, then the scheduler moves to the next process
• Otherwise, cycling evenly through processes: fair and responsive (particularly if time quantum short)
• Easier to analyze: if there are n processes waiting to run and time quantum is q units, what is the worst case length of time a process must wait for the CPU?
���13
worst case wait time = (n-1) x q
Chapter 6: Scheduling
Round Robin with time quantum 15 secs
���14
Chapter 6: Scheduling
Performance of Round Robin Scheduling
• Highly dependent on time quantum
• a very large time quantum means RR is essentially equal to FCFS
• a very small time quantum might result in many context-switches
• In practice, time quanta range from 10 to 100 milliseconds, which is much longer than the typical time of a context switch (less than 10 microseconds)
• Want quantum large enough so that many processes can complete CPU burst within quantum and improve turnaround time; but not so large that RR becomes FCFS
���15
Chapter 6: Scheduling
Turnaround time varies with Time Quantum
���16
6.23 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition
Turnaround Time Varies With The Time Quantum
80% of CPU bursts should be shorter than q
Chapter 6: Scheduling
Video Break: brought to you by another stupendous classmate (you should submit too!)
���17
Chapter 6: Scheduling
Shortest Job First (SJF) without preempting
• Associate with each process the length of its next CPU burst; use lengths to schedule process with shortest expected time (still a very simple algorithm)
• SJF is optimal for average wait time, i.e. gives minimum average waiting time for a given set of processes
���18
Chapter 6: Scheduling
Shortest Job First (SJF)
���19
6.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition
Example of SJF
ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
� SJF scheduling chart
� Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
P3
0 3 24
P4 P1
169
P2
Chapter 6: Scheduling
Proof that SJF optimal for average wait time
• Assume given m process in order of increasing runtime length, n1, …, nm
• SJF returns that order (since runs shortest jobs first).
• Assume there exists a different ordering that has the lowest average wait time; since the order is different than SJF, its not in increasing order, and so there must exist processes j and k where nj < nk but process k runs before j !
• Let w_i be wait time for process i under this different ordering, with average wait time
���20
Proof by contradiction:
1
m
mX
i=1
wi
WLOG, can assume exists nj < nk, not just nj nk
Chapter 6: Scheduling
Proof that SJF optimal for average wait time
• Let t1 be the index when process k is run, t2 when process j is run (e.g. t1 = 5 means process k is fifth process run)
• We know t1 < t2. Now pretend we swap process k and j, so process j now runs at index t1, and k at t2
���21
Proof by contradiction:
Case 1: wait before t1
Before swap
t1X
i=1
wi
t1X
i=1
wi
t2X
i=t1+1
wi
After swap
Case 2: wait between t1 & t2
t2X
i=t1+1
wi � (nk � nj)
= >
Case 3: wait after t2
=
mX
i=t2+1
wi
mX
i=t2+1
wi
Chapter 6: Scheduling
Proof that SJF optimal for average wait time
• Total wait time is less after swapping process j and k, but the ordering before the swap was supposed to be optimal!
!
• So, must be that there does not exist such an ordering, that has lower total wait time (and so lower average wait time) than the SJF ordering
���22
Proof by contradiction:
Chapter 6: Scheduling
Disadvantages of SJF
• Difficult to determine the CPU-burst for each process
• could ask the user for total length of job (or an upper bound)
• could try to predict this length, based on typical behaviour or previous burst
• Long running jobs could starve for CPU usage when there is a steady supply of short jobs
• Variant of SJF called Highest Response Ratio Next
• similar to SJF, but includes a priority which increases the longer a job waits
• Variant of SJF called Shortest-remaining-time-first, which is a preemptive version of SJF
���23
Chapter 6: Scheduling
Predicting CPU-burst lengths
• Can only estimate the length — hope that it might be similar to (most recent) previous lengths
• One approach: use average of all previous CPU bursts
• any disadvantages to this approach?
• Another approach: use exponential averaging
���24
Chapter 6: Scheduling
Exponential averaging for CPU bursts
���25
6 4 6 4 13 13 13 …810 6 6 5 9 11 12 …
CPU burst (ti)
"guess" (τi)
ti
τi
2
time
4
6
8
10
12tn = actual length of
nthCPU burst
⌧n+1 = predicted length of
next CPU burst
⌧n+1 = ↵tn + (1� ↵)⌧n
where 0 ↵ 1
and commonly ↵ = 0.5
Chapter 6: Scheduling
Example of Shortest-remaining-time-first
���26
6.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition
Example of Shortest-remaining-time-first
� Now we add the concepts of varying arrival times and preemption to the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
� Preemptive SJF Gantt Chart
� Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec
P4
0 1 26
P1 P2
10
P3P1
5 17