Module2 Os
Module2 Os
For conditions 2 and 3 there is a choice - To either continue running the current
process, or select a different one.
When scheduling takes place only under circumstances 1 and 4, we say that the
scheduling scheme is non-preemptive or cooperative; otherwise, it is preemptive.
Under non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching
to the waiting state.
Windows used non-preemptive scheduling up to Windows 3.x, and started using pre-
emptive scheduling with Win95
Dispatcher
• A dispatcher is a special program which comes into play after the scheduler.
• When the scheduler completes its job of selecting a process, it is the dispatcher which
• The dispatcher is the module that gives a process control over the CPU after it has been
• Switching context
• Jumping to the proper location in the user program to restart that program
The Difference between the Scheduler and Dispatcher
Arrival Time
Arrival time is the point of time at which a
process enters the ready queue.
Completion Time
Completion time is the point of time at
which a process completes its execution
on the CPU and takes exit from the system
Turn Around Time
From the point of view of a particular process, the important criterion
is how long it takes to execute that process. The interval from the time
of submission of a process to the time of completion is the turnaround
time
Burst Time:
Burst time is the amount of time required by a process for
executing on CPU.It is also called as execution time or running
time.
Waiting Time
• Waiting time is the amount of time spent by a process waiting in the ready
queue for getting the CPU.
Response Time
Response time is the amount of time after which a process gets the CPU for
the first time after entering the ready queue
In this scheduling, once the resources (CPU cycles) are The resources (mainly CPU cycles) are allocated to the
allocated to a process, the process holds the CPU till it process for a limited amount of time and then taken
gets terminated or reaches a waiting state. In the case away, and the process is again placed back in the
of non-preemptive scheduling does not interrupt a ready queue if that process still has CPU burst time
process running CPU in the middle of the execution remaining. That process stays in the ready queue till it
gets its next chance to execute.
Scheduling Algorithm
Non-Pre emptive Scheduling Pre-emptive Scheduling
• In FCFS
Scheduling,
• The process
which arrives
first in the
ready queue is
firstly
assigned the
CPU.
• In case of a
tie, process
with smaller
process id is
executed first.
Problem-01:
• If the CPU scheduling policy is FCFS, calculate the average waiting time and average turn around
time. If the CPU scheduling policy is FCFS, calculate the average waiting time and average turn
around time.
Solution
Problem-02:
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
Preemptive SJF (Shortest Remaining Time First
Scheduling)
• Preemptive version of SJF
• In Preemptive SJF Scheduling, jobs are put into the ready queue as
they come. A process with shortest burst time begins execution.
• If a process with even a shorter burst time arrives, the current
process is removed or preempted from execution, and the shorter job
is allocated CPU cycle.
• Step 4) At time = 4, process P5 will arrive. The burst time of P3, P5, and P1 is
compared. Process P5 is executed because its burst time is lowest. Process P1 is
preempted.
Problem-02
Solution
Problem-3
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 turn around time.
P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1
• Advantages-
• SRTF is optimal and guarantees the minimum average waiting time.
• It provides a standard for other algorithms since no other algorithm
performs better than it.
• Disadvantages-
• It can not be implemented practically since burst time of the
processes can not be 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.
Non-pre emptive priority scheduling
• Priority scheduling is a type of scheduling algorithm used by the
operating system to schedule the processes for execution.
• The priority scheduling has both the preemptive mode of scheduling
and the non-preemptive mode of scheduling.
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest
integer highest priority)
• Preemptive
• Non preemptive
Problem 1
•There is only P1 available at time 0, so it will be executed first irrespective of the priority, and it
cannot be preempted in between before its completion.
•When it is completed at 4th-time unit, we have all P2, P3, and P4 available. So, they are executed
according to their priorities.
solution
P ID Arrival Burst Priority
Time Time Number
(ms) (ms)
P1 0 4 3
P2 1 2 2
P3 2 3 4
P4 4 2 1
Problem 2
• Suppose we've got seven following processes with their priority number and arrival time given; let's
schedule them using a non-preemptive priority scheduling:
Preemptive Priority CPU Scheduling Algorithm
• Examples to show the working of Preemptive Priority CPU
Scheduling Algorithm
• Example-1: Consider the following table of arrival time, Priority, and
burst time for five processes P1, P2, P3, P4, and P5.
• The Preemptive Priority CPU Scheduling Algorithm will work on the
basis of the steps mentioned below:
• At time t = 0,
• Process P1 is the only process available in the ready queue, as its arrival time
is 0ms.
• Hence Process P1 is executed first for 1ms, from 0ms to 1ms, irrespective of
its priority.
• Remaining Burst time (B.T) for P1 = 3-1 = 2 ms.
• At time t = 1 ms,
• There are 2 processes available in the ready queue: P1 and P2.
• Since the priority of process P2 is higher than the priority of process P1,
therefore Process P2 will get executed first.
• Hence Process P2 is executed for 1ms, from 1ms to 2ms.
• Remaining Burst time (B.T) for P2 = 4-1 = 3 ms.
• At time t = 2 ms,
• There are 3 processes available in the ready queue: P1, P2, and P3.
• Since the priority of process P2 is higher than the priority of process P1 and
P3, therefore Process P2 will get executed first.
• Hence Process P2 is executed for 1ms, from 2ms to 3ms.
• Remaining Burst time (B.T) for P2 = 3-1 = 2 ms.
• At time t = 3 ms,
• There are 4 processes available in the ready queue: P1, P2, P3, and P4.
• Since the priority of process P2 is highest among the priority of processes P1,
P2, P3, and P4, therefore Process P2 will get executed first.
• Hence Process P2 is executed for 1ms, from 3ms to 4ms.
• Remaining Burst time (B.T) for P2 = 1-1 = 0 ms.
• At time t = 4 ms,
• There are 5 processes available in the ready queue: P1, P2, P3, P4, and P5.
• Since the priority of process P2 is highest among the priority of processes P1,
P2, P3, P4, and P5, therefore Process P2 will get executed first.
• Hence Process P2 is executed for 1ms, from 4ms to 5ms.
• Remaining Burst time (B.T) for P2 = 1-1 = 0 ms.
• Since Process P2’s burst time has become 0, therefore it is complete and will
be removed from the process queue.
• At time t = 5 ms,
• There are 4 processes available in the ready queue: P1, P3, P4, and P5.
• Since the priority of process P1 is the highest among the priority of processes
P1, P3, P4 and P5, therefore Process P1 will get executed first.
• Hence Process P1 is executed for 2ms, from 5ms to 7ms.
• Remaining Burst time (B.T) for P1 = 2-2 = 0 ms.
• Since Process P1’s burst time has become 0, therefore it is complete and will
be removed from the process queue.
• At time t = 7 ms,
• There are 3 processes available in the ready queue: P3, P4, and P5.
• Since the priority of process P3 is the highest among the priority of processes
P3, P4, and P5, therefore Process P3 will get executed first.
• Hence Process P3 is executed for 6ms, from 7ms to 13ms.
• Remaining Burst time (B.T) for P3 = 6-6 = 0 ms.
• Since Process P3’s burst time has become 0, therefore it is complete and will
be removed from the process queue.
• At time t = 13 ms,
• There are 2 processes available in the ready queue: P4 and P5.
• Since the priority of process P4 is highest among the priority of process P4
and P5, therefore Process P4 will get executed first.
• Hence Process P4 is executed for 4ms, from 13ms to 17ms.
• Remaining Burst time (B.T) for P4 = 4-4 = 0 ms.
• Since Process P4’s burst time has become 0, therefore it is complete and will
be removed from the process queue.
• At time t = 17 ms,
• There is only 1 process available in the ready queue: P5.
• Hence Process P5 is executed for 2ms, from 17ms to 19ms.
• Remaining Burst time (B.T) for P5 = 2-2 = 0 ms.
• Since Process P5’s burst time has become 0, therefore it is complete and will
be removed from the process queue.
• At time t = 19 ms, There is no more process available in the ready
queue. Hence the processing will now stop.
Round Robin Scheduling
• Round Robin is a CPU scheduling algorithm where each process is
cyclically assigned a fixed time slot. It is the preemptive version of the
First come First Serve CPU Scheduling algorithm.
• Round Robin CPU Algorithm generally focuses on Time Sharing
technique.
• The period of time for which a process or job is allowed to run in a
pre-emptive method is called time quantum.
• Each process or job present in the ready queue is assigned the CPU
for that time quantum, if the execution of the process is completed
during that time then the process will end else the process will go
back to the waiting table and wait for its next turn to complete the
execution.
1.First, the processes which are
eligible to enter the ready queue
enter the ready queue.
2. After entering the first process in
Ready Queue is executed for a Time
Quantum chunk of time.
3. After execution is complete, the
process is removed from the ready
queue, Even now the process requires
some time to complete its execution,
then the process is added to Ready
Queue.
RR Scheduling Example
In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival
time and burst time are given below in the table. The time quantum of the system is 4 units
Disadvantages