Unit-III CPU Scheduling
Unit-III CPU Scheduling
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 1
UNIT-III CPU Scheduling
Priority Scheduling
3.4 Deadlock:
System Models
Deadlock Handling:
Deadlock prevention
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 2
UNIT-III CPU Scheduling
Scheduling:
-In a computer system, the CPU (Central Processing Unit) is the brain that performs all
the calculations and operations.
-Many processes (programs) want to use the CPU at the same time.
-But the CPU can only work on one process at a time. So, the system needs a way to
decide which process gets the CPU next.
-This decision-making process is called CPU Scheduling.
-The main goal of CPU scheduling is to maximize CPU usage, reduce waiting time,
and improve system performance. It helps in handling multiple processes efficiently.
-There is a special part of the operating system called the CPU Scheduler. It selects one
process from the ready queue (list of waiting processes) and assigns the CPU to it.
➢ Types of Scheduling
- Long-Term Scheduling – decides which processes are admitted into the system.
- Short-Term Scheduling (CPU scheduling) – decides which ready process runs next.
- Medium-Term Scheduling – temporarily removes processes to free up memory.
• Arrival Time: The time at which the process arrives in the ready queue.
• Completion Time: The time at which the 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 turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 3
UNIT-III CPU Scheduling
1. CPU Burst
o Time when the process is using the CPU to execute instructions.
o Example: Calculating, processing logic, etc.
2. I/O Burst
o Time when the process is waiting for input or output (I/O) operations.
o Example: Reading from a file, writing to disk, waiting for user input.
A process does not keep using the CPU continuously. Instead, it uses the CPU for a while
(CPU burst) and then waits for I/O (I/O burst). This cycle repeats until the process
finishes.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 4
UNIT-III CPU Scheduling
If the scheduler selects a process wisely (like choosing a short CPU burst process first), it can
reduce waiting time for others and improve system performance.
1. Preemptive Scheduling
Example:
Think of a traffic signal. If an ambulance comes, it is given priority even if other vehicles are
waiting.
Similarly, in preemptive scheduling, high-priority or short tasks can interrupt running processes.
2. Non-Preemptive Scheduling
• Once a process gets the CPU, it keeps it until it finishes or goes to waiting (I/O).
• The CPU cannot be taken away from the process.
• It is simpler and easier to implement.
• But it can be unfair to short processes if a long process is already running.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 5
UNIT-III CPU Scheduling
Example:
Imagine a single-lane road. If a car enters, other cars must wait until it finishes its journey.
Similarly, in non-preemptive scheduling, once a process starts, others have to wait.
Scheduling Criteria
The scheduling criteria for CPU scheduling algorithms include the following key metrics:
💨 2. Throughput
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 6
UNIT-III CPU Scheduling
⏱️ 3. Turnaround Time
⌛ 4. Waiting Time
• Time a process spends in the ready queue, waiting for the CPU.
• Lower waiting time = better scheduling.
🕒 5. Response Time
• Time between submitting the process and the first response from CPU.
• Important in real-time systems where quick response is needed.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 7
UNIT-III CPU Scheduling
Example 01:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 8
UNIT-III CPU Scheduling
Advantages:
• Easy to implement
• Fair for processes that come first
Disadvantages:
• Long waiting time for short processes if a long one comes first
• Convoy effect – long process delays all others
Example 1:
Given processes:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 9
UNIT-III CPU Scheduling
• By SJF scheduling:
• Gantt chart:
Example 2:
• Given processes:
• Average waiting time = [(10 -1) +(1 - 1) +(17 - 2) + (5 - 3)] /4 = 26 /4 = 6.5 ms.
Advantages:
Disadvantages:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 10
UNIT-III CPU Scheduling
Example 1:
Process Arrival Time (T0) Time required for completion (∆T)(CPU Burst Time)
P0 0 10
P1 1 6
P2 3 2
P3 5 4
Gantt Chart:
P0 P1 P2 P3 P0
01 3 5 13 22
Initially only process P0 is present and it is allowed to run. But, when P1 comes, it has
shortest remaining run time. So, P0 is preempted and P1 is allowed to run. Whenever new
process comes or current process blocks, such type of decision is taken. This procedure is
repeated till all processes complete their execution
Statistics:
Waiting
CPU Turnaround
Arrival Finish Time
Process Burst Time(TAT=T1-
Time(T0) Time(T1) (WT=TAT-
Time(∆T) T0)
∆T)
P0 0 10 22 22 12
P1 1 6 9 8 2
P2 3 2 5 2 0
P3 5 4 13 8 4
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 11
UNIT-III CPU Scheduling
Advantages:
• Better average turnaround and waiting time than non-preemptive SJF.
Disadvantages:
• Can lead to starvation of long processes
• Requires accurate prediction of remaining time
Example 1:
• Gantt chart:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 12
UNIT-III CPU Scheduling
Advantages:
Disadvantages:
5. Priority Scheduling:
- Priority scheduling is a more general case of SJF, in which each job is assigned a priority
and the job with the highest priority gets scheduled first. ( SJF uses the inverse of the next
expected burst time as its priority - The smaller the expected burst, the higher the
priority.)
- Each process is assigned a priority number.
- Process with highest priority gets the CPU first.
- Can be preemptive or non-preemptive.
- Each process is assigned a priority. Process with highest priority is to be executed first
and so on.
- Processes with same priority are executed on first come first served basis.
- Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
Example 1 :
• Given processes:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 13
UNIT-III CPU Scheduling
Advantages:
Disadvantages:
Example Queues:
- System processes → Priority Scheduling
- User processes → Round Robin
Advantages:
• Organizes different types of processes well
• Flexible and can be tuned for better performance
Disadvantages:
• Hard to manage and balance between queues
• Risk of starvation for lower-priority queues
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 14
UNIT-III CPU Scheduling
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 15
UNIT-III CPU Scheduling
Deadlock:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 16
UNIT-III CPU Scheduling
System Model:
- In an operating system, many processes run at the same time. These processes need
resources like CPU, memory, files, printers, etc., to complete their tasks.
- The System Model explains how processes and resources interact, and how
deadlock can happen when this interaction goes wrong.
➢ Types of Resources
There are two types of system resources:
1. Preemptable Resources
o These resources can be taken away from a process without causing problems.
2. Non-preemptable Resources
Deadlocks usually happen with non-preemptable resources, because they can’t be forcefully
taken back.
Deadlock occurs when processes hold some resources and keep waiting for others, which are
held by other blocked processes — forming a cycle of waiting.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 17
UNIT-III CPU Scheduling
There are four conditions that must happen together for a deadlock to occur. If even one of
these is missing, deadlock won’t happen.
1. Mutual Exclusion
Example:
Only one process can use the printer at a time.
Example:
Process A is using the printer and waiting for a file.
Process B is using the file and waiting for the printer.
→ Both are holding and waiting.
3. No Preemption
Example:
If Process A has the printer, it will not give it up until it's done — even if Process B needs it
urgently.
4. Circular Wait
• There is a cycle of processes, each waiting for a resource held by the next.
Example:
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 18
UNIT-III CPU Scheduling
Deadlock Handling:
- Deadlock occurs when two or more processes are stuck, each waiting for a resource
that another process is holding. To solve this, operating systems use deadlock
handling methods.
1. Deadlock Prevention:
- The Concept of deadlock prevention is like a normal, the very important phrase is
“Prevention is better than cure”.
- The Method is the time when the deadlock occurs, we are trying to find the solution
better is that before the deadlock occurs we try to find some solutions.
- Deadlock prevention means we change the system rules so that at least one of the four
deadlock conditions never occurs.
1. Mutual Exclusion
3. No Preemption
4. Circular Wait
A. Mutual Exclusion
• Some resources must be used one at a time (like printers), so mutual exclusion is
necessary.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 19
UNIT-III CPU Scheduling
• We can stop hold and wait by forcing each process to request all required resources at
once, before starting.
• But this can cause low resource utilization and long waiting time.
C. No Preemption
• Allow preemption: If a process is waiting and cannot get a resource, then take back the
resources it already has and give them to others.
D. Circular Wait
• Prevent circular wait by assigning a fixed order to all resource types (like R1 < R2 < R3).
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 20
UNIT-III CPU Scheduling
2.Deadlock Avoidance:
- Instead of strict prevention, deadlock avoidance means the system checks before
allocating resources whether the allocation is safe or not.
- Means when we give the resources to a particular, we check whether deadlock can
occur or not? Or deadlock can be avoided or not?
- For every resources allocation ,we check safe and Unsafe Situation.
- This is done by Banker’s Algorithm.
- The Dijkstra’s Algorithm is also known as the Banker’s Algorithm.
- This is the most popular deadlock avoidance algorithm, used in systems like
banks
- We also called Banker’s algorithm as Deadlock Avoidance Algorithm.
- Just like a bank only gives loans if it knows it has enough funds to meet
future needs, the system only gives resources if it will still be in a safe state.
- Banker's algorithm is a deadlock avoidance algorithm. It is named so
because this algorithm is used in banking systems to determine whether a
loan can be granted or not.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 21
UNIT-III CPU Scheduling
• In this algorithm, if any process gets all the needed resources, then it is that it should
return the resources in a restricted period.
• Various resources are maintained in this algorithm that can fulfill the needs of at least
one client.
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 22
UNIT-III CPU Scheduling
Operating System by Prof. Vishal Jadhav Sir’s (VJTech Academy, contact us: +91-9730087674) 23