5. CPU Scheduler
Whenever the CPU becomes idle, the operating
system must select one of the processes in the
ready queue to be executed.
The selection process is carried out by the CPU
scheduler.
The ready queue may be ordered in various
ways
(Is ready Queue always follow FIFO Rule?? ).
7. CPU Scheduler
CPU scheduling decisions may take place when a
process:
1. Switches from running state to waiting state
2. Switches from running state to ready state
3. Switches from waiting state to ready state
4. When a process terminates
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.
There is a choice, however, for situations 2 and 3.
8. Non-preemptive Scheduling
• When scheduling takes place only under
circumstances 1 and 4.
• Under non-preemptive scheduling, once the CPU
has been allocated to a process, the process keeps
the CPU until it releases the CPU either by
terminating or by switching to the waiting state.
• It is efficient on hardware platforms.
9. Preemptive scheduling
When scheduling takes place only under circumstances
2 and 3
Preemptive scheduling can result in race conditions
when data are shared among several processes.
Problems in Preemptive scheduling:
Consider the case of two processes that share data. While one process is
updating the data, it is preempted so that the second process can run.
The second process then tries to read the data, which is in an
inconsistent state.
Consider preemption while in kernel mode
Consider interrupts occurring during crucial OS activities
10. Dispatcher
• Dispatcher module gives control of the
CPU to the process selected by the CPU
scheduler; this involves:
– switching context
– switching to user mode
– jumping to the proper location in the user
program to restart that program
• Dispatch latency – time it takes for the
dispatcher to stop one process and start
another running
11. Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – number of processes that complete
their execution per time unit (e.g., 5 per second)
• Turnaround time – amount of time to execute a
particular process
• Waiting time – total amount of time a process has
been waiting in the ready queue
• 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)
12. Common Terms used in Scheduling
• Arrival Time (AT)-(Submission time)
• Burst time (BT) -(CPU service time)
13. Optimization Criteria for Scheduling
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
15. Scheduling methods
• Non-preemptive:
– FCFS –First come first served
– SJF –shortest job first
– Priority scheduling
• Pre-emptive:
– SJFand SRTF
– Priority scheduling
– Round robin
– Multilevel queue
– Multilevel feed back queue
16. First- Come, First-Served (FCFS) Scheduling
• Consider the following three processes and their burst time
ProcessBurst Time
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 , P3
• We use Gantt Chart to illustrate a particular schedule
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
P P P
1 2 3
0 24 30
27
17. 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
– Consider one CPU-bound and many I/O-bound processes
P1
0 3 6 30
P2
P3
20. Shortest-Job-First (SJF)
• Associate with each process the length of its
next CPU burst
– Use these lengths to schedule the process with the
shortest time
• SJF is optimal – gives minimum average waiting
time for a given set of processes
– How do we know what is the length of the next CPU
request
– Could ask the user
• what if the user lies?
21. 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
16
9
P2
Consider the following four processes and their burst time
23. Determining Length of Next CPU Burst
• Can only estimate (predict) the length – in
most cases should be similar to the previous
CPU burst
– Pick the process with shortest predicted next CPU
burst
• Can be done by using the length of previous
CPU bursts, using exponential averaging
• Commonly, α set to ½
:
Define
4.
1
0
,
3.
burst
CPU
next
the
for
value
predicted
2.
burst
CPU
of
length
actual
1.
1
n
th
n n
t
.
1
1 n
n
n t
27. Q: SJRF
• Consider three processes, all arriving at time zero, with
total execution time (CPU and I/O) of 10, 20 and 30 units,
respectively.
• Each process spends the first 20% of execution time doing
I/O, the next 70% of time doing computation, and the last
10% of time doing 1/0 again.
• The operating system uses a SJF scheduling algorithm and
schedules a new process either when the running process
gets blocked on I/O or when the running process finishes its
compute burst.
• Assume that all I/O operations can be overlapped as much
as possible. For what percentage of time does the CPU
remain idle?
28. ??
Consider the following table of arrival time and burst time for
three processes P0, P1 and P2.
Process Arrival time Burst Time
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The shortest job first scheduling algorithm is used. Scheduling
is carried out only at arrival or completion of processes. What
is the average waiting time for the three processes?
Answer 5ms
29. ??
• Consider the following set of processes, with the arrival
times and the CPU-burst times given in milliseconds
Process Arrival Time Burst Time
P1 0 5
P2 1 3
P3 2 3
P4 4 1
• What is the average turnaround time for these
processes with the preemptive shortest remaining
processing time first (SRPT) algorithm ?
Answer 5.5
30. *??*
Consider the following table (Assume Non Preemptive scheduling):
Process Arrival Time Burst Time
P1 0.0 8
P2 0.4 4
P3 1.0 1
a. Give the Gantt Chart & Calculate average turnaround time using
i) FCFS scheduling
ii) SJF scheduling
b. Give the Gantt Chart & Compute average turnaround time if the
CPU is left idle for the first 1 unit
and then SJF scheduling is used. (Remember that processes P1 and P2
are waiting during this idle time, so their waiting time may increase.)
31. Round Robin (RR)
• Designed for time-sharing systems
• Each process gets a small unit of CPU time (time
quantum q).
• 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.
• Timer (hardware support) interrupts every quantum
to schedule next process
32. Round Robin (RR)
• *Process may use less than a full time slice*
• Performance
– q large FIFO
– q small q must be large with respect to context
switch, otherwise overhead is too high
34. Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
• The Gantt chart is:
• The average waiting time under the RR policy is often longer
• Typically, higher average turnaround than SJF, but better
response
• q should be large compared to context switch time
• q is usually 10ms to 100ms, context switch < 10 usec
P P P
1 1 1
0 18 30
26
14
4 7 10 22
P2
P3
P1
P1
P1
Consider the following three processes and their burst time
35. Time Quantum and Context Switch Time
• The performance of the RR algorithm depends on the size of
the time quantum. If the time quantum is extremely small
(say, 1 millisecond), RR can result in a large number of
context switches.
36. Turn Around Time in RR
• Increase in quantum time -less frequent
executions of switching –lesser turn around time
• Average turn around time (TT) does not always
improve with increase in quantum time
• TT can be improved if most processes can finish
their jobs in one quantum
• With 3 processes of 10 ms each, and time slice =
1 ms, average turn around time = 29 ms
(excluding switching time)
• If time slice = 10, average turn around time = 20
ms (plus switching time)
37. Turnaround Time Varies With 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.
39. *??*
Consider the set of processes where processes are arrived in the order P1, P2,
P3, P4 all at time to.
Process Burst Time (milliseconds) Priority
P1 20 2
P2 2 1
P3 4 2
P4 2 3
a. Find the total number of context switches present in Non Preemptive
Priority & RR scheduling (Time Slice=2). Diagram Required.
b. When the behavior of RR scheduling become equivalent to FCFS. Explain. In
above mentioned case find out the time quantum when RR & FCFS will be
equivalent.
41. Priority Scheduling
• A priority number (integer) is associated with each
process
• The CPU is allocated to the process with the highest
priority (smallest integer highest priority)
– Preemptive
– Nonpreemptive
• SJF is priority scheduling where priority is the inverse
of predicted next CPU burst time
• Problem Starvation – low priority processes may
never execute
• Solution Aging – as time progresses increase the
priority of the process
42. Example of Priority Scheduling
ProcessAarri Burst TimeTPriority
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
1
0 1 19
P1
P2
16
P4
P3
6 18
P
Consider the following five processes and their burst time
43. Combining Priority Scheduling and RR
ProcessA arri Burst TimeT Priority
P1 4 3
P2 5 2
P3 8 2
P4 7 1
P5 3 3
• Priority scheduling Gantt Chart
• Average waiting time = 8.2 msec
System executes the highest priority process; processes with the same
priority will be run using round-robin.
Consider the following five processes and their burst time
45. We wish to schedule three processes PI, P2 and P3 on a
uniprocessor system. The priorities, CPU time requirements and
arrival times of the processes are as shown below.
Process Priority CPU time required Arrival time (hh:mm:ss)
• PI 10(highest) 20 sec 00:00:05
• P2 9 10 sec 00:00:03
• P3 8 (lowest) 15 sec 00:00:00
We have a choice of preemptive or non-preemptive scheduling. In
preemptive scheduling, a late-arriving higher priority process can
preempt a currently running process with lower priority. In non-
preemptive scheduling, a late-arriving higher priority process
must wait for the currently executing process to complete before
it can be scheduled on the processor. What are the turnaround
times (time from arrival till completion) of P2 using preemptive
and non-preemptive scheduling respectively.
??
47. Multilevel Queue
• Ready queue is partitioned into separate queues, eg:
– foreground (interactive)
– background (batch)
• Process permanently in a given queue
• Each queue has its own scheduling algorithm:
– foreground – RR
– background – FCFS
• 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
50. Multilevel Feedback Queue
• A process can move between the various
queues; aging can be implemented this way
• 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
51. 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 job enters queue Q0 which is served
FCFS
• 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 FCFS and receives
16 additional milliseconds
• If it still does not complete, it is
preempted and moved to queue Q2
53. What scheduling policy will you use for each of
the following cases? Explain your reasons for
choosing them.
a. The processes arrive at large time intervals:
b. The system’s efficiency is measured by the
percentage of jobs completed.
c. All the processes take almost equal amounts
of time to complete.
55. • In which of the following operations, the
scheduler is not called into play?
a. Process requests for I/O.
b. Process finishes execution.
c. Process finishes its time allotted.
d. All of the above
e. None of the above
Answer D
56. • What are the factors that need to be
considered to determine the degree of
multiprogramming in a system?
Ans: The two factors that need to be considered
are:
1. The overheads in context switching may
become excessive.
2. With excessive multiprogramming the
response times may become unacceptable.
57. • What happens if the time allocated in a
Round Robin Scheduling is very large? And
what happens if the time allocated is very
low?
Ans: It results in a FCFS scheduling. If time is too
low, the processor through put is reduced. More
time is spent on context switching
58. • What is the difference between the idle and
blocked state of a process?
Ans: In idle state, the process is waiting for the
processor to become free so that it can execute.
In blocked state, the process has been put out
from the running state by the processor due to
some I/O.
59. • Put the following in the chronological order
in the context of the birth of a process
executes: Ready, suspended, execute,
terminate, create.
Ans: Create, Ready, Execute, Suspended,
Terminate
60. • A Shortest Job First algorithm may lead to
starvation where a process with large execution
time is made to wait for indefinitely long times.
Suggest a modification to the SJF that
overcomes this problem.
Ans: A clock value (arrival time) is stored for each
process. This helps to determine the priority of a
process as a function of execution time and the
clock value.
61. • If the waiting time for a process is p and
there are n processes in the memory then the
CPU utilization is given by,
a. p/n
b. p^n (p raised to n)
c. 1-p^n
d. n-(p^n)
Ans: p^n
62. *??*
• Suppose a new process in a system arrives at
an average of six processes per minute and
each such process requires an average of 8
seconds of service time. Estimate the fraction
of time the CPU is busy in a system with a
single processor.
63. • Given that there are on an average 6 processes
per minute. So the arrival rate = 6 process/min.
i.e. every 10 seconds a new process arrives on an
average. or we can say that every process stays
for 10 seconds with the CPU Service time = 8 sec.
• Hence the fraction of time CPU is busy = service
time / staying time
=8 / 10 =0.8
So the CPU is busy for 80% of the time.