0% found this document useful (0 votes)
13 views

CPU Scheduling Complete

Uploaded by

0abubakar221
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

CPU Scheduling Complete

Uploaded by

0abubakar221
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

CPU SCHEDULING

INTRODUCTION

 In the uni-programmming systems like MS DOS, when a process waits for any I/O operation to be done, the CPU remains
idle.
 This is an overhead since it wastes the time and causes the problem of starvation.

 However, In Multiprogramming systems, the CPU doesn't remain idle during the waiting time of the Process and it starts
executing other processes. Operating System has to define which process the CPU will be given.
 In Multiprogramming systems, the Operating system schedules the processes on the CPU to have the maximum utilization
of it and this procedure is called CPU scheduling.
 The Operating System uses various scheduling algorithm to schedule the processes.

 This is a task of the short term scheduler to schedule the CPU for the number of processes present in the Job Pool.

 Whenever the running process requests some IO operation then the short term scheduler saves the current context of the
process (also called PCB) and changes its state from running to waiting.
 During the time, process is in waiting state; the Short term scheduler picks another process from the ready queue and assigns
the CPU to this process. This procedure is called context switching.
WHY DO WE NEED SCHEDULING?

 In Multiprogramming, if the long term scheduler picks more I/O bound processes then most of the time, the CPU remains
idle.
 The task of Operating system is to optimize the utilization of resources.

 If most of the running processes change their state from running to waiting then there may always be a possibility of
deadlock in the system.
 Hence to reduce this overhead, the OS needs to schedule the jobs to get the optimal utilization of CPU and to avoid the
possibility to deadlock.
THE PURPOSE OF A SCHEDULING ALGORITHM

 Maximum CPU utilization

 Fare allocation of CPU

 Maximum throughput

 Minimum turnaround time

 Minimum waiting time

 Minimum response time


SCHEDULING ALGORITHMS

 There are various algorithms which are used by the Operating System to schedule the processes on the processor in an
efficient way.
 First Come First Serve (FCFS)

 Round Robin

 Shortest Job First

 Shortest Remaining Time First

 Priority based scheduling


SCHEDULING ALGORITHMS
First Come First Serve
 It is the simplest algorithm to implement. The process with the minimal arrival time will get the CPU first.

 The lesser the arrival time, the sooner will the process gets the CPU. It is the non-preemptive type of scheduling.

Round Robin
 In the Round Robin scheduling algorithm, the OS defines a time quantum (slice).

 All the processes will get executed in the cyclic way.

 Each of the process will get the CPU for a small amount of time (called time quantum) and then get back to the ready
queue to wait for its next turn.
 It is a preemptive type of scheduling.
Shortest Job First
 The job with the shortest burst time will get the CPU first.

 The lesser the burst time, the sooner will the process get the CPU. It is the non-preemptive type of scheduling.
SCHEDULING ALGORITHMS
Shortest remaining time first
 It is the preemptive form of SJF.

 In this algorithm, the OS schedules the Job according to the remaining time of the execution.

Priority based scheduling


 In this algorithm, the priority will be assigned to each of the processes.

 The higher the priority, the sooner will the process get the CPU.

 If the priority of the two processes is same then they will be scheduled according to their arrival time.
Highest Response Ratio Next
 In this scheduling Algorithm, the process with highest response ratio will be scheduled next.

 This reduces the starvation in the system.


FCFS SCHEDULING
 First come first serve (FCFS) scheduling algorithm simply schedules the jobs according to their arrival time.

 The job which comes first in the ready queue will get the CPU first.

 The lesser the arrival time of the job, the sooner will the job get the CPU.

 FCFS scheduling may cause the problem of starvation if the burst time of the first process is the longest among all the
jobs.
Advantages of FCFS
 Simple

 Easy

 First come, First serv

Disadvantages of FCFS
 The scheduling method is non preemptive, the process will run to the completion.

 Due to the non-preemptive nature of the algorithm, the problem of starvation may occur.

 Although it is easy to implement, but it is poor in performance since the average waiting time is higher as compare to
other scheduling algorithms.
EXAMPLE

 Let's take an example of The FCFS scheduling algorithm.

 In the Following schedule, there are 5 processes with process ID P0, P1, P2, P3 and P4.

 P0 arrives at time 0, P1 at time 1, P2 at time 2, P3 arrives at time 3 and Process P4 arrives at time 4 in the ready queue.

 The processes and their respective Arrival and Burst time are given in the following table.

Process ID Arrival Burst Time Completion Turn Around Waiting


Time Time Time Time
0 0 2
1 1 6
2 2 4
3 3 9
4 6 12
GANTT CHART
CONVOY EFFECT IN FCFS
 FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all.

 As in the real life, if a convoy is passing through the road then the other persons may get blocked until it passes
completely.
 This can be simulated in the Operating System also.

 If the CPU gets the processes of the higher burst time at the front end of the ready queue then the processes of lower
burst time may get blocked which means they may never get the CPU if the job in the execution has a very high burst
time.
 This is called convoy effect or starvation.
EXAMPLE
 In this example, We have 3 processes named as P1, P2 and P3. The Burt Time of process P1 is highest.

 In the First scenario, The Process P1 arrives at the first in the queue although; the burst time of the process is the highest
among all.
 Since, the Scheduling algorithm, we are following is FCFS hence the CPU will execute the Process P1 first.

 In this schedule, the average waiting time of the system will be very high.

 That is because of the convoy effect.

 The other processes P2, P3 have to wait for their turn for 40 units of time although their burst time is very low.

 This schedule suffers from starvation.

Process ID Arrival Burst Time Completion Turn Waiting


Time Time Around Time
Time
1 0 40 40 40 0
2 1 3 43 42 39
3 1 1 44 43 42
GANTT CHART

 Avg waiting Time = 81/3


 In the Second scenario, If Process P1 would have arrived at the last of the queue and the other processes P2 and P3 at
earlier then the problem of starvation would not be there.
 Following example shows the deviation in the waiting times of both the scenarios.

 Although the length of the schedule is same that is 44 units but the waiting time will be lesser in this schedule.

Process ID Arrival Burst Time Completion Turn Waiting


Time Time Around Time
Time
1 1 40 44 43 3
2 0 3 3 3 0
3 0 1 4 4 3
 Avg Waiting Time=6/3
SHORTEST JOB FIRST (SJF) SCHEDULING

 So far, we were scheduling the processes according to their arrival time (in FCFS scheduling).

 However, SJF scheduling algorithm, schedules the processes according to their burst time.

 In SJF scheduling, the process with the lowest burst time, among the list of available processes in the ready queue, is
going to be scheduled next.
 However, it is very difficult to predict the burst time needed for a process hence this algorithm is very difficult to
implement in the system.
Advantages of SJF
 Maximum throughput

 Minimum average waiting and turnaround time

Disadvantages of SJF
 May suffer with the problem of starvation

 It is not implementable because the exact Burst time for a process can't be known in advance.
EXAMPLE

PID Arrival Time Burst Time Completion Turn Around Waiting Time
 In this example, there are five jobs named as P1, P2, P3, P4 and
TimeP5. Their arrival Time
time and burst time are given in the
table below.
1 1 7
2 3 3
3 6 2
4 7 10
5 9 8
EXAMPLE
 Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from time 0 to 1 (the time at which
the first process arrives).
 According to the algorithm, the OS schedules the process which is having the lowest burst time among the available
processes in the ready queue.
 Till now, we have only one process in the ready queue hence the scheduler will schedule this to the processor no matter what
is its burst time.
 This will be executed till 8 units of time. Till then we have three more processes arrived in the ready queue hence the
scheduler will choose the process with the lowest burst time.
 Among the processes given in the table, P3 will be executed next since it is having the lowest burst time among all the
available processes.
 So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.
GANNT CHART

 Avg Waiting Time = 27/5


SHORTEST REMAINING TIME FIRST (SRTF) SCHEDULING ALGORITHM

 This Algorithm is the preemptive version of SJF scheduling.

 In SRTF, the execution of the process can be stopped after certain amount of time.

 At the arrival of every process, the short term scheduler schedules the process with the least remaining burst time among
the list of available processes and the running process.
 Once all the processes are available in the ready queue, No preemption will be done and the algorithm will work as SJF
scheduling.
 The context of the process is saved in the Process Control Block when the process is removed from the execution and
the next process is scheduled.
 This PCB is accessed on the next execution of this process.
EXAMPLE
 In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their arrival time and burst time are given below in the
table.

Process Arrival Burst Completio Turn Waiting Response


ID Time Time n Time Around Time Time
Time
1 0 8
2 1 4
3 2 2
4 3 1
5 4 3
6 5 2
 Avg Waiting Time = 24/6
HOW IT WORKS

 Since, at time 0, the only available process is P1 with CPU burst time 8. This is the only available process in the list
therefore it is scheduled.
 The next process arrives at time unit 1. Since the algorithm we are using is SRTF which is a preemptive one, the current
execution is stopped and the scheduler checks for the process with the least burst time.
 Till now, there are two processes available in the ready queue. The OS has executed P1 for one unit of time till now; the
remaining burst time of P1 is 7 units.
 The burst time of Process P2 is 4 units. Hence Process P2 is scheduled on the CPU according to the algorithm.

 The next process P3 arrives at time unit 2. At this time, the execution of process P3 is stopped and the process with the
least remaining burst time is searched.
 Since the process P3 has 2 unit of burst time hence it will be given priority over others.
HOW IT WORKS
 The Next Process P4 arrives at time unit 3.

 At this arrival, the scheduler will stop the execution of P4 and check which process is having least burst time among the
available processes (P1, P2, P3 and P4). P1 and P2 are having the remaining burst time 7 units and 3 units respectively.
 P3 and P4 are having the remaining burst time 1 unit each. Since, both are equal hence the scheduling will be done
according to their arrival time. P3 arrives earlier than P4 and therefore it will be scheduled again.
 The Next Process P5 arrives at time unit 4. Till this time, the Process P3 has completed its execution and it is no more in
the list.
 The scheduler will compare the remaining burst time of all the available processes. Since the burst time of process P4 is 1
which is least among all hence this will be scheduled.
HOW IT WORKS

 The Next Process P6 arrives at time unit 5, till this time, the Process P4 has completed its execution.

 We have 4 available processes till now, that are P1 (7), P2 (3), P5 (3) and P6 (2).

 The Burst time of P6 is the least among all hence P6 is scheduled. Since, now, all the processes are available hence the
algorithm will now work same as SJF.
 P6 will be executed till its completion and then the process with the least remaining time will be scheduled.
ROUND ROBIN SCHEDULING ALGORITHM

 Round Robin scheduling algorithm is one of the most popular scheduling algorithm which can actually be implemented
in most of the operating systems.
 This is the preemptive version of first come first serve scheduling.

 The Algorithm focuses on Time Sharing.


 In this algorithm, every process gets executed in a cyclic way.

 A certain time slice is defined in the system which is called time quantum.
 Each process present in the ready queue is assigned the CPU for that time quantum, if the execution of the process is
completed during that time then the process will terminate else the process will go back to the ready queue and waits for
the next turn to complete the execution.
Advantages
 It can be actually implementable in the system because it is not depending on the burst time.

 It doesn't suffer from the problem of starvation or convoy effect.

 All the jobs get a fare allocation of CPU.

Disadvantages
 The higher the time quantum, the higher the response time in the system.

 The lower the time quantum, the higher the context switching overhead in the system.

 Deciding a perfect time quantum is really a very difficult task in the system.
RR SCHEDULING EXAMPLE

 In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6.
 Their arrival time and burst time are given below in the table.
 The time quantum of the system is 4 units.
 According to the algorithm, we have to maintain the ready queue and the Gantt chart.
Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units
PRIORITY SCHEDULING
 In Priority scheduling, there is a priority number assigned to each process.

 In some systems, the lower the number, the higher the priority.

 While, in the others, the higher the number, the higher will be the priority.
 The Process with the higher priority among the available processes is given the CPU.

 There are two types of priority scheduling algorithm exists. One is Preemptive priority scheduling while the other is Non
Preemptive Priority scheduling.
 The priority number assigned to each of the process may or may not vary.

 If the priority number doesn't change itself throughout the process, it is called static priority, while if it keeps changing
itself at the regular intervals, it is called dynamic priority.
NON PREEMPTIVE PRIORITY SCHEDULING
 In the Non Preemptive Priority scheduling, The Processes are scheduled according to the priority number assigned to
them.
 Once the process gets scheduled, it will run till the completion.

 Generally, the lower the priority number, the higher is the priority of the process.

Example
 In the Example, there are 7 processes P1, P2, P3, P4, P5, P6 and P7. Their priorities, Arrival Time and burst time are
given in the table.
 The Process P1 arrives at time 0 with the burst time of 3 units and the priority number 2.

 Since No other process has arrived till now hence the OS will schedule it immediately.

 Meanwhile the execution of P1, two more Processes P2 and P3 are arrived. Since the priority of P3 is 3 hence the CPU
will execute P3 over P2.
 Meanwhile the execution of P3, All the processes get available in the ready queue.

 The Process with the lowest priority number will be given the priority. Since P6 has priority number assigned as 4 hence
it will be executed just after P3.
 After P6, P4 has the least priority number among the available processes; it will get executed for the whole burst time.

 Since all the jobs are available in the ready queue hence All the Jobs will get executed according to their priorities.

 If two jobs have similar priority number assigned to them, the one with the least arrival time will be executed
 From the GANTT Chart, we can determine the completion time of every process, the turnaround time, waiting time and
response time will be determined.
 Avg Waiting Time = (0+11+2+7+12+2+18)/7 = 52/7 units

Process Id Priority Arrival Burst Completio Turnaroun Waiting Response


Time Time n Time d Time Time Time

1 2 0 3

2 6 2 5

3 3 1 4

4 5 4 2

5 7 6 9

6 4 5 4

7 10 7 10
PREEMPTIVE PRIORITY SCHEDULING

 In Preemptive Priority Scheduling, at the time of arrival of a process in the ready queue, its Priority is compared with the
priority of the other processes present in the ready queue as well as with the one which is being executed by the CPU at
that point of time.
 The One with the highest priority among all the available processes will be given the CPU next.

 The difference between preemptive priority scheduling and non preemptive priority scheduling is that, in the preemptive
priority scheduling, the job which is being executed can be stopped at the arrival of a higher priority job.
 Once all the jobs get available in the ready queue, the algorithm will behave as non-preemptive priority scheduling,
which means the job scheduled will run till the completion and no preemption will be done.
Example
 There are 7 processes P1, P2, P3, P4, P5, P6 and P7 given. Their respective priorities, Arrival Times and Burst times are
given in the table below.
Process Id Priority Arrival Time Burst Time

1 2(L) 0 1

2 6 1 7

3 3 2 3

4 5 3 6

5 4 4 5

6 10(H) 5 15

7 9 15 8
HOW IT WORKS

 At time 0, P1 arrives with the burst time of 1 units and priority 2. Since no other process is available hence this will be
scheduled till next job arrives or its completion (whichever is lesser).
 At time 1, P2 arrives. P1 has completed its execution and no other process is available at this time hence the Operating
system has to schedule it regardless of the priority assigned to it.
 The Next process P3 arrives at time unit 2, the priority of P3 is higher to P2. Hence the execution of P2 will be stopped
and P3 will be scheduled on the CPU.
 During the execution of P3, three more processes P4, P5 and P6 becomes available.

 Since, all these three have the priority lower to the process in execution so PS can't preempt the process.

 P3 will complete its execution and then P5 will be scheduled with the priority highest among the available processes.
 Meanwhile the execution of P5, all the processes got available in the ready queue.

 At this point, the algorithm will start behaving as Non Preemptive Priority Scheduling.

 Hence now, once all the processes get available in the ready queue, the OS just took the process with the highest priority
and execute that process till completion.
 In this case, P4 will be scheduled and will be executed till the completion.

 Since P4 is completed, the other process with the highest priority available in the ready queue is P2. Hence P2 will be
scheduled next.
 P2 is given the CPU till the completion. Since its remaining burst time is 6 units hence P7 will be scheduled after this.

 The only remaining process is P6 with the least priority, the Operating System has no choice unless of executing it. This
will be executed at the last.
GANNT CHART
 The Completion Time of each process is determined with the help of GANTT chart. The turnaround time and the waiting
time can be calculated by the following formula.
 Avg Waiting Time = (0+14+0+7+1+25+16)/7 = 63/7 = 9 units

Process Id Priority Arrival Burst Completio Turn Waiting


Time Time n Time around Time
Time

1 2 0 1
2 6 1 7
3 3 2 3
4 5 3 6
5 4 4 5
6 10 5 15
7 9 6 8

You might also like