OS_L5
OS_L5
• 1. Preemptive Scheduling:
• Preemptive scheduling is used when a process switches from running state
to ready state or from the waiting state to ready state.
• The resources (mainly CPU cycles) are allocated to the process for a
limited amount of time and then 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 the ready queue till it gets its next chance to execute.
• 2. Non-Preemptive Scheduling:
• In this scheduling, once the resources (CPU cycles) are allocated to a
process, the process holds the CPU till it gets terminated or reaches a
waiting state.
• In the case of non-preemptive scheduling, the OS does not interrupt a
process running in the CPU in the middle of the execution. Instead, it waits
till the process completes its CPU burst time, and then it can allocate the
CPU to another process.
First Come First Serve (FCFS) CPU scheduling algorithm
• 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
• 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.
Cont..
Q. Consider the following set of processes that arrive at time 0, with the
length of the CPU burst time given in millisecond as:
Process Burst Time
P1 24
P2 3
P3 3
Calculate the average waiting time using the FCFS CPU scheduling algorithm.
Solution:
Cont..
• If the process arrive in the order P2, P3, P1, then the average WT will be
(0+3+6)/3 = 3ms.
• Thus, the average WT under FCFS policy is generally not minimal and
may vary substantially if the processes CPU burst time vary.
If the CPU scheduling policy is FCFS, calculate the average waiting time and
average turn around time.
Ans: Average Turn Around time = (4 + 8 + 2 + 9 + 6) / 5 = 29 / 5 = 5.8 unit
Average waiting time = (0 + 5 + 0 + 8 + 3) / 5 = 16 / 5 = 3.2 unit
Shortest Job First (SJF) scheduling
• Till now, 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.
Cont..
Q. Consider the following set of processes that arrive at time 0, with the
length of the CPU burst time given in millisecond as:
Process Burst Time
P1 6
P2 8
P3 7
P4 3
Calculate the average waiting time using the SJF CPU scheduling algorithm.
Cont..
• Q. 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.
• Ans:
Cont..
• Average WT = 24/6.
Cont..
• Q. 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.
Cont..
• Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time