Unit IV OSY Handout Revised 07.08.2023
Unit IV OSY Handout Revised 07.08.2023
Pre-emptive Scheduling:
Pre-emptive scheduling is used when a process switches from running state to ready
state or from waiting state to ready state.
The resources (mainly CPU cycles) are allocated to the process for the limited amount
of time and then is taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining.
That process stays in ready queue till it gets next chance to execute.
Non-Pre-emptive Scheduling:
Non-pre-emptive Scheduling is used when a process terminates, or a process switches
from running to waiting state.
In this scheduling, once the resources (CPU cycles) is allocated to a process, the process
holds the CPU till it gets terminated or it reaches a waiting state.
In case of non-pre-emptive scheduling does not interrupt a process running CPU in
middle of the execution.
Instead, it waits till the process complete its CPU burst time and then it can allocate the
CPU to another process.
Basis for
Pre-emptive Scheduling Non Pre-emptive 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 cannot be interrupted till it
Interrupt
between. terminates or switches to waiting state.
If a high priority process frequently If a process with long burst time is
Starvation arrives in the ready queue, low running CPU, then another process with
priority process may starve. less CPU burst time may starve.
Pre-emptive scheduling has
Non-pre-emptive scheduling does not
Overhead overheads of scheduling the
have overheads.
processes.
Flexibility Pre-emptive scheduling is flexible. Non-pre-emptive scheduling is rigid.
Pre-emptive scheduling is cost Non-pre-emptive scheduling is not cost
Cost
associated. associative.
Scheduling Criteria
There are several different criteria to consider when trying to select the "best"
scheduling algorithm for a particular situation and environment, including:
o CPU utilization - Ideally the CPU would be busy 100% of the time, so as to
waste 0 CPU cycles. On a real system CPU usage should range from 40%
( lightly loaded ) to 90% ( heavily loaded. )
o Throughput - Number of processes completed per unit time. May range from
10 / second to 1 / hour depending on the specific processes.
Example 1:
Example 2:
Consider the processes P1, P2, P3 given in the below table, arrives for execution in
the same order, with Arrival Time 0, and given Burst Time,
PROCESS ARRIVAL TIME BURST TIME
P1 0 24
P2 0 3
P3 0 3
Gantt chart
P1 P2 P3
0 24 27 30
Average Waiting Time = (Total Wait Time) / (Total number of processes) = 51/3 = 17 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
= 81 / 3 = 27 ms
Throughput = 3 jobs/30 sec = 0.1 jobs/sec
Example 3:
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution
in the same order, with given Arrival Time and Burst Time.
PROCESS ARRIVAL TIME BURST TIME
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt chart
P1 P2 P3 P4
0 8 12 21 26
Average Waiting Time = (Total Wait Time) / (Total number of processes)= 35/4 = 8.75 ms
Average Turn Around time = (Total Turn Around Time) / (Total number of processes)
61/4 = 15.25 ms
Solution-
If the CPU scheduling policy is SJF non-preemptive, calculate the average waiting
time and average turnaround time.
Gantt Chart-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time
Example-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
If the CPU scheduling policy is SJF pre-emptive, calculate the average waiting time and
average turnaround time.
Solution-
Gantt Chart-
Now,
Example-03:
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,
Average Turn Around time = (19 + 12 + 4 + 1 + 5 + 2) / 6 = 43 / 6 = 7.17 unit
Average waiting time = (12 + 7 + 1 + 0 + 3 + 1) / 6 = 24 / 6 = 4 unit
Example -04:
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
Example-05:
Consider the set of 4 processes whose arrival time and burst time are given below-
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
Advantages-
It gives the best performance in terms of average response time.
It is best suited for time sharing system, client server architecture and
interactive system.
Disadvantages-
It leads to starvation for processes with larger burst time as they have to repeat
the cycle many times.
Its performance heavily depends on time quantum.
Priorities cannot be set for the processes.
With decreasing value of time quantum,
Number of context switch increases
Response time decreases
Chances of starvation decreases
Thus, smaller value of time quantum is better in terms of 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 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:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the average
waiting time and average turnaround time.
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 are
given below-
Process Id Arrival time Burst time
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 3, calculate the
average waiting time and average turnaround time.
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,
Average Turn Around time = (27 + 23 + 30 + 29 + 4 + 15) / 6 = 128 / 6 = 21.33 unit
Average waiting time = (22 + 17 + 23 + 20 + 2 + 12) / 6 = 96 / 6 = 16 unit
(d) Priority Scheduling
Out of all the available processes, CPU is assigned to the process having the
highest priority.
In case of a tie, it is broken by FCFS Scheduling.
Priority Scheduling can be used in both preemptive and non-preemptive mode.
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.
Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
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
Problem-02: Consider the set of 5 processes whose arrival time and burst time are
given below-
Process Id Arrival time Burst time Priority
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
If the CPU scheduling policy is priority pre-emptive, 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 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
(d) Multilevel Queue Scheduling
A multi-level queue scheduling algorithm partitions the ready queue into several separate
queues. The processes are permanently assigned to one queue, generally based on some
property of the process, such as memory size, process priority, or process type. Each queue has
its own scheduling algorithm.
Let us consider an example of a multilevel queue-scheduling algorithm with five queues:
1. System Processes
2. Interactive Processes
3. Interactive Editing Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the batch queue,
for example, could run unless the queues for system processes, interactive processes, and
interactive editing processes were all empty. If an interactive editing process entered the ready
queue while a batch process was running, the batch process will be pre-empted.
4.3 Deadlock
Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
For example, in the below diagram, Process 1 is holding Resource 1 and waiting for
resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
No Preemption
Preemption of process resource allocations can prevent this condition of deadlocks,
when it is possible.
Circular Wait
One way to avoid circular wait is to number all resources, and to require that processes
request resources only in strictly increasing (or decreasing) order.
a. Deadlock Avoidance
In deadlock avoidance, the operating system checks whether the system is in safe state
or in unsafe state at every step which the operating system performs.
The process continues until the system is in safe state.
Once the system moves to unsafe state, the OS has to backtrack one step.
In simple words, The OS reviews each allocation so that the allocation doesn't cause
the deadlock in the system.
3. Deadlock detection and recovery
This strategy involves waiting until a deadlock occurs.
After deadlock occurs, the system state is recovered.
The main challenge with this approach is detecting the deadlock.
4. Deadlock Ignorance
This strategy involves ignoring the concept of deadlock and assuming as if it does not
exist.
This strategy helps to avoid the extra overhead of handling deadlock.
Windows and Linux use this strategy and it is the most widely used method.
Banker's Algorithm
Banker's algorithm is a deadlock avoidance or detection algorithm and named so
because this algorithm is used in banking systems to determine whether a loan can be
granted or not.
Whenever a new process is created, it must specify the maximum number of instances
of each resource type that it needs, exactly.
This number must not be more than the total number of resources in the system. Now,
when a new process requests resources, the system must calculate whether the
allocation of the requested resources will leave the computer system in a safe state. If
so then the process will get allocated the requested resources, otherwise it must wait
until some other process releases the requested resources.
By following this practice, the banker’s algorithm avoids deadlock and allocate
resources safely.
The data structures that are used to implement the banker's algorithm are:
a. Available
It is an array of length m. It represents the number of available resources of each type.
If Available[j] = k, then there are k instances available, of resource type Rj.
b. Max
It is an n x m matrix which represents the maximum number of instances of each
resource that a process can request. If Max[i][j] = k, then the process Pi can request
atmost k instances of resource type Rj.
c. Allocation
It is an n x m matrix which represents the number of resources of each type currently
allocated to each process. If Allocation[i][j] = k, then process Pi is currently allocated
k instances of resource type Rj.
d. Need
It is a two-dimensional array. It is an n x m matrix which indicates the remaining
resource needs of each process. If Need[i][j] = k, then process Pi may need k more
instances of resource type Rj to complete its task.
Step 1: If Requesti <= Needi, then go to step 2; else raise an error condition, since the process
has exceeded its maximum claim.
Step 2: If Requesti <= Availablei then go to step 3; else Pi must have to wait as resources are
not available.
Step 3: Now we will assume that resources are assigned to process Pi and thus perform the
following steps:
Available= Available-Requesti;
Allocationi=Allocationi +Requesti;
Needi =Needi - Requesti;
If the resulting resource allocation state comes out to be safe, then the transaction is completed
and, process Pi is allocated its resources. But in this case, if the new state is unsafe, then Pi
waits for Requesti, and the old resource-allocation state is restored.
Example:
Consider a system that contains five processes P1, P2, P3, P4, P5 and the three resource types
A, B and C. Following are the resources types: A has 10, B has 5 and the resource type C has
7 instances available.
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
Total 7 2 5
Allocated
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 2 2 2 0 1 1
P5 0 0 2 4 3 3 4 3 1
Step 1: For Process P1: Need <= Available 7, 4, 3 <= 3, 3, 2 condition is false.
So, we examine another process, P2.
Step 2: For Process P2: Need <= Available 1, 2, 2 <= 3, 3, 2 condition true
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2 P2-Finish
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 2 2 2 0 1 1
P5 0 0 2 4 3 3 4 3 1
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2 P2-Finish
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 2 2 2 P4-Finish
P5 0 0 2 4 3 3 4 3 1
Similarly, we examine another process P5.
Step 5: For Process P5: P5 Need <= Available 4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
P1 0 1 0 7 5 3 3 3 2 7 4 3
P2 2 0 0 3 2 2 5 3 2 P2-Finish
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 2 2 2 7 4 5 P4-Finish
P5 0 0 2 4 3 3 P5-Finish
Now, we again examine each type of resource request for processes P1 and P3.
Step 6: For Process P1: P1 Need <= Available 7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
P1 0 1 0 7 5 3 3 3 2 P1-Finish
P2 2 0 0 3 2 2 5 3 2 P2-Finish
P3 3 0 2 9 0 2 7 4 3 6 0 0
P4 2 1 1 2 2 2 7 4 5 P4-Finish
P5 0 0 2 4 3 3 7 5 5 P5-Finish
P1 0 1 0 7 5 3 3 3 2 P1-Finish
P2 2 0 0 3 2 2 5 3 2 P2-Finish
P3 3 0 2 9 0 2 7 4 3 P3-Finish
P4 2 1 1 2 2 2 7 4 5 P4-Finish
P5 0 0 2 4 3 3 7 5 5 P5-Finish
Hence, we execute the banker's algorithm to find the safe state and the safe sequence is P2, P4,
P5, P1 and P3.
4.4 Starvation:
Starvation is the problem that occurs when high priority processes keep executing and low
priority processes get blocked for indefinite time. In heavily loaded computer system, a
steady stream of higher-priority processes can prevent a low-priority process from ever
getting the CPU. In starvation resources are continuously utilized by high priority processes.
Problem of starvation can be resolved using Aging. In Aging priority of long waiting
processes is gradually increased.
Some of the common causes of starvation are as follows −
1. If a process is never provided the resources it requires for execution because of faulty
resource allocation decisions, then starvation can occur.
2. A lower priority process may wait forever if higher priority processes constantly
monopolize the processor.
3. Starvation may occur if there are not enough resources to provide to every process as
required.
4. If random selection of processes is used then a process may wait for a long time
because of non-selection.
Some solutions that can be implemented in a system to handle starvation are as follows −
1. An independent manager can be used for allocation of resources. This resource
manager distributes resources fairly and tries to avoid starvation.
2. Random selection of processes for resource allocation or processor allocation should
be avoided as they encourage starvation.
3. The priority scheme of resource allocation should include concepts such as aging,
where the priority of a process is increased the longer it waits. This avoids starvation.
Difference between Starvation and Deadlock
Sr. Deadlock Starvation
Questions Bank:
Q.1. List any two/four/eight objective of scheduling. (R-2/4 marks)
Q.2. Describe CPU and I/O burst cycle with suitable diagram. (U-4 Marks)
Q.3. Describe pre-emptive and non pre-emptive scheduling. (U-4 Marks)
Q.4. Differentiate between pre-emptive and non pre-emptive scheduling. (A-4 Marks)
Q.5. Define following (R-2/4 Marks)
a. Throughput
b. Turnaround time
c. Waiting time
d. Response time
Q.6. Describe FCFS Scheduling algorithm with suitable example. (U-4 Marks)
Q.7. State any two advantages and two disadvantages of FCFS Scheduling algorithm. (R-4
Marks)
Q.8. Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in
the same order, with given Arrival Time and Burst Time. Calculate average turnaround
time and average waiting time using FCFS Scheduling algorithm. (A-4 Marks)
Q.9. Describe SJF Scheduling algorithm with suitable example. (U-4 Marks)
Q.10. State any two advantages and two disadvantages of SJF Scheduling algorithm. (R-4
Marks)
Q.11. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SJF non-preemptive, calculate the average waiting time
and average turnaround time. (A-4 Marks)
Q.12. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling policy is SJF preemptive, calculate the average waiting time and
average turnaround time. (A-4 Marks)
Q.13. Describe Round Robin Scheduling algorithm with suitable example. (U-4 Marks)
Q.14. State any two advantages and two disadvantages of Round Robin Scheduling algorithm.
(R-4 Marks)
Q.15. Consider the set of 5 processes whose arrival time and burst time are given below. If
the CPU scheduling algorithm is Round Robin with time quantum = 2 unit, calculate
the average waiting time and average turnaround time. (A-4 Marks)
Ans:
(a) First Come First Serve (FCFS)
Gant Chart
(𝟐𝟎+𝟕+𝟐𝟒+𝟐𝟐) 𝟕𝟑
AverageTAT = = = 18.25 ms
𝟒 𝟒
Waiting Time (WT) = Turnaround Time(TAT) – Burst Time (BT)
P1WT = 20 - 8 = 12
P2WT = 7 – 4 = 3
P3WT = 24 – 9 = 15
P4WT = 22 – 5 = 17
(𝟏𝟐+𝟑+𝟏𝟓+𝟏𝟕) 𝟒𝟕
AverageTAT = = = 11.75 ms
𝟒 𝟒
(a) Shortest Job First (SJF)
Gant Chart: