OS Study Material
OS Study Material
OPERATING
SYSTEMS
[CS2204T]
PREPARED BY
PROF. R. G. KHEDKAR
JSPM’S RSCOE
OPERATING SYSTEMS SL LAB
2
UNIT-II
Process Scheduling
Content
Process Scheduling
• Definition
• The process scheduling is the activity of the process manager that handles the removal
of the running process from the CPU and the selection of another process on the basis of
a particular strategy.
• Such operating systems allow more than one process to be loaded into the executable
memory at a time and the loaded process shares the CPU using time multiplexing.
• Process scheduling is allocating resources to tasks so that the tasks can be completed in
an efficient and timely manner.
• Keep the CPU busy and have maximum utilization of the CPU.
• Minimum response time for executing a process means the shortest possible time for
process execution.
• The OS maintains all Process Control Blocks (PCBs) in Process Scheduling Queues.
• The OS maintains a separate queue for each of the process states and PCBs of all
processes in the same execution state are placed in the same queue.
• When the state of a process is changed, its PCB is unlinked from its current queue and
moved to its new state queue.
• The Operating System maintains the following three important process scheduling
queues –
1. Job queue
2. Ready queue
3. Device queues/ Waiting queues
1. Job queue –
• The long term scheduler (Job scheduler) picks some of the jobs and put them in the
primary memory.
2. Ready queue –
• This queue keeps a set of all processes residing in main memory, ready and waiting to
execute.
• The short term scheduler picks the job from the ready queue and dispatch to the CPU for
the execution.
• The processes which are blocked due to unavailability of an I/O device constitute this
queue.
• When the process needs some IO operation in order to complete its execution, OS
changes the state of the process from running to waiting.
• The context (PCB) associated with the process gets stored on the waiting queue which
will be used by the Processor when the process finishes the IO.
Types of Schedulers
• Schedulers are special system software which handle process scheduling in various
ways.
• Their main task is to select the jobs to be submitted into the system and to decide which
process to run.
• It helps to keep all computer resources busy and allows multiple users to share system
resources effectively.
• It is important that the long-term scheduler make a careful selection of both I/O and
CPU-bound processes.
• I/O-bound tasks are which use much of their time in input and output operations while
CPU-bound processes are which spend their time on the CPU.
• The job scheduler increases efficiency by maintaining a balance between the two.
• They operate at a high level and are typically used in batch-processing systems.
• Note: Short-term scheduler only selects the process to schedule it doesn’t load the
process on running. Here is when all the scheduling algorithms are used.
• The CPU scheduler is responsible for ensuring no starvation due to high burst time
processes.
• The dispatcher is responsible for loading the process selected by the Short-term
scheduler on the CPU (Ready to Running State) Context switching is done by the
dispatcher only.
• Switching context.
3. Medium-Term Scheduler
• It is responsible for suspending and resuming the process. It mainly does swapping
(moving processes from main memory to disk and vice versa).
• Swapping may be necessary to improve the process mix or because a change in memory
requirements has overcommitted available memory, requiring memory to be freed up.
• It is helpful in maintaining a perfect balance between the I/O bound and the CPU bound.
It reduces the degree of multiprogramming.
https://www.youtube.com/watch?v=DtQ9_WPezNA
Scheduling criteria
a. CPU utilization:
• The main purpose of any CPU algorithm is to keep the CPU as busy as possible.
• 40 to 90 percent depending on the system load.
b. Throughput:
• The average CPU performance is the number of processes performed and completed
during each unit. This is called throughput.
• The output may vary depending on the length or duration of the processes.
d. Waiting Time:
• The Scheduling algorithm does not affect the time required to complete the process once
it has started performing.
• It only affects the waiting time of the process i.e. the time spent in the waiting process in
the ready queue.
e. Response Time:
• In a collaborative system, turn around time is not the best option.
• The process may produce something early and continue to computing the new results
while the previous results are released to the user.
• Therefore another method is the time taken in the submission of the application process
until the first response is issued. This measure is called response time.
https://www.youtube.com/watch?v=bWHFY8-
rL5I&list=PLBlnK6fEyqRitWSE_AyyySWfhRgyA-rHk&index=4
Scheduling algorithms:
• There are two categories of scheduling:
1. Non-preemptive:
2. Preemptive:
1. Non-preemptive:
• Preemptive 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.
https://www.youtube.com/watch?v=4DhFmL-
6SDA&list=PLBlnK6fEyqRitWSE_AyyySWfhRgyA-rHk&index=3
• Arrival Time: Time at which the process arrives in the ready queue.
• Turn Around Time: Time Difference between completion time and arrival time.
• Waiting Time(W.T): Time Difference between turn around time and burst time.
Scheduling Algorithm
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.
It is always non-preemptive in nature.
Jobs are executed on first come, first serve basis.
It is a non-preemptive, pre-emptive scheduling algorithm.
Easy to understand and implement.
Its implementation is based on FIFO queue.
Poor in performance as average wait time is high.
Advantages-
Disadvantages-
It does not consider the priority or burst time of the processes.
It suffers from convoy effect i.e. processes with higher burst time arrived before the
processes with smaller burst time.
https://www.youtube.com/watch?v=7DoP1L9nAAs&list=PLBlnK6fEyqRitWSE_Ay
yySWfhRgyA-rHk&index=5
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
TURN AROUND
PROCESS WAIT TIME TIME
P1 0 24
P2 24 27
P3 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
Process which have the shortest burst time are scheduled first.
If two processes have the same bust time, then FCFS is used to break the tie.
This is a non-pre-emptive, pre-emptive scheduling algorithm.
Best approach to minimize waiting time.
Easy to implement in Batch systems where required CPU time is known in advance.
Impossible to implement in interactive systems where required CPU time is not
known.
The processer should know in advance how much time process will take.
Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time First
(SRTF).
Advantages-
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.
https://www.youtube.com/watch?v=t0g9b3SJECg&list=PLBlnK6fEyqRitWSE_AyyyS
WfhRgyA-rHk&index=8
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
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
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
Example-02:
Consider the set of 5 processes whose arrival time and burst time are given below-
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
P1 13 13 – 0 = 13 13 – 9 = 4
P2 5 5–1=4 4–4=0
P3 22 22- 2 = 20 20 – 9 = 11
Now,
Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit
Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit
Example-05:
Consider the set of 4 processes whose arrival time and burst time are given below-
If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.
Solution-
Gantt Chart-
Now, we know-
Thus,
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-
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 can not be set for the processes.
https://www.youtube.com/watch?v=YzBBJYfwdi8&list=PLBlnK6fEyqRitWSE_AyyySWfhRgyA
-rHk&index=14
Thus, higher value of time quantum is better in terms of number of context switch.
Example-01:
Consider the set of 5 processes whose arrival time and burst time are given below-
P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3
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-
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
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-
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
(d)
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
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.
https://www.youtube.com/watch?v=yKD3pcFvGmY&list=PLBlnK6fEyqRitWSE_AyyySWfh
RgyA-rHk&index=11
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
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-
P3 2 1 4
P4 3 5 5
P5 4 2 5
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-
Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst 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
• It uses scheduling algorithms based on static or dynamic methods where the number of
tasks and workload may vary.
• Preemptive or non-preemptive approaches are used to select the highest priority tasks to
be executed first compared with the lower priority tasks.
• It is preemptive in nature. The priority is decided according to the cycle time of the
processes that are involved.
• If the process has a small job duration, then it has the highest priority. Thus if a process
with highest priority starts execution, it will preempt the other running processes.
• The priority of a process is inversely proportional to the period it will run for. A set of
processes can be scheduled only if they satisfy the following equation:
Where n is the number of processes in the process set, Ci is the computation time of the process,
Ti is the Time period for the process to run and U is the processor utilization.
1
Example:
P1 3 20
P2 2 5
P3 2 10
• It is less than 1 or 100% utilization. The combined utilization of three processes is less
than the threshold of these processes which means the above set of processes is
schedulable and thus satisfies the above equation of the algorithm.
• Scheduling time – For calculating the Scheduling time of the algorithm we have to take
the LCM of the Time period of all the processes.
• Priority – As discussed above, the priority will be the highest for the process which has
the least running time period.
• Thus P2 will have the highest priority, and after that P3 and lastly P1.
• P2>P3>P1
• The above figure says that Process P2 will execute two times for every 5 time units,
Process P3 will execute two times for every 10 time units and Process P1 will execute
three times in 20 time units. This has to be kept in mind for understanding the entire
execution of the algorithm below.
• Process P2 will run first for 2 time units because it has the highest priority.
• After completing its two units, P3 will get the chance and thus it will run for 2 time
units.
• As we know that process P2 will run 2 times in the interval of 5 time units and process
P3 will run 2 times in the interval of 10 time units, they have fulfilled the criteria and thus
now process P1 which has the least priority will get the chance and it will run for 1 time.
And here the interval of five time units have completed.
• Because of its priority P2 will preempt P1 and thus will run 2 times. As P3 have
completed its 2 time units for its interval of 10 time units, P1 will get chance and it will
run for the remaining 2 times, completing its execution which was thrice in 20 time units.
• Now 9-10 interval remains idle as no process needs it. At 10 time units, process P2 will
run for 2 times completing its criteria for the third interval ( 10-15 ).
• Process P3 will now run for two times completing its execution. Interval 14-15 will again
remain idle for the same reason mentioned above. At 15 time unit, process P2 will
execute for two times completing its execution.This is how the rate monotonic scheduling
works.
• Conditions: The analysis of Rate monotonic scheduling assumes few properties that
every process should possess. They are :
• Processes involved should not share the resources with other processes.
• Process running with highest priority that needs to run, will preempt all the other
processes.
• Priorities must be assigned to all the processes according to the protocol of Rate
monotonic scheduling.
• Advantages:
• It is easy to implement.
• If any static priority assignment algorithm can meet the deadlines then rate monotonic
scheduling can also do the same. It is optimal.
• It consists of a calculated copy of the time periods, unlike other time-sharing algorithms
as Round robin which neglects the scheduling needs of the processes.
• Disadvantages:
• RMA is not optimal when the task period and deadline differ.
https://www.youtube.com/watch?v=xgW8VhEOpFg
• Earliest Deadline First (EDF) is an optimal dynamic priority scheduling algorithm used
in real-time systems.
• EDF uses priorities to the jobs for scheduling. It assigns priorities to the task according to
the absolute deadline. The task whose deadline is closest gets the highest priority. The
priorities are assigned and changed in a dynamic fashion. EDF is very efficient as
compared to other scheduling algorithms in real-time systems. It can make the CPU
utilization to about 100% while still guaranteeing the deadlines of all the tasks.
• EDF includes the kernel overload. In EDF, if the CPU usage is less than 100%, then it
means that all the tasks have met the deadline. EDF finds an optimal feasible schedule.
The feasible schedule is one in which all the tasks in the system are executed within the
deadline. If EDF is not able to find a feasible schedule for all the tasks in the real-time
system, then it means that no other task scheduling algorithms in real-time systems can
give a feasible schedule. All the tasks which are ready for execution should announce
their deadline to EDF when the task becomes runnable.
• EDF scheduling algorithm does not need the tasks or processes to be periodic and also
the tasks or processes require a fixed CPU burst time. In EDF, any executing task can be
preempted if any other periodic instance with an earlier deadline is ready for execution
and becomes active. Preemption is allowed in the Earliest Deadline First scheduling
algorithm.
• Now, comparing the deadline of (P1, P2) = (100, 75), P2 continues to execute.
• Now, again comparing the deadline of (P1, P2) = (100, 150), P1 continues to execute.
• Finally at time 150, both P1 and P2 have the same deadline, so P2 will continue to
execute till its processing time after which P1 starts to execute.
• Meeting Deadlines: EDF ensures that tasks with the earliest deadlines are executed first.
By prioritizing tasks based on their deadlines, EDF minimizes the chances of missing
deadlines and helps meet real-time requirements.
• Flexibility: EDF can handle both periodic and aperiodic tasks, making it suitable for a
wide range of real-time systems. It allows for dynamic task creation and scheduling
without disrupting the execution of existing tasks.
https://www.youtube.com/watch?v=ejPXTOcMRPA