CPU scheduling is the basis of multiprogrammed operating systems. By switching the CPU among processes, the operating system can make the computer more productive
- To introduce CPU scheduling, which is the basis for multiprogrammed operating systems
- To describe various CPU-scheduling algorithms
- To discuss evaluation criteria for selecting a CPU-scheduling algorithm for a particular system
- To examine the scheduling algorithms of several operating systems
3. 3
Scheduling Algorithm Optimization Criteria
• Max CPU utilization - keep CPU as busy as possible
• Max throughput - Number of processes that complete their execution per time unit
• Min turnaround time - amount of time to execute a particular process
• Min waiting time - amount of time that a process spends waiting in the ready queue
• Min response time - amount of time it takes from when a request was submitted
until the first response is produced, not output (for time-sharing environment)
4. Priority Scheduling
• A priority is associated with each process, and the CPUis allocated to the process
with the highest priority
• Priorities are generally indicated by some fixed range of numbers, such as 0 to 7 or 0
to 4,095
• There is no general agreement on whether 0 is the highest or lowest priority
• Some systems use low numbers to represent low priority; others use low numbers
for high priority
• In this text, we assume that low numbers represent high priority
• Equal-priority processes are scheduled in FCFS order
• SJF is a special case of the general priority scheduling, where priority is the inverse of
predicted next CPU burst time (the larger the CPU burst, the lower the priority, and
vice versa)
4
5. Example of Priority Scheduling
• Consider the following set of processes, assumed to have arrived at time 0 in the order
P1, P2, ···, P5, with the length of the CPU burst given in milliseconds:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
• Priority scheduling Gantt Chart
• Average waiting time = 8.2 msec
• Average turnaround time = (16+1+18+19+6)/5 =60/5=12msec
5
6. Priority Scheduling
• The CPU is allocated to the process with the highest priority (smallest integer
highest priority)
• Preemptive - When a process arrives at the ready queue, its priority is compared
with the priority of the currently running process.
A preemptive priority scheduling algorithm will preempt the CPU if the
priority of the newly arrived process is higher than the priority of the
currently running process
• Nonpreemptive - A nonpreemptive priority scheduling algorithm will simply put
the new process at the head of the ready queue
6
7. Example of Priority Scheduling - nonpremptive
• Consider the following set of processes, P1,P2, P3, P4, P5, with the length of the CPU
burst given in milliseconds. Compute and compare which among the processes provides
minimum waiting time and minimum turnaround time
Process Burst Time Priority Arrival Time
P1 18 2 0
P2 18 3 1
P3 5 4 2
P4 8 1 3
P5 10 5 4
• P4 provides 15ms (min waiting time)
• P1 provides 18ms(min turnaround time)
• Average waiting time = (0+25+42+15+45)/5=25.4 msec
• Average turnaround time = (18+43+47+23+55)/5=37.2msec
7
8. Example of Priority Scheduling - premptive
• Consider the following set of processes, P1,P2, P3, P4, P5, with the length of the CPU
burst given in milliseconds. Compute and compare which among the processes provides
minimum waiting time and minimum turnaround time
Process Burst Time Priority Arrival Time
P1 18 2 0
P2 18 1 5
P3 5 4 3
P4 8 3 8
P5 10 5 1
• P2 provides 0ms (min waiting time)
• P2 provides 18ms(min turnaround time)
• Average waiting time = (18+0+41+28+48)/5=27 msec
• Average turnaround time = (36+18+46+36+58)/5=38.8msec
8
9. Priority Scheduling
• Problem Starvation or Indefinite blocking – A process that is ready to run but
waiting for the CPU can be considered blocked. A priority scheduling algorithm can
leave some low priority processes waiting indefinitely. In a heavily loaded computer
system, a steady stream of higher-priority processes can prevent a low-priority
process from ever getting the CPU
• Solution Aging – as time progresses increase the priority of the process that wait
(ready queue) in the system for a long time
9
10. Round-Robin Scheduling
• The round-robin (RR) scheduling algorithm is designed especially for time sharing
systems
• It is similar to FCFS scheduling, but preemption is added to enable the system to
switch between processes
• A small unit of time, called a time quantum or time slice, is defined. A time quantum
is generally from 10 to 100 milliseconds in length
• The ready queue is treated as a circular queue
• The CPU scheduler goes around the ready queue, allocating the CPU to each process
for a time interval of up to 1 time quantum
• To implement RR scheduling, we again treat the ready queue as a FIFO queue of
processes (new processes are added to the tail of the ready queue)
10
11. Round-Robin Scheduling
• The CPU scheduler picks the first process from the ready queue, sets a timer to
interrupt after 1 time quantum, and dispatches the process
• The process may have a CPU burst of less than 1 time quantum
• In this case, the process itself will release the CPU voluntarily
• The scheduler will then proceed to the next process in the ready queue
• If CPU burst of the currently running process is longer than 1 time quantum
• The timer will go off and will cause an interrupt to the operating system
• A context switch will be executed, and the process will be put at the tail of the
ready queue
• The CPU scheduler will then select the next process in the ready queue
• The average waiting time under the RR policy is often long
11
12. Example of RR with Time Quantum = 4
• Consider the following set of processes that arrive at time 0, with the length of the
CPU burst given in milliseconds:
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:
• The average waiting time (10-4)+(4-0)+(7-0)/3 = 17/3 = 5.6
• The average turnaround time (30+7+10)/3 = 15.6
P P P
1 1 1
0 1
8 3
0
2
6
1
4
4 7 1
0 2
2
P
2
P
3
P
1
P
1
P
1
12
13. Round-Robin Scheduling
• In the RR scheduling algorithm, no process is allocated the CPU for more than 1 time
quantum in a row (unless it is the only runnable process). If a process’s CPU burst
exceeds 1 time quantum, that process is preempted and is put back in the ready
queue. The RR scheduling algorithm is thus preemptive
• 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. Each process
must wait no longer than (n − 1) × q time units until its next time quantum. For
example, with five processes and a time quantum of 20 milliseconds, each process
will get up to 20 milliseconds every 100 milliseconds
• The performance of the RR algorithm depends heavily on the size of the time
quantum (q)
• q large RR policy same as the FCFS policy (maintained by FIFO)
• q small RR approach can result in a large number of context switches,
otherwise overhead is too high
13
14. Round-Robin Scheduling
• The performance of the RR algorithm depends heavily on the size of the time
quantum (q)
• q large RR policy same as the FCFS policy (maintained by FIFO)
• q small RR approach can result in a large number of context switches,
otherwise overhead is too high
14
15. Round-Robin Scheduling
• Turnaround time also depends on the
size of the time quantum
• The average turnaround time of a set of
processes does not necessarily improve
as the time-quantum size increases
• In general, the average turnaround time
can be improved if most processes finish
their next CPU burst in a single time
quantum
15
80% of CPU bursts
should be shorter
than q
16. Round-Robin Scheduling
• The time quantum should be large compared with the context switch time, it should
not be too large
• As we pointed out earlier, if the time quantum is too large, RR scheduling
degenerates to an FCFS policy
• A rule of thumb is that 80 percent of the CPU bursts should be shorter than the time
quantum
16
17. Multilevel Queue Scheduling
• Scheduling algorithms has been created
for situations in which processes are
easily classified into different groups
• For example, common division is
made between
• foreground (interactive)
processes - high priority and
• background (batch) processes -
low priority
• A multilevel queue scheduling
algorithm partitions the ready queue
into several separate queues
17
18. Multilevel Queue Scheduling
• The processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority, or process type
• Each queue has its own scheduling algorithm
• The foreground queue might be scheduled by an RR algorithm
• The background queue is scheduled by an FCFS algorithm
• Scheduling must be done between the queues:
• Fixed priority scheduling; (i.e., serve all from foreground then from background).
Possibility of starvation.
• Time slice – each queue gets a certain amount of CPU time which it can schedule
amongst its processes; i.e., 80% to foreground in RR, 20% to background in FCFS
18
19. Multilevel Queue Scheduling
• Advantage:
• When the multilevel queue scheduling algorithm is used, processes are
permanently assigned to a queue when they enter the system
• If there are separate queues for foreground and background processes, for
example, processes do not move from one queue to the other, since processes do
not change their foreground or background nature
• This setup has the advantage of low scheduling overhead
• But it is inflexible
19
20. Multilevel Feedback Queue Scheduling
• The multilevel feedback queue scheduling algorithm, allows a process to move
between queues
• The idea is to separate processes according to the characteristics of their CPU bursts
• If a process uses too much CPU time, it will be moved to a lower-priority queue
• This scheme leaves I/O-bound and interactive processes in the higher-priority queues
• In addition, a process that waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation
• For example, consider a multilevel feedback queue scheduler with three queues,
numbered from 0 to 2
20
21. Example
• For example, consider a multilevel
feedback queue scheduler with three
queues, numbered from 0 to 2
• The scheduler first executes all
processes in queue 0. Only when queue
0 is empty will it execute processes in
queue 1. Similarly, processes in queue 2
will be executed only if queues 0 and 1
are empty
• A process that arrives for queue 1 will
preempt a process in queue 2. A process
in queue 1 will in turn be preempted by
a process arriving for queue 0
21
22. Example
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
• Q1 – RR with time quantum 16 milliseconds
• Q2 – FCFS
• Scheduling
• A new job enters queue Q0
• When it gains CPU, job receives 8
milliseconds
• If it does not finish in 8 milliseconds, job
is moved to queue Q1
• At Q1 job is again served and receives 16
additional milliseconds
• If it still does not complete, it is preempted and
moved to queue Q2. Processes in queue 2 are
run on an FCFS basis but are run only when
queues 0 and 1 are empty
22
23. Multilevel Feedback Queue Scheduling
• 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
23
24. Examples of FCFS with Arrival time
24
Consider the following set of
processes, with the length of the CPU
burst given in milliseconds. Compute
average waiting time and average
turnaround time
25. Examples of FCFS with Arrival time
25
Consider the following set of processes, with the length of the CPU burst given in
milliseconds. Compute average waiting time and average turnaround time
26. Examples of FCFS with Arrival time
26
Consider the following set of jobs, A, B, C, D, E, with the length of the CPU burst and
arrival time given in milliseconds. Compute average waiting time and average
turnaround time
27. Examples of FCFS with Arrival time
27
Consider the following set of processes, with the length of the CPU burst given in
milliseconds. Compute average waiting time and average turnaround time
28. 28
References
1. Silberscatnz and Galvin, “Operating System Concepts,” Ninth Edition, John
Wiley and Sons, 2012.
2. https://slidetodoc.com/cpu-scheduling-1-st-case-fcfs-first-come/