Ch-5 - CPU Scheduling
Ch-5 - CPU Scheduling
Ch-5 - CPU Scheduling
CS3500
PROF. SUKHENDU DAS DEPTT. OF COMPUTER SCIENCE AND ENGG., IIT
MADRAS, CHENNAI – 600036.
Email: [email protected]
URL: http://www.cse.iitm.ac.in/~vplab/os.html
Aug. – 2022.
CPU SCHEDULING
Basic Concepts
Maximum CPU utilization obtained with
multiprogramming
For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready
queue) must be selected for execution.
Max throughput
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
SJF is optimal – gives minimum average waiting time for a given set of
processes
P4 P1 P3 P2
0 3 9 16 24
Commonly, α set to ½
Prediction of the Length of the Next CPU Burst
Examples of Exponential Averaging
α =0
τn+1 = τn
Recent history does not count
α =1
τn+1 = α tn
Only the actual last CPU burst counts
If we expand the formula, we get:
τn+1 = α tn+(1 - α)α tn -1 + …
+(1 - α )j α tn -j + …
+(1 - α )n +1 τ0
Since both α and (1 - α) are less than or equal to 1,
each successor predecessor term has less weight than
its predecessor
Shortest Remaining Time First Scheduling
Preemptive version of SJN
P1 P2 P4 P1 P3
0 1 5 10 17 26
Average waiting time = [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5
Round Robin (RR)
Each process gets a small unit of CPU time (time quantum q), usually 10-100
milliseconds. After this time has elapsed, the process is preempted and added to
the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then 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.
Performance
q large FIFO (FCFS)
q small RR
Note that q must be large with respect to context switch, otherwise overhead is
too high
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
The CPU is allocated to the process with the highest priority (smallest
integer ≡ highest priority)
Preemptive
Nonpreemptive
An example of a multilevel queue scheduling algorithm with four queues, listed below in
order of priority:
1. Real-time processes
2. System processes
3. Interactive processes
4. Batch processes
No process in the batch queue, for example, could run unless the queues for real-time
processes, system processes, and interactive processes were all empty. If an interactive process
entered the ready queue while a batch process was running, the batch process would be preempted.
Time-slice among the queues: each queue gets a certain portion of the CPU time, which it
can then schedule among its various processes. For instance, in the foreground–background
queue example, the foreground queue can be given 80 percent of the CPU time for RR
scheduling, among its processes, while the background queue receives 20 percent of the CPU to
give to its processes on an FCFS basis.
Multilevel Feedback Queue
A process can move between the various queues. The idea is to separate
processes according to the characteristics of their bursts. If a process
uses too much CPU time, it will be moved to a lower-priority queue
Multilevel-feedback-queue scheduler defined by the following parameters:
Number of queues
Scheduling algorithms for each queue
Method used to determine when to upgrade 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
Aging can be implemented using multilevel feedback Q. To prevent
starvation, a process that waits too long in a lower-priority Q may
gradually be moved to a higher-priority Q.
Example of Multilevel Feedback Queue
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new process enters queue Q0 which is served
in RR
When it gains CPU, the process receives 8
milliseconds
If it does not finish in 8 milliseconds, the
process is moved to queue Q1
At Q1 job is again served in RR and receives 16
additional milliseconds
If it still does not complete, it is preempted
and moved to queue Q2