0% found this document useful (0 votes)
21 views30 pages

Schedulers

Uploaded by

o8799621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views30 pages

Schedulers

Uploaded by

o8799621
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Process Queue

The Operating system manages various types of queues for each of the
process states.
The PCB related to the process is also stored in the queue of the same
state. If the Process is moved from one state to another state then its
PCB is also unlinked from the corresponding queue and added to the
other state queue in which the transition is made.
Contd…
Job Queue: In starting, all the processes get stored in the job queue. It
is maintained in the secondary memory. The long term scheduler (Job
scheduler) picks some of the jobs and put them in the primary
memory.
Ready Queue: Ready queue is maintained in primary memory. The
short term scheduler picks the job from the ready queue and dispatch
to the CPU for the execution. A ready queue can be implemented as a
FIFO queue, a priority queue, a tree, or simply an unordered linked list.
Waiting (or Device) 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.
Process Scheduler
Schedulers in OS are special system software. They help in scheduling
the processes in various ways. They are mainly responsible for
selecting the jobs to be submitted into the system and deciding which
process to run.
Process State Transition Diagram
STS

LTS

MTS
Long Term Scheduler
Long term scheduler is also known as job scheduler. It chooses the
processes from the pool (secondary memory) and keeps them in the
ready queue maintained in the primary memory.
The primary objective of long-term scheduler is to maintain a good
degree of multiprogramming, i.e., number of process present in ready
state at any point of time.
The purpose of long term scheduler is to choose a perfect mix of I/O
bound and CPU bound processes among the jobs present in the pool.
Short term Scheduler
Short term scheduler is also known as CPU scheduler. It selects one of
the Jobs from the ready queue and dispatch to the CPU for the
execution.

The primary objective of short-term scheduler is to increase the system


performance.

Short-term scheduler only selects the process to schedule it doesn’t


load the process on running. A scheduling algorithm is used to select
which job is going to be dispatched for the execution.

Dispatcher is responsible for loading the process selected by Short-


term scheduler on the CPU (Ready to Running State).
Contd…
The dispatcher should be as fast as possible, since it is invoked during
every context switch. The time it takes for the dispatcher to stop one
process and start another running is known as the dispatch latency.
This function involves the following:
Switching context from one process to another.
 Switching to user mode.
Jumping to the proper location in the user program
to resume that program.
The short term scheduler selects job whose
CPU time is very high then all the jobs after that,
will have to wait in the ready queue for a
very long time. This problem is called starvation.
Medium term Scheduler
The primary objective of medium-term scheduler is to perform
swapping, i.e., moving processes from main memory to disk and vice
versa. Medium-term is also called swapping scheduler.
In order to remove the process from memory and make space for other
processes, the suspended process should be moved to secondary
storage.
It reduces the degree of multiprogramming.
The swapping is necessary to have a perfect mix of processes in the
ready queue.
Various Times related to the Process
Arrival Time
The time at which the process enters into the ready queue is called the
arrival time (AT).
 Burst Time
The total amount of time required by the CPU to execute the whole
process is called the Burst Time (BT). It is confusing to calculate the
execution time for a process even before executing it hence the
scheduling problems based on the burst time cannot be implemented in
reality.
 Completion Time
The Time at which the process enters into the completion state or the
time at which the process completes its execution, is called completion
time (CT).
Contd…
 Turnaround time
The total amount of time spent by the process from its arrival to its
completion, is called Turnaround time (TAT).
 Waiting Time
The Total amount of time for which the process waits for the CPU to be
assigned is called waiting time (WT).
 Response Time
The difference between the arrival time and the time at which the
process first gets the CPU is called Response Time(RT).

TAT=WT+BT
TAT=CT-AT
WT=TAT-BT
Process or CPU Scheduling
In the uniprogrammming systems like MS DOS, when a process waits
for any I/O operation to be done, the CPU remains idol. In
Multiprogramming systems, the CPU doesn't remain idle during the
waiting time of the Process and it starts executing other processes.
The Operating system uses various scheduling algorithm to schedule
the processes on the CPU to have the maximum utilization of it and this
procedure is called CPU scheduling.
Different CPU-scheduling algorithms such as FCFS, round robin, SJF
have different properties, and the choice of a particular algorithm may
favor one class of processes over another. Many criteria have been
suggested for comparing CPU-scheduling algorithms.
Scheduling Criteria
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.
Throughput:-A measure of the work done by CPU is the number of
processes being executed and completed per unit time. This is called
throughput. The throughput may vary depending upon the length or
duration of processes.
Turn Around Time:-The time elapsed from the time of submission of a
process to the time of completion is known as the turnaround time.
Turn-around time is the sum of times spent waiting to get into memory,
waiting in ready queue, executing in CPU, and waiting for I/O.
Contd…
Waiting time:- A scheduling algorithm does not affect the time
required to complete the process once it starts execution. It only affects
the waiting time of a process i.e. time spent by a process waiting in the
ready queue.
Response time:- The time from the submission of a request until the
first response is produced is called response time. It is the time it takes
to start responding, not the time it takes to output the response.

Note*:-It is desirable to maximize CPU utilization and throughput and


to minimize turnaround time, waiting time, and response time.
Types of CPU Scheduling
CPU scheduling decisions may take place under the following four
circumstances:
1) When a process switches from the running state to the waiting state(for I/O
request or invocation of wait for the termination of one of the child processes).
2) When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
3) When a process switches from the waiting state to the ready state(for
example, completion of I/O).
4) When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A
new process(if one exists in the ready queue) must be selected for
execution. There is a choice, however in circumstances 2 and 3.
When Scheduling takes place only under circumstances 1 and 4, we say
the scheduling scheme is non-preemptive; otherwise, the scheduling
scheme is preemptive.
Contd…
Non-Preemptive Scheduling:- 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.

Preemptive Scheduling:- 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.
First Come First Serve (FCFS) Scheduling Algorithm
Criteria:- Arrival Time Time Complexity:- O(n)
Mode:- Non-preemptive
Data structure:- Queue
Tiebreaker:- Process ID (smaller ID is given preference)
The process which arrives first, gets executed first, or we can say that
the process which requests the CPU first, gets the CPU allocated first.
Example:- Find Average waiting time and average turn around time.
Process ID Arrival Time (AT) Burst Time (BT)
1 0 2
2 1 6
3 2 4
4 3 9
5 4 12
Contd…
Process ID Arrival Time Burst Time Completion Turn Around Waiting Time
(AT) (BT) Time (CT) Time (TAT) (WT)
1 0 2 2 2 0
2 1 6 8 7 1
3 2 4 12 10 6
4 3 9 21 18 9
5 4 12 33 29 17

Gantt Chart
P1 P2 P3 P4 P5
0 2 8 12 21 33

Turn Around Time (TAT) = CT – AT


Process P1: 2 – 0 = 2 Process P2: 8 – 1 = 7 Process P3 : 12 – 2 = 10
Process P4: 21 – 3 = 18 Process P5: 33 – 4 = 29
Average TAT = 66/5 = 13.2
Contd…
Waiting Time (WT) = TAT- BT
Process P1: 2 – 2 = 0 Process P2: 7 – 6 = 1 Process P3: 10 – 4 = 6
Process P4: 18 – 9 = 9 Process P5: 29 -12 = 17
Average WT = 33/5 = 6.6
Throughput: number of process/ total CT = 5/33
Example: Find Average Waiting Time, Turn Around Time and Throughput.
Example
Process ID Arrival Time (AT) Burst Time (BT) Completion Time (CT) Turn Around Time Waiting Time
(TAT) (WT)
1 0 20 20 20 0
2 1 2 22 21 19
3 1 1 23 22 21

Gantt Chart

P1 P2 P3
0 20 22 23

Average Turn Around Time = 63/3 = 21.6


Average Waiting Time = 40/3 = 13.3
Throughput = 3/23
Example
Process ID AT BT CT TAT WT
1 1 20 23 22 2
2 0 2 2 2 0
3 0 1 3 3 2

Gantt Chart
P2 P3 P1
0 2 3 23

Average Turnaround Time = 27/3 = 9


Average Waiting Time = 4/3 = 1.3
Throughput = 3/23
Advantage and Disadvantage of FCFS
Advantage
Easy to understand and implement.
Disadvantage
It doesn't consider priority or BT.
It suffers from convoy effect. (Consider processes with higher BT
arrived before the processes with smaller BT. Then, smaller processes
have to wait for a long time for longer processes to release the CPU)
FCFS with Overhead
We are assuming that all the processes are the CPU bound processes only.
However, if the time taken by the scheduler in context switching is considered
then the average waiting time of the system will be increased which also
affects the efficiency of the system.
Example:- context switching time = 1 time unit
Process ID AT BT CT TAT WT
1 0 2 2 2 0
2 3 1 4 1 0
3 5 6 11 6 0

The system will take extra 1 unit of time (overhead) after the execution of
every process to schedule the next process.
 Efficiency= (1 - useless time/total CT) X 100= (1-(2/11)) X 100= 81.8
Shortest Job First (SJF)
Criteria:- Burst Time
Mode :- non- preemptive
Data Structure :- min-heap
Time complexity :- O(nlogn)
Tie break:- FCFS scheduling
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
first.
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.
Example

Turn Around Time (TAT) = CT –AT


P1: 8 – 1=7 P2: 16 - 2 = 14 P3: 9 – 3 = 6 P4: 11 – 4 = 7 P5= 24 – 5 = 19
Average TAT= 53/5= 10.6
Waiting Time (WT) = TAT – BT
P1: 7 – 7 = 0 P2: 14 – 5= 9 P3: 6 – 1 = 5 P4: 7 – 2 = 5 P5: 19 – 8 = 11
Average WT = 30/5=6
Throughput = 5/24
SJF with Preemption
Mode :- preemptive
The Pre-emptive SJF is also known as Shortest Remaining Time First (SRTF),
because at any given point of time, the job with the shortest remaining time
is executed first.
Process AT BT CT TAT WT
ID
1 1 7 16 15 8
2 2 5 10 8 3
3 3 1 4 1 0
4 4 2 6 2 0
5 5 8 24 19 11

Find average TAT and WT, throughput.

Average TAT = 45/5 = 9


Average WT = 22/5=4.4
Throughput = 5/24
Contd…
Q. Find Average TAT, Average WT and throughput for non-premptive SJF and
preemptive SJF.
Process ID BT AT CT TAT WT

P1 6 2 9 7 1

P2 2 5 11 6 4

P3 8 1 23 22 14

P4 3 0 3 3 0

P5 4 4 15 11 7

Non-preemptive
 At time t=0, P4 is the only process present in ready queue, so assign P4 to
CPU. BT left for P4= 2.
 At time t=1, P3 arrived, Now select between P4 P3, Sort processes based
on BT ( P4(2) P3(8)) Assign P4 to CPU. BT left for P4= 1.
Contd…
At time t=2, P1 arrived, Now select between P3 P1 P4, Sort
processes based on BT (P4(1) P1(6) P3(8)), Assign P4 to CPU.
P4 completes its execution. P4 completes its execution.
At time t=3, No new process arrived, Select between P1 P3
in ready queue, Sort processes based on BT (P1(6) P3(8)),
Assign CPU to P1. P1 completes its execution.
At time t=9, Select between P3 P2 P5, Sort processes based
on BT (P2(2) P5(4) P3(8)), Assign CPU to processes in the
following order P2 P5 P3.

Average TAT= 49/5 = 9.8


Average WT = 26/5 = 5.2
Throughput = 5/23
Contd… Process ID BT AT CT TAT WT

P1 6 2 15 13 7

Preemptive P2 2 5 7 2 0

P3 8 1 23 22 14

P4 3 0 3 3 0

P5 4 4 10 6 2

At time t=0, P4 is the only process present in ready queue, so assign P4
to CPU. BT left for P4=2
At time t=1, P3 has arrived, Select between P3 P4, Sort processes based
on BT (P4(2) P3(8)), P4 will keep the CPU. BT left for P4=1
At time t=2, P1 has arrived, Select between P1 P3 P4, Sort processes
based on BT (P4(1) P1(6) P3(8)), P4 will keep the CPU. P4 completes its
execution.
Contd…
At time t=3, no new process, Select between P1 P3, Sort processes
based on BT (P1(6) P3(8)) Assign P1 to CPU. BT left for P1=5.
At time t=4, P5 has arrived, Select between P1 P3 P5, Sort processes
based on BT (P5(4) P1(5) P3(8)), Assign P5 to CPU. BT left for P5=3.
At time t=5, P2 has arrived, Select between P1 P3 P5 P2, sort processes
based on BT (P2(2) P5(3) P1(5) P3(8)), follow this sequence for
assignment of CPU.
Response Time (RT) P1: 3-2=1 P2: 5-5=0 P3:
15-1=14 P4: 0-0=0 P5: 4-4=0
Average TAT = 46/5=9.2
Average WT = 23/5=4.6
Throughput = 5/23
Thank You

You might also like