Chapter Five
CPU Scheduling
Introduction
 CPU scheduling is the basis of multi programmed operating systems. By switching the
CPU among processes, the operating system can make the computer more productive
 When a computer is multi-programmed, it frequently has multiple processes computing
for the CPU at the same time.
 This situation occurs whenever two or more processes are simultaneously in the ready
state.
 If only one CPU is available, a choice has to be made which process to run next.
 The part of the operating system that makes the choice is called the scheduler and the
algorithm it uses is called the scheduling algorithm.
2
CPU Scheduling
• Back in the old days of batch systems with input in the form of card images on a
magnetic tape, the scheduling algorithm was simple: just run the next job on the tape.
• With timesharing systems, the scheduling algorithm became more complex because
there were generally multiple users waiting for service.
3
Cont’d..
 Operating system uses two types of scheduling processes execution.
• Preemptive and Non-preemptive scheduling
• Preemptive scheduling is a CPU scheduling technique that works by dividing time
slots of CPU to a given process.
• In preemptive scheduling the CPU is allocated to the processes for the limited time.
• Non-preemptive scheduling is a CPU scheduling technique that process takes the
resource (CPU time) and holds it till the process gets terminated or is pushed to the
waiting state. No process is interrupted until it is completed.
4
Cont’d..
Preemptive scheduling
• Resources are allocated according to
the cycles for a limited time.
• The process can be interrupted, even
before the completion.
• Maintaining queue and remaining time
needs storage overhead.
• Algorithms are: RR, priority and SRTF
Non-preemptive scheduling
• Resources are used and then held by
the process until it gets terminated
• The process is not interrupted until its
lifecycle is complete.
• No such overheads are required.
• Algorithms are: non-preemptive
priority and SJF
5
CPU Scheduling :- Scheduling Criteria
 Different CPU scheduling algorithms have different properties and may favor one class of processes over another.
 In choosing which algorithm to use in a particular situation, we must consider the properties of the various
algorithms.
 Many criteria have been suggested for comparing CPU scheduling algorithms.
 Criteria that are used include the following:
◦ CPU utilization - keep the CPU as busy as possible
◦ Throughput - number of processes that complete their execution per time unit
◦ Turnaround time - amount of time to execute a particular process
◦ Waiting time - 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)
◦ Fairness – comparable processes should get comparable services (gives each process a fair share of the CPU).
6
CPU Scheduling:- Optimization
Max CPU utilization
Max throughput
Minimum turnaround time
Minimum waiting time
Minimum response time
7
1. First-Come, First-Served (FCFS) Scheduling
 The simplest of all scheduling algorithm is non-preemptive FCFS scheduling
algorithm.
 Processes are assigned the CPU in the order they request it.
 When the first job enters the system from the outside in the morning, it is started
immediately and allowed to run as long as it wants too.
 As other jobs come in, they are put onto the end of the queue.
 When the running process blocks, the first process on the queue is run next.
 When a blocked process becomes ready, like a newly arrived job, it is put on the end
of the queue.
8
CPU Scheduling
 Example:
 Process Burst time
 P1 = 24, P2 = 3, P3 = 3
 Suppose that the processes arrive in the order: P1, P2, P3.
 A diagram to show this schedule is:
9
Waiting time for:
P1 = 0
P2 = 24
P3 = 27
Average waiting time: (0 + 24 + 27)/3 = 17
2. Shortest Job First (SJF) Scheduling
 It is a non preemptive batch algorithm that assumes the run times are known in
advance.
 Associate with each process the length of its next CPU burst. Use these lengths to
schedule the process with the shortest time.
 When several equally important jobs are sitting in the input queue waiting to be
started, the scheduler picks the shortest job first.
10
CPU Scheduling
11
CPU Scheduling
12
Shortest Job First is provably Optimal.. It is worth pointing out that shortest job first is
only optimal when all the jobs are available simultaneously. E.g. five processes having
run times of 2, 4, 1, 1, 1 respectively. Their arrival times are 0, 0,3,3, and 3. check the
difference of the average turn around time if they run in the order of A, B, C, D and E
and in the order of B, C, D, E, A.
3. Shortest remaining time next
• A preemptive version of shortest job first is shortest remaining time next. With this
algorithm, the scheduler always chooses the process whose remaining run time is the
shortest. Again here, the run time has to be known in advance. When a new job
arrives, its total time is compared to the current process’ remaining time. If the new
job needs less time to finish than the current process, the current process is suspended
and the new job started. This scheme allows new short jobs to get good service.
•
13
Process Arrival time CPU time
P1 0 2
P2 2 2
P3 4 3
P4 5 4
P1 P2 P3 P4
0 2 4 7 11
Aw ?
4. Round-Robin (RR)Scheduling
 One of the oldest, simplest, fairest, and mostly used algorithm. Each process is assigned a
time interval called its quantum, which it is allowed to run.
 If the process is still running at the end of the quantum, the CPU is preempted and given to
another process.
 It is easy to implement, all the scheduler needs to do is maintain a list of runnable processes.
 When the process uses up its quantum, it is put on the end of the list.
 The only interesting issue with round robin is the length of the quantum
14
Cont’d..
15
Fig. Round-Robin scheduling (a) the list of runnable processes. (b) the list of
runnable processes after B uses up its quantum.
Setting the quantum too short causes too many processes switches and lowers the CPU
efficiency. But setting it too long may cause poor response to short interactive request. A
quantum around 20-50 msec. is often a reasonable compromise
CPU Scheduling
Example: with time quantum = 20
16
5. Priority Scheduling
Round-Robin scheduling makes the implicit assumption that all processes are equally important.
 The need to take external factors into account leads to priority scheduling.
 The basic idea is each process is assigned a priority, and the runnable process with the highest
priority is allowed to run.
 (smallest integer means highest priority).
 It can be preemptive or non-preemptive.
Problem: starvation (or indefinite blocking) – low priority processes may never execute.
Solution: aging as time progresses increase the priority of the process.
17
Summary
 FCFS simple but causes short jobs to wait for long jobs.
 SJF is optimal giving shortest waiting time but need to know length of next burst.
 SJF is a type of priority scheduling - may suffer from starvation - prevent using aging
 RR is gives good response time, it is preemptive. Problem selecting the quantum.
 Multiple queue Algorithms use the best of each algorithm by having more than one
queue. Feedback queues allow jobs to move from queue to queue.
18
Exercise
Suppose that the ff jobs arrive as indicated for scheduling and execution on a single CPU.
1. Draw a Gantt chart showing FCFS and calculate Avg. wait. time
2. Draw a Gantt chart showing non preemptive SJF and calculate Avg. wait. Time
3. Draw a Gantt chart showing SRTF and calculate Avg. wait. Time
4. Draw a Gantt chart showing RR (q=4 msec.) and calculate Avg. wait. Time
19
Job Arrival time Size (msec.) Priority
J1 0 12 1(Gold)
J2 2 4 3(Bronze)
J3 5 2 1(Gold)
J4 8 10 3(Bronze)
J5 10 6 2(Silver)

chapter 5 CPU scheduling.ppt

  • 1.
  • 2.
    Introduction  CPU schedulingis the basis of multi programmed operating systems. By switching the CPU among processes, the operating system can make the computer more productive  When a computer is multi-programmed, it frequently has multiple processes computing for the CPU at the same time.  This situation occurs whenever two or more processes are simultaneously in the ready state.  If only one CPU is available, a choice has to be made which process to run next.  The part of the operating system that makes the choice is called the scheduler and the algorithm it uses is called the scheduling algorithm. 2
  • 3.
    CPU Scheduling • Backin the old days of batch systems with input in the form of card images on a magnetic tape, the scheduling algorithm was simple: just run the next job on the tape. • With timesharing systems, the scheduling algorithm became more complex because there were generally multiple users waiting for service. 3
  • 4.
    Cont’d..  Operating systemuses two types of scheduling processes execution. • Preemptive and Non-preemptive scheduling • Preemptive scheduling is a CPU scheduling technique that works by dividing time slots of CPU to a given process. • In preemptive scheduling the CPU is allocated to the processes for the limited time. • Non-preemptive scheduling is a CPU scheduling technique that process takes the resource (CPU time) and holds it till the process gets terminated or is pushed to the waiting state. No process is interrupted until it is completed. 4
  • 5.
    Cont’d.. Preemptive scheduling • Resourcesare allocated according to the cycles for a limited time. • The process can be interrupted, even before the completion. • Maintaining queue and remaining time needs storage overhead. • Algorithms are: RR, priority and SRTF Non-preemptive scheduling • Resources are used and then held by the process until it gets terminated • The process is not interrupted until its lifecycle is complete. • No such overheads are required. • Algorithms are: non-preemptive priority and SJF 5
  • 6.
    CPU Scheduling :-Scheduling Criteria  Different CPU scheduling algorithms have different properties and may favor one class of processes over another.  In choosing which algorithm to use in a particular situation, we must consider the properties of the various algorithms.  Many criteria have been suggested for comparing CPU scheduling algorithms.  Criteria that are used include the following: ◦ CPU utilization - keep the CPU as busy as possible ◦ Throughput - number of processes that complete their execution per time unit ◦ Turnaround time - amount of time to execute a particular process ◦ Waiting time - 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) ◦ Fairness – comparable processes should get comparable services (gives each process a fair share of the CPU). 6
  • 7.
    CPU Scheduling:- Optimization MaxCPU utilization Max throughput Minimum turnaround time Minimum waiting time Minimum response time 7
  • 8.
    1. First-Come, First-Served(FCFS) Scheduling  The simplest of all scheduling algorithm is non-preemptive FCFS scheduling algorithm.  Processes are assigned the CPU in the order they request it.  When the first job enters the system from the outside in the morning, it is started immediately and allowed to run as long as it wants too.  As other jobs come in, they are put onto the end of the queue.  When the running process blocks, the first process on the queue is run next.  When a blocked process becomes ready, like a newly arrived job, it is put on the end of the queue. 8
  • 9.
    CPU Scheduling  Example: Process Burst time  P1 = 24, P2 = 3, P3 = 3  Suppose that the processes arrive in the order: P1, P2, P3.  A diagram to show this schedule is: 9 Waiting time for: P1 = 0 P2 = 24 P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17
  • 10.
    2. Shortest JobFirst (SJF) Scheduling  It is a non preemptive batch algorithm that assumes the run times are known in advance.  Associate with each process the length of its next CPU burst. Use these lengths to schedule the process with the shortest time.  When several equally important jobs are sitting in the input queue waiting to be started, the scheduler picks the shortest job first. 10
  • 11.
  • 12.
    CPU Scheduling 12 Shortest JobFirst is provably Optimal.. It is worth pointing out that shortest job first is only optimal when all the jobs are available simultaneously. E.g. five processes having run times of 2, 4, 1, 1, 1 respectively. Their arrival times are 0, 0,3,3, and 3. check the difference of the average turn around time if they run in the order of A, B, C, D and E and in the order of B, C, D, E, A.
  • 13.
    3. Shortest remainingtime next • A preemptive version of shortest job first is shortest remaining time next. With this algorithm, the scheduler always chooses the process whose remaining run time is the shortest. Again here, the run time has to be known in advance. When a new job arrives, its total time is compared to the current process’ remaining time. If the new job needs less time to finish than the current process, the current process is suspended and the new job started. This scheme allows new short jobs to get good service. • 13 Process Arrival time CPU time P1 0 2 P2 2 2 P3 4 3 P4 5 4 P1 P2 P3 P4 0 2 4 7 11 Aw ?
  • 14.
    4. Round-Robin (RR)Scheduling One of the oldest, simplest, fairest, and mostly used algorithm. Each process is assigned a time interval called its quantum, which it is allowed to run.  If the process is still running at the end of the quantum, the CPU is preempted and given to another process.  It is easy to implement, all the scheduler needs to do is maintain a list of runnable processes.  When the process uses up its quantum, it is put on the end of the list.  The only interesting issue with round robin is the length of the quantum 14
  • 15.
    Cont’d.. 15 Fig. Round-Robin scheduling(a) the list of runnable processes. (b) the list of runnable processes after B uses up its quantum. Setting the quantum too short causes too many processes switches and lowers the CPU efficiency. But setting it too long may cause poor response to short interactive request. A quantum around 20-50 msec. is often a reasonable compromise
  • 16.
    CPU Scheduling Example: withtime quantum = 20 16
  • 17.
    5. Priority Scheduling Round-Robinscheduling makes the implicit assumption that all processes are equally important.  The need to take external factors into account leads to priority scheduling.  The basic idea is each process is assigned a priority, and the runnable process with the highest priority is allowed to run.  (smallest integer means highest priority).  It can be preemptive or non-preemptive. Problem: starvation (or indefinite blocking) – low priority processes may never execute. Solution: aging as time progresses increase the priority of the process. 17
  • 18.
    Summary  FCFS simplebut causes short jobs to wait for long jobs.  SJF is optimal giving shortest waiting time but need to know length of next burst.  SJF is a type of priority scheduling - may suffer from starvation - prevent using aging  RR is gives good response time, it is preemptive. Problem selecting the quantum.  Multiple queue Algorithms use the best of each algorithm by having more than one queue. Feedback queues allow jobs to move from queue to queue. 18
  • 19.
    Exercise Suppose that theff jobs arrive as indicated for scheduling and execution on a single CPU. 1. Draw a Gantt chart showing FCFS and calculate Avg. wait. time 2. Draw a Gantt chart showing non preemptive SJF and calculate Avg. wait. Time 3. Draw a Gantt chart showing SRTF and calculate Avg. wait. Time 4. Draw a Gantt chart showing RR (q=4 msec.) and calculate Avg. wait. Time 19 Job Arrival time Size (msec.) Priority J1 0 12 1(Gold) J2 2 4 3(Bronze) J3 5 2 1(Gold) J4 8 10 3(Bronze) J5 10 6 2(Silver)