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
Section 2.5
CPU Scheduling
CPU scheduling goals
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.
CPU scheduling and process states
new terminated
ready running
waiting
cpu sched
I/O or event waitI/O or event complete
interruptjob sched
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
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
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?
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?
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
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?
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
FCFS (Batch)
Whoever arrived first gets to use the machine to make as many copies as desired.
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?
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
SJF (Batch)
“3” goes next!
“5” “32” “28” “3” “31” “26” “11” “28”
dispatcher
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
SRT (Batch)
“2” goes next! “5” “32” “28” “3” “31” “26” “11” “28”
“2”
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
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
Three level scheduling (Batch)
Long-term Intermediate
Short-term
Round Robin (interactive)N copies at a time!
Dispatcher
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.
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
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
Priority (interactive)
“2” goes next! “5” “32” “28” “3” “31” “26” “11” “28”
“200”
The boss goes next!
Boss
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?
MLFQ (interactive)
“3” goes next!
max = 10
max = 20
max = 30
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
MLFQ with four classes
q = 1
q = 2
q = 4
q = 8
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
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
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
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
Shortest Process Next
• The interactive version of SJF.
• Each command is considered as a process
• The necessary processing time for each command is estimated.
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
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
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
User-level thread scheduling
Kernel-level thread scheduling
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
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
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