section 2.5
DESCRIPTION
Section 2.5. CPU Scheduling. CPU scheduling goals. CPU utilization . Keep the CPU as busy as possible Throughput . Maximize the number of processes completed in a unit of time Turnaround Time . Minimize the time it takes a process to execute. Waiting Time . - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/1.jpg)
Section 2.5
CPU Scheduling
![Page 2: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/2.jpg)
CPU scheduling goals
![Page 3: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/3.jpg)
Competing performance objectives
• CPU utilization. Keep the CPU as busy as possible
• Throughput. Maximize the number of processes completed in a unit of time
• Turnaround Time. Minimize the time it takes a process to execute
• Waiting Time. Minimize the total amount of time spent in the ready queue
• Response Time. (for interactive jobs)Minimize the amount of time from the submission of a job to the first response.
![Page 4: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/4.jpg)
CPU scheduling and process states
new terminated
ready running
waiting
cpu sched
I/O or event waitI/O or event complete
interruptjob sched
![Page 5: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/5.jpg)
How the OS handles CPU allocation
• When the CPU becomes idle the short-term scheduler is invoked.– It selects a process from the ready queue.
• Then the dispatcher assigns the CPU to the chosen process. Its functions include:– context switching– switching from kernel to user mode– branching to the proper place in the user process
![Page 6: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/6.jpg)
About that ready queue...
• It contains the PCBs of all processes ready to execute.
• We refer to it as a queue but it is not necessarily FIFO.– It may be a priority queue, a tree, or an
unordered linked list– The scheduling algorithm determines which of
these data structures is used
![Page 7: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/7.jpg)
When scheduling decisions are important
• When a process is created– Should the parent or the child process run?
• When a process is terminated– What if no other process is ready to run?
• When a process is blocked– Why is it blocked? What effect does the reason have
on which process is chosen next?
• When an I/O interrupt occurs– Should the process that was waiting on I/O be
scheduled immediately?
![Page 8: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/8.jpg)
Two kinds of scheduling algorithms
• Non-Preemptive– Once a process is allocated the CPU a process,
it keeps it until it voluntarily relinquishes it (by terminating or switching to another state).
• Preemptive– The OS can ‘bump’ a process from the CPU
and allocate it to another process.
• Which has more overhead?
![Page 9: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/9.jpg)
Scheduling algorithm categories• Batch scheduling
– First-Come, First-Served (FCFS)– Shortest Job First (SJF)– Shortest Remaining Time (SRT)– Three-level scheduling
• Interactive scheduling– Round-Robin– Priority Scheduling– Multilevel Feedback Queues
(MLFQ)
• Real-Time scheduling
– Shortest Process Next
– Guaranteed Scheduling
– Fair-Share Scheduling
![Page 10: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/10.jpg)
Analogy: Waiting to photocopycredit: John Estell, Bluffton College
• In an office, we have several people and one photocopier.
• Each person has a variety of items to photocopy - some have one page, others a few pages out of several books, and there are also those who want to copy an entire book.
• How should we allocate access to the photocopier?
![Page 11: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/11.jpg)
An individual represents a process• A variety of processes:
– short processes --- copying one page – long processes --- copying an entire book – CPU-bound processes (performing many
computations without interruption) • copying a sequence of pages from one book
– I/O-bound processes (performing only a few computations before an interruption occurs)
• copying a few pages each from several books, or single pages scattered throughout a single book
![Page 12: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/12.jpg)
FCFS (Batch)
Whoever arrived first gets to use the machine to make as many copies as desired.
![Page 13: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/13.jpg)
FCFS
• Non-preemptive
• Processes are assigned CPU in the order in which they request it.
• Easy to implement.– The ready queue is FIFO.
• What are its weaknesses?
![Page 14: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/14.jpg)
Process CPU burst
P1 24
P2 3
P3 3
P1 P2 P3
240 27 30
Average waiting Time = (0 + 24 + 27)/3 = 17 msec
If the processes arrive in order P2, P3, P1 we have:
P2 P3 P1
0 3 6 30
Average waiting Time = (0 + 3 + 6)/3 = 3 msec
FCFS
![Page 15: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/15.jpg)
SJF (Batch)
“3” goes next!
“5” “32” “28” “3” “31” “26” “11” “28”
dispatcher
![Page 16: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/16.jpg)
SJF
• Non-preemptive
• Processes are assigned the CPU on the basis of the length of their next CPU bursts– This algorithm should really be called “shortest
next burst”
• Theoretically optimal when all processes are available at the same time– SRT is the preemptive version of SJF
![Page 17: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/17.jpg)
SRT (Batch)
“2” goes next! “5” “32” “28” “3” “31” “26” “11” “28”
“2”
![Page 18: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/18.jpg)
Process Burst Time
P1 6
P2 8
P3 7
P4 3
P4 P1 P3 P2
0 3 9 16 24
Average waiting time = (0 + 3 + 9 + 16)/4 = 7 msec
With FCFS scheduling the average waiting time would be10.25 milliseconds - try it out!
SJF
![Page 19: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/19.jpg)
Process Arrival Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Non-preemptive:P1
0 8
P2
12
P4 P3
17 26
Average waiting time = (0 + 7 + 9 + 15)/4 = 7.75 msec
Preemptive:
0 1 5 10 17 26P1 P2 P4 P1 P3
Average waiting time = (0 + 0 + 2 + 9 + 15)/4 = 6.5 msec
SJF
![Page 20: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/20.jpg)
Three level scheduling (Batch)
Long-term Intermediate
Short-term
![Page 21: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/21.jpg)
Round Robin (interactive)N copies at a time!
Dispatcher
![Page 22: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/22.jpg)
Round-Robin
• Preemptive FCFS– A time slice or quantum q, 20 q 50 msec– The next process in the ready queue gets up to q
msec of CPU time. • If the CPU burst of the process < q, it voluntarily
relinquishes the CPU
• If it is > q, a timer goes off, the CPU is interrupted and the process is preempted and put at the end of the ready queue. The next process at the head of the queue is gets the CPU.
![Page 23: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/23.jpg)
Process Burst Time
P1 24
P2 3
P3 3
Time quantum is 4
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
Average waiting time = (0 + 4 + 7 + 6)/3 = 5.66 msec
Recall that for these same processes with no preemption the average waiting time was 17 msec
Round Robin
![Page 24: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/24.jpg)
RR performance depends on the size of the time quantum
• If it is very large, it is the same as having no preemption (i.e. FCFS)
• If it is very small, there are many context switches and valuable CPU time is spent swapping processes in and out
• A rule of thumb is that 80% of the CPU bursts should be shorter than the quantum
![Page 25: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/25.jpg)
Priority (interactive)
“2” goes next! “5” “32” “28” “3” “31” “26” “11” “28”
“200”
The boss goes next!
Boss
![Page 26: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/26.jpg)
Priority Scheduling• Some processes are deemed more important
than others – So, processes are assigned numbers indicating
their relative priority
• Preemptive or non-preemptive• Static or dynamic• SJF is a priority scheduling algorithm
– the length of the job determines the priority level.
• How can starvation be avoided?
![Page 27: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/27.jpg)
MLFQ (interactive)
“3” goes next!
max = 10
max = 20
max = 30
![Page 28: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/28.jpg)
MLFQ• Implemented as multiple level Round-Robin queues:
– the highest level has smallest quantum size
– a process enters the ready queue at the highest level; if it does not complete the first time it gets the CPU, it returns to the ready queue one level down
• I/O bound and interactive processes usually complete execution after one time in the CPU.
• CPU-bound processes that wait too long in lower priority queues may be promoted to prevent starvation
![Page 29: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/29.jpg)
MLFQ with four classes
q = 1
q = 2
q = 4
q = 8
![Page 30: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/30.jpg)
Multilevel Queue (hybrid)• Processes are placed in different queues
depending on their processing requirements– E.g., interactive jobs have different response time requirements
from batch jobs
• Each queue has its own scheduling algorithm. – E.g., the foreground (interactive) queue might be RR, the
background (batch) queue FCFS
• There is a scheduling algorithm among queues. – The foreground queue has priority over the background queue
• a background job is run only when the foreground queue is empty
![Page 31: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/31.jpg)
A multilevel system of queues
Batch processes
Interactive processes
System processes
• Each queue has absolute priority over lower queues• no batch process can run unless upper queues are empty
• If a higher priority process enters a queue while a batch process is running, the batch process may be preempted.
Interactive editing processes
![Page 32: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/32.jpg)
Parameters of a multilevel scheduler
– the number of queues– the scheduling algorithm for each queue– the method used to determine when to upgrade a
process to a higher priority queue– the method used to determine when to downgrade
a process to a lower priority queue– the method used to determine which queue a
process will enter initially
![Page 33: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/33.jpg)
The lowest levels may starve
• Instead of absolute priority, we can time slice between the queues. Each queue gets a certain amount of CPU time, which can then be scheduled among the processes– E.g., with two queues we can give 80% of CPU
time to the high priority queue, 20% to the secondary one
• This ensures that background jobs run
![Page 34: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/34.jpg)
Shortest Process Next
• The interactive version of SJF.
• Each command is considered as a process
• The necessary processing time for each command is estimated.
![Page 35: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/35.jpg)
Miscellaneous algorithms
• Guaranteed scheduling– With n users, each gets about 1/n of CPU power
• Lottery scheduling– Randomly distributed “lottery tickets” – Lottery may be held 50 times/second– Many variations
• Fair-share scheduling
![Page 36: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/36.jpg)
Real-Time Scheduling
• Time is an important factor in real-time systems– Data must be processed within a given time
frame or the system is worthless
• Program is divided into a number of processes whose behaviors are known in advance– They run to completion once they have the
CPU
![Page 37: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/37.jpg)
Separating scheduling mechanism from the scheduling policy
• A process knows which of its children are important and need priority– So, provide the mechanism in the kernel but
allow user processes to set policy among their child processes & threads
• Scheduling algorithm is parameterized– mechanism is in the kernel
• Parameters are filled in by user processes– policy is set by user process
![Page 38: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/38.jpg)
User-level thread scheduling
![Page 39: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/39.jpg)
Kernel-level thread scheduling
![Page 40: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/40.jpg)
Scheduling algorithm performance
• The only accurate way to evaluate a scheduling algorithm is to code it and see how it works
• This subjects the algorithm to the system’s actual conditions
• However, it is costly to rewrite the code & modify the operating system, and users must deal with a changing environment
![Page 41: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/41.jpg)
Windows NT CPU scheduling
• The scheduler runs in the kernel. • It is time-sliced (ie, round robin)
20 msec <= quantum <= 200 msecServers have 6 * quantum of workstations
• It is priority based – real time, high, normal, idle priority classes– Threads inherit their process priority & also have
relative priorities within their process.
• It is preemptive• MLFQ
– 32 queues, with absolute priority from top to bottom
![Page 42: Section 2.5](https://reader035.vdocuments.us/reader035/viewer/2022062314/56814a74550346895db78bcb/html5/thumbnails/42.jpg)
Linux CPU scheduling
• Threads are implemented at the kernel level– Scheduling is based on threads, not processes
• Three classes of threads– Real time FIFO– Real time round robin– Timesharing
• See pp. 708-709
Priority decreases downward