Osy Chapter 4 Notes
Osy Chapter 4 Notes
Non-Preemptive Scheduling:
In this scheduling, once the system has allocated CPU to a process, the system cannot
remove that processor from the process. The process switches from running to waiting
state and termination of process.
Scheduling Criteria
There are several different criteria to consider when trying to select the "best" scheduling
algorithm for a particular situation and environment, including:
1. CPU utilization
The main objective of any CPU scheduling algorithm is to keep the CPU as busy as
possible. Theoretically, CPU utilization can range from 0 to 100 but in a real-time system,
it varies from 40 to 90 percent depending on the load upon the system.
2. Throughput
A measure of the work done by the CPU is the number of processes being executed and
completed per unit of time. This is called throughput. The higher is the number, the more
work is done by the system.
3. Turnaround Time
For a particular process, an important criterion is how long it takes to execute that
process. The turnaround time start from the time of submission of a process to the time
of completion of a process.
4. Waiting time
How much time processes spend in the ready queue waiting their turn to get on the CPU.
5. Response Time
The time taken from submission of the process of the request until the first response is
produced is called response time.
Response Time = CPU Allocation Time (when the CPU was allocated for the first) -
Arrival Time
In brief:
Arrival Time: Time at which the process arrives in the ready queue.
Completion Time: Time at which process completes its execution.
Burst Time: Time required by a process for CPU execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time (W.T): Time Difference between turnaround time and burst time.
Waiting Time = Turn Around Time – Burst Time
4.2 Types of Scheduling Algorithm
1. First Come First Serve (FCFS)
2. Shortest Job First (SJF)
3. Round Robin Scheduling
4. Priority Scheduling
5. Multilevel Queue Scheduling
Problem 1:
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time using FCFS
algorithm.
Process ID Arrival time Burst time
P1 2 2
P2 5 6
P3 0 4
P4 0 7
P5 7 4
Solution
Gantt chart
Process ID Arrival time Burst time CT TAT=CT-AT WT=TAT-BT RT
P1 2 2 13 13-2= 11 11-2= 9 9
P2 5 6 19 19-5= 14 14-6= 8 8
P3 0 4 4 4-0= 4 4-4= 0 0
P4 0 7 11 11-0= 11 11-7= 4 4
P5 7 4 23 23-7= 16 16-4= 12 12
Problem 2:
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time.
P1 2 2
P2 0 1
P3 2 3
P4 3 5
P5 4 5
Solution
Gantt chart −
For this problem CT, TAT, WT, RT is shown in the given table –
P1 2 2 4 4-2= 2 2-2= 0 0
P2 0 1 1 1-0= 1 1-1= 0 0
P3 2 3 7 7-2= 5 5-3= 2 2
P4 3 5 12 12-3= 9 9-5= 4 4
P5 4 5 17 17-4= 13 13-5= 8 8
Disadvantages-
1. May suffer with the problem of starvation
2. It is not implementable because the exact Burst time for a process can't be known in
advance.
Problem 1:
Consider the given table below and find Completion time (CT), Turn-around time (TAT), Waiting
time (WT), Response time (RT), Average Turn-around time and Average Waiting time using SJF.
Solution
Gantt chart −
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.
Process Id Arrival time Burst time
P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3
Solution-
Gantt Chart-
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.
Problem 1:
Consider the set of 5 processes whose arrival time and burst time are given below- If the CPU
scheduling policy is SRTF (SJF pre-emptive), calculate the average waiting time and average
turnaround time.
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.
CPU is assigned to the process on the basis of FCFS for a fixed amount of time.
This fixed amount of time is called as time quantum or time slice.
After the time quantum expires, the running process is preempted and sent to the
ready queue.
Then, the processor is assigned to the next arrived process.
It is always preemptive in nature.
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.
Problem 1:
Consider the set of 5 processes whose arrival time and burst time are given below. If the
CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average
waiting time and average turnaround time.
Solution-
Ready Queue- P5, P1, P2, P5, P4, P1, P3, P2, P1
Gantt Chart-
Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below-If the
CPU scheduling policy is Round Robin with time quantum = 2, calculate the average waiting
time and average turnaround time.
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
Solution-
Ready Queue- P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1
Gantt chart-
o Out of all the available processes, CPU is assigned to the process having the
highest priority.
o In case of a tie, it is broken by FCFS Scheduling.
o Priority Scheduling can be used in both preemptive and non-preemptive
mode.
o The waiting time for the process having the highest priority will always be
zero in preemptive mode.
o The waiting time for the process having the highest priority may not be zero
in non-preemptive mode.
Advantages-
o It considers the priority of the processes and allows the important processes
to run first.
o Priority scheduling in pre-emptive mode is best suited for real time
operating system.
Disadvantages-
o Processes with lesser priority may starve for CPU.
o 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-If
the CPU scheduling policy is priority non-preemptive, calculate the average waiting time
and average turnaround time. (Higher number represents higher priority)
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
Solution-
Gantt Chart-
Process Arrival Burst Priority
CT TAT=CT-AT WT=TAT-BT RT
Id time time
P1 0 4 2 4 4–0=4 4–4=0 0
P2 1 3 3 15 15 – 1 = 14 14 – 3 = 11 12
P3 2 1 4 12 12 – 2 = 10 10 – 1 = 9 11
P4 3 5 5 9 9–3=6 6–5=1 4
P5 4 2 5 11 11 – 4 = 7 7–2=5 9
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-If the
CPU scheduling policy is priority preemptive, calculate the average waiting time and
average turn around time. (Higher number represents higher priority).
Solution-
Gantt Chart-
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.
Necessary conditions for deadlock
1. Mutual Exclusion: One or more than one resource are non-sharable means Only one process can
use at a time.
2. Hold and Wait: A process is holding at least one resource and waiting for another resource.
3. No Pre-emption: A resource cannot be taken from a process unless the process releases the
resource means the process which once scheduled will be executed till the completion and no other
process can be scheduled by the scheduler meanwhile.
4. Circular Wait: A set of processes are waiting for each other in circular form means All the
processes must be waiting for the resources in a cyclic manner so that the last process is waiting for
the resource which is being held by the first process.
Deadlock Handling
The various strategies for handling deadlock are-
1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection and Recovery
4. Deadlock Ignorance
1. Deadlock Prevention
Deadlocks can be prevented by preventing at least one of the four required conditions:
Mutual Exclusion
o Shared resources such as read-only files do not lead to deadlocks.
o Unfortunately, some resources, such as printers and tape drives, require
exclusive access by a single process.
No Preemption
o Preemption of process resource allocations can prevent this condition of
deadlocks, when it is possible.
Circular Wait
o 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.
2. 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.
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.