Chapter No:04 CPU Scheduling and Algorithms Total Marks:14: Mr. C. R. Ghuge
Chapter No:04 CPU Scheduling and Algorithms Total Marks:14: Mr. C. R. Ghuge
Total Marks:14
CO: Apply Scheduling Algorithms to calculate Turn Around Time and Average
Waiting Time
Basis for
heduling Non Preemptive Scheduling
Comparison
Once resources are allocated to a
The resources are allocated to a process, the process holds it till it
Basic
process for a limited time. completes its burst time or switches to
waiting state.
Process can be interrupted in Process can not be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process
If a process with long burst time is
frequently arrives in the ready
Starvation running CPU, then another process with
queue, low priority process may
less CPU burst time may starve.
starve.
Preemptive scheduling has
Non-preemptive scheduling does not
Overhead overheads of scheduling the
have overheads.
processes.
Flexibility Preemptive scheduling is flexible. Non-preemptive scheduling is rigid.
Preemptive scheduling is cost Non-preemptive scheduling is not cost
Cost
associated. associative.
For e.g.: Round Robin,
Example For e.g.: FCFS Algorithm
Priority Algorithms
Suppose that the processes arrive in the order: P1, P2, P3 Gantt Chart:
Advantage:
It is simple to implement.
Disadvantage:
This scheduling method is non preemptive, that is, the process will run until it
finishes. Because of this non preemptive scheduling, short processes which are at the
back of the queue have to wait for the long process at the front to finish.
It is not suitable for real time systems.
Average waiting time and average turnaround time is more comparatively.
Find the average waiting time and average turn arround time using FCFS algorithm?
Solution:
Therefore,
Gantt chart
P1 P2 P3
0 24 27 30
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
81 / 3 = 27 ms
In the above example, if order of process arriving is p2, p3, p1 instead of p1, p2, p3 then
Gantt chart
P2 P3 P1
0 3 6 30
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
39 / 3 = 13 ms
Gantt chart
P1 P2 P3 P4 P5
0 80 100 110 130 180
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
600/5 = 120 ms
Gantt chart
P1 P2 P3 P4
0 8 12 21 26
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
61/4 = 15.25 ms
Advantages-
SRTF is optimal and guarantees the minimum average waiting time.
It provides a standard for other algorithms since no other algorithm
performsbetter than it.
Disadvantages-
It can not be implemented practically since burst time of the processes
can notbe known in advance.
It leads to starvation for processes with larger burst time.
Priorities can not be set for the processes.
Processes with larger burst time have poor response time.
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 7 7–3=4 4–1=3
P2 16 16 – 1 = 15 15 – 4 = 11
P3 9 9–4=5 5–2=3
P4 6 6–0=6 6–6=0
P5 12 12 – 2 = 10 10 – 3 = 7
Now,
Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit
Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit
Solution-
Gantt Chart-
Now,
Consider the set of 6 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is shortest remaining time first, calculate the average
waiting time and average turnaround time.
Solution-
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Now,
Consider the set of 3 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SRTF, calculate the average waiting time and average
turn around time.
Solution-
Gantt
Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Now,
Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit
Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit
Consider the set of 4 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.
Solution-
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Thus,
Turn Around Time of process P2 = 55 – 15 = 40 unit
Waiting time of process P2 = 40 – 25 = 15 unit
The waiting time for the process having the highest priority will always be zero in
preemptive mode.
The waiting time for the process having the highest priority may not be zero in non-
preemptive mode.
Priority scheduling in preemptive and non-preemptive mode behaves exactly same under
following conditions-
The arrival time of all the processes is same
All the processes become available
Advantages-
It considers the priority of the processes and allows the important processes to
run first.
Priority scheduling in pre-emptive mode is best suited for real time operating
system.
Disadvantages-
Processes with lesser priority may starve for CPU.
There is no idea of response time and waiting time.
If the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
Solution- Gantt
Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 4 4–0=4 4–4=0
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5
Now,
Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4
Now,
Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 unit
Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit
Disadvantages-
It leads to starvation for processes with larger burst time as they have to repeatthe cycle
many times.
Its performance heavily depends on time quantum.
Priorities can not be set for the processes.
With increasing value of time quantum, Round Robin Scheduling tends tobecome
FCFS Scheduling.
When time quantum tends to infinity, Round Robin Scheduling becomes FCFS
Scheduling.
The performance of Round Robin scheduling heavily depends on the value of time
quantum.
The value of time quantum should be such that it is neither too big nor toosmall.
Solution-
Ready Queue- P5, P1, P2, P5, P4, P1, P3, P2, P1
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 13 13 – 0 = 13 13 – 5 = 8
P2 12 12 – 1 = 11 11 – 3 = 8
P3 5 5–2=3 3–1=2
P4 9 9–3=6 6–2=4
P5 14 14 – 4 = 10 10 – 3 = 7
Now,
Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 unit
Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit
Problem-02:
Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Gantt chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 8 8–0=8 8–4=4
P2 18 18 – 1 = 17 17 – 5 = 12
P3 6 6–2=4 4–2=2
P4 9 9–3=6 6–1=5
P5 21 21 – 4 = 17 17 – 6 = 11
P6 19 19 – 6 = 13 13 – 3 = 10
Now,
Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 / 6 = 10.84 unit
Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 = 7.33 unit
Problem-03: Consider the set of 6 processes whose arrival time and burst time aregiven
Solution-
Ready Queue- P3, P1, P4, P2, P3, P6, P1, P4, P2, P3, P5, P4
Gantt chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Process Id Exit time Turn Around time Waiting time
P1 32 32 – 5 = 27 27 – 5 = 22
P2 27 27 – 4 = 23 23 – 6 = 17
P3 33 33 – 3 = 30 30 – 7 = 23
P4 30 30 – 1 = 29 29 – 9 = 20
P5 6 6–2=4 4–2=2
P6 21 21 – 6 = 15 15 – 3 = 12
Now,
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Example:
Here the road is the resource required by cars and cars getting to the other side of the road can be
considered a process.
Now, none of the cars can move further and have entered a waiting state.
As it is a one-way road, both cars cannot move forward at the same time.
We can say that the road is a single instance resource (only one unit of resource is available).
List four Deadlock prevention condition and explain the following terms.
Deadlocks can be prevented by preventing at least one of the four required conditions:
Most prevention algorithms have poor resource utilization, and hence result in reduced
throughputs.
Instead, we can try to avoid deadlocks by making use prior knowledge about the usage of
resources by processes including resources available, resources allocated, future requests and
future releases by processes.
Most deadlock avoidance algorithms need every process to tell in advance the maximum number
of resources of each type that it may need.
Based on all this info we may decide if a process should wait for a resource or not and thus avoid
chances for circular wait.
Banker’s Algorithm:
This algorithm calculates resources allocated, required and available before allocating resources to
any process to avoid deadlock.
It contains two matrices on a dynamic basis. Matrix A contains resources allocated to different
processes at a given time.
Matrix B maintains the resources which are still required by different processes at the same time.
Algorithm F: Free resources
Step 1: When a process requests for a resource, the OS allocates it on a trial basis.
Deadlock Detection
Detection and Recovery: If deadlocks do occur, the operating system must detect and resolve them.
Deadlock detection algorithms, such as the Wait-For Graph, are used to identify deadlocks, and
recovery algorithms, such as the Rollback and Abort algorithm, are used to resolve them. The
recovery algorithm releases the resources held by one or more processes, allowing the system to
continue to make progress.