CPU Scheduling
CPU Scheduling
Utility of CPU
scheduler
CPU bound
process
I/O bound
process
Large number of short CPU bursts and small number of long CPU
bursts
Preemptive and non preemptive
CPU scheduler
Preemptive scheduling
Preemptive scheduling
Results in cooperative processes
Issues:
• Consider access to shared data
• Process synchronization
• Consider preemption while in kernel mode
• Updating the ready or device queue
• Preempted and running a “ps -el”
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Priority Scheduling
Performance evaluation
• Ideally many processes with several CPU and I/O bursts
P1 P2 P3
0 24 27 30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
• The Gantt chart for the schedule is:
P2 P3 P1
0 3 6 30
0 3 9 16 24
0 1 5 10 17 26
Avg waiting
• Average time
waiting time for non preemptive?
= [(10-1)+(1-1)+(17-2)+(5-3)]/4 = 26/4 = 6.5 msec
Determining Length of Next CPU Burst
• Estimation of the CPU burst length – should be similar
to the previous burst
• Then pick process with shortest predicted next CPU burst
• Estimation can be done by using the length of previous
CPU bursts, using time series analysis
1. t n actual length of n th CPU burst
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define : Boundary
n 1 tn 1 n . cases α=0, 1
• Commonly, α set to ½
Examples of Exponential Averaging
• =0
• n+1 = n
• Recent burst time 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
0 1 2 3 6
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) nice
• Set priority value
• Internal (time limit, memory req., ratio of I/O Vs CPU burst)
• External (importance, fund etc)
• SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
• Two types
• Preemptive
• Nonpreemptive
0 1 6 16 18 19
0 4 7 10 14 18 22 26 30
• No overhead
• However, poor
response time
• Too much
overhead!
• q must be large with respect to context switch,
• Slowing the
otherwise overhead is too high
execution time
• q usually 10ms to 100ms, context switch < 10 microsec
Effect on Turnaround Time
• TT depends on the time quantum and CPU burst time
• Better if most processes complete there next CPU burst in a
single q
• Large q=>
processes in ready
queue suffer
• Small q=>
Completion will
take more time
0 6 9 10 16 17
P1 P2 P3 P4 P4
(6+9+10+17)/4=10.5
q=6
Process classification
• Foreground process
• Interactive
• Frequent I/O request
• Requires low response time
• Background Process
• Less interactive
• Like batch process
• Allows high response time
• Can use different scheduling algorithms for two types
of processes ?
Multilevel Queue
• Ready queue is partitioned into separate queues, eg:
• foreground (interactive)
• background (batch)
• Process permanently assigned in a given queue
• Based on process type, priority, memory req.
Another possibility
• 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
Multilevel Feedback Queue
• So a process is permanently assigned a queue when
they enter in the system
• They do not move
• Flexibility!
• Multilevel-feedback-queue scheduling
• A process can move between the various queues;
• Separate processes based of the CPU bursts
• Process using too much CPU time can be moved to lower
priority
• Interactive process => Higher priority
• Move process from low to high priority
• Implement aging
Example of Multilevel Feedback Queue
Q0
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
Q1
• Q1 – RR time quantum 16 milliseconds
• Q2 – FCFS
Q2
• 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 receives 16 milliseconds
• If it still does not complete, it is preempted and moved to
queue Q2
Multilevel Feedback Queues