0% found this document useful (0 votes)
2 views

Module2 Os

The document discusses CPU scheduling in operating systems, detailing types of schedulers including long-term, short-term, and medium-term schedulers, along with their functions. It explains scheduling concepts such as preemptive and non-preemptive strategies, scheduling criteria, and various algorithms like FCFS, SJF, and priority scheduling. Additionally, it covers the CPU-I/O burst cycle, dispatcher roles, and the importance of maximizing CPU utilization and throughput while minimizing turnaround, waiting, and response times.

Uploaded by

Shashank S
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)
2 views

Module2 Os

The document discusses CPU scheduling in operating systems, detailing types of schedulers including long-term, short-term, and medium-term schedulers, along with their functions. It explains scheduling concepts such as preemptive and non-preemptive strategies, scheduling criteria, and various algorithms like FCFS, SJF, and priority scheduling. Additionally, it covers the CPU-I/O burst cycle, dispatcher roles, and the importance of maximizing CPU utilization and throughput while minimizing turnaround, waiting, and response times.

Uploaded by

Shashank S
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/ 116

Department: Information Science & Engineering

Course Name: OPERATING SYSTEMS (MODULE 2)


Module2
CPU Scheduling:
Basic Concepts, Preemptive strategies, Non preemptive strategies,
Scheduling Criteria, Scheduling algorithms, Multilevel Queue
Scheduling, Multilevel Feedback Queue Scheduling
Schedulers in OS-

• 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.
1. Long-Term Scheduler or Job Scheduler

• The job scheduler is another name for Long-Term scheduler. It selects


processes from the pool (or the secondary memory) and then
maintains them in the primary memory’s ready queue.
2. Short-term Scheduler or CPU Scheduler
• It is responsible for selecting one process from the ready state for
scheduling it on the running state.
• 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.
Medium-Term Scheduler

• It is responsible for suspending and resuming the process


• Medium-term scheduler swaps-out the processes from main memory
to secondary memory to free up the main memory when required.
• Thus, medium-term scheduler reduces the degree of
multiprogramming.
Basic Concepts
• Scheduling of this kind is a fundamental operating-system function.
Almost all computer resources are scheduled before use. The CPU is,
of course, one of the primary computer resources.

• CPU–I/O Burst Cycle


• CPU Scheduler
• Pre-emptive Scheduling
• Dispatcher
CPU–I/O Burst Cycle
• process execution consists of a
cycle of CPU execution and I/O
wait. Processes alternate
between these two states.
• Process execution begins with
a CPU burst That is followed by
an I/O burst, which is followed
by another CPU burst, then
another I/O burst, and so on.
• Eventually, the final CPU burst
ends with a system request to
terminate execution
CPU Scheduler
• Whenever the CPU becomes idle, the operating system must select
one of the processes in the ready queue to be executed. The selection
process is carried out by the short-term scheduler (or CPU scheduler).
• The scheduler selects a process from the processes in memory that
are ready to execute and allocates the CPU to that process
• Note that the ready queue is not necessarily a first-in, first-out (FIFO)
queue., a ready queue can be implemented as a FIFO queue, a
priority queue, a tree, or simply an unordered linked list. however, all
the processes in the ready queue are lined up waiting for a chance to
run on the CPU
Pre-emptive 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
example, as the result of an I/O request or an 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, at completion of I/O)
• 4. When a process terminates
For conditions 1 and 4 there is no choice - A new process must be selected.

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

takes that process to the desired state/queue

• The dispatcher is the module that gives a process control over the CPU after it has been

selected by the short-term scheduler. This function involves the following:

• Switching context

• Switching to user mode

• Jumping to the proper location in the user program to restart that program
The Difference between the Scheduler and Dispatcher

The CPU cannot execute all of these processes


simultaneously, so the operating system has to choose a
particular process on the basis of the scheduling algorithm
used. So, this procedure of selecting a process among
Dispatch Latency: various processes is done by the scheduler.

dispatcher who takes that process from the ready queue


The time taken by dispatcher is
and moves it into the running state
called dispatch latency.
Scheduling Criteria
There are several different criteria to consider when trying to select the "best" scheduling

algorithm for a particular situation and environment


(i) CPU utilization -
• CPU utilization refers to a computer’s usage of processing resources, or the amount of
work handled by a CPU
• Ideally the CPU would be busy 100% of the time, so as to waste 0 CPU cycles. On a real
system CPU usage should range from 40% ( lightly loaded ) to 90% ( heavily loaded. )
CPU Utilization= Useful Time/ Total Time
(ii) Throughput:
One measure of work is the number of processes that are completed per time unit, called
throughput.
Throughput= How many jobs completed/ total time
GOAL: Maximum CPU Utilization and Max Throughput
(iii) Various times related to process

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

When present in the system, a process is either waiting in the ready


queue for getting the CPU or it is executing on the CPU

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

• It is desirable to maximize CPU utilization and throughput and


• to minimize turnaround time, waiting time, and response time
Types of Scheduling
Non-Preemptive Scheduling Pre-emptive Scheduling

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

1. FCFS Scheduling 1. Shortest Remaining Time First


2. SJF Scheduling (SRTF)
3. Non Pre-emptive Priority 2. Priority Scheduling
3. Round Robin Scheduling
First- Come, First-Served (FCFS) 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:

If the CPU scheduling policy is FCFS, calculate the


average waiting time and average turn around time.
Answer
• Advantages-
• It is simple and easy to understand.
• It can be easily implemented using queue data structure.
• It does not lead to starvation.
• Disadvantages-
• It does not consider the priority or burst time of the processes.
• It suffers from convoy effect.
• Convoy Effect
In convoy effect,
• Consider processes with higher burst time arrived before the
processes with smaller burst time.
• Then, smaller processes have to wait for a long time for longer
processes to release the CPU.
Shortest-Job-First (SJF) Scheduling
• In SJF Scheduling, Out of all the available processes, CPU is assigned to the
process having smallest burst time.

• SJF Scheduling can be used in both preemptive and non-preemptive mode.


• Preemptive mode of Shortest Job First is called as Shortest Remaining Time First
(SRTF).
• Non-Preemptive: The CPU is allotted to the process until its completion.
• Preemptive: The CPU can be deallocated for the running process for some time
and again allocated to it.
Approach
• At t=0 ms,

• The only process that is available in the ready queue is P1 because


the arrival time for P1 is 0ms. Although the process with least burst
time (B.T.) is P4( with B.T. of 1 ms) but P4 is not available at t= 0 ms.
So we have to allocate CPU to P1.
Problem-02:
• 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 turn around time.

Solution-
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
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.

Process Id Arrival time Burst 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

According to the algorithm, we have to maintain the ready


queue and the Gantt chart. The structure of both the data
structures will be changed after every scheduling.
After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which
is lesser then the time quantum hence it will be completed.
Ready Queue
Since P6 is completed, hence it will not be added again to
the queue. There are only two processes present in the
ready queue. The Next process P2 requires only 2 units of
time
GANTT chart
P5 will get executed till completion.
solution
Problem-03:
solution
Time Quantum and Context Switch Time
The Advantages of Round Robin CPU Scheduling are:

• A fair amount of CPU is allocated to each job.


• Because it doesn't depend on the burst time, it can truly be implemented in the system.
• It is not affected by the convoy effect or the starvation problem as occurred in First Come First
Serve CPU Scheduling Algorithm.

Disadvantages

• Time quantum has a significant impact on its performance.


• The procedures cannot have priorities established.
• If time quantum is very high then RR degrades to FCFS.
• Setting the quantum too short increases the overhead and lowers the CPU efficiency, but setting it
too long may cause a poor response to short processes.
Multilevel Queue (MLQ) CPU Scheduling
• It may happen that processes in the ready queue can be divided into
different classes where each class has its own scheduling needs
• A common division is a foreground (interactive) process and
a background (batch) process. These two classes have different
scheduling needs. For this kind of situation, Multilevel Queue
Scheduling is used.
• A multilevel queue scheduling algorithm partitions the ready queue
into several separate queues
• The processes are permanently assigned to one queue, generally
based on some property of the process, such as memory size, process
priority, or process type.
• Each queue has its own scheduling algorithm. For example, separate
queues might be used for foreground and background processes. The
foreground queue might be scheduled by an RR algorithm, while the
background queue is scheduled by an FCFS algorithm
• In addition, there must be scheduling among the queues, which is
commonly implemented as fixed-priority preemptive scheduling. For
example, the foreground queue may have absolute priority over the
background queue.
Example
• Let's take an example of a multilevel queue-scheduling algorithm with
five queues to understand how this scheduling works:
• if there are no processes
on the higher priority
queue only then the
processes on the low
priority queues will run.
• Once processes on the
system queue, the
Interactive queue, and
Interactive editing queue
become empty, only then
•System Process – FCFS the processes on the
•Interactive Process – SJF
batch queue will run.
•Batch Process – RR
•Student Process – PB
• The Description of the processes in the above diagram is as follows:
• System Process
The Operating system itself has its own process to run and is termed as System
Process.
• These processes handle critical operations such as memory management, process
scheduling, I/O handling, and other system-level functions
• Interactive Process
The Interactive Process is a process in which there should be the same kind of
interaction.
• These processes are typically initiated by user interaction, such as keyboard input or
mouse clicks. Examples of interactive processes include running a text editor, web
browser, or a graphical application.
• Batch Processes
Batch processing is basically a technique in the Operating system that collects the
programs and data together in the form of the batch before the processing starts.
• Batch processes are non-interactive processes that are executed in the background
without direct user interaction.
• Batch processes are typically resource-intensive and can include tasks such as data
processing, large-scale computations, or scheduled backups.
• Student Process
The system process always gets the highest priority while the student processes
always get the lowest priority.
Advantages and Disadvantages of Multilevel Queue
Scheduling
• Advantages of Multilevel Queue Scheduling
• The Multilevel Queue Scheduling algorithm has a low scheduling
overhead.
• Different scheduling methods can be applied to different processes.
• Disadvantages of Multilevel Queue Scheduling
• It is not flexible in nature.
• Some processes may starve for CPU if some higher priority queues are
never becoming empty.
Multilevel Feedback Queue Scheduling
• when the multilevel queue scheduling algorithm is used, processes
are permanently assigned to a queue when they enter the system.
• If there are separate queues for foreground and background
processes, for example, processes do not move from one queue to
the other, since processes do not change their foreground or
background nature
• The multilevel feedback queue scheduling algorithm, in contrast,
allows a process to move between queues
• The idea is to separate processes according to the characteristics of
their CPU bursts.
• If a process uses too much CPU time, it will be moved to a lower-
priority queue.
• This scheme leaves I/O-bound and interactive processes in the higher-
priority queues.
• In addition, a process that waits too long in a lower-priority queue
may be moved to a higher-priority queue. This form of aging prevents
starvation
• For example, consider a multilevel feedback queue scheduler with three queues,
numbered from 0 to 2
• The scheduler first executes all processes in queue 0. Only when queue 0 is
empty will it execute processes in queue 1. Similarly, processes in queue 2 will
only be executed if queues 0 and 1 are empty
• A process that arrives for queue 1 will preempt a process in queue 2. A process in
queue 1 will in turn be preempted by a process arriving for queue 0
• A process entering the ready queue is put in queue 0. A process in
queue 0 is given a time quantum of 8 milliseconds. If it does not finish
within this time, it is moved to the tail of queue 1.
• If queue 0 is empty, the process at the head of queue 1 is given a
quantum of 16 milliseconds. If it does not complete, it is preempted
and is put into queue 2
• Processes in queue 2 are run on an FCFS basis but are run only when
queues 0 and 1 are empty
• This scheduling algorithm gives highest priority to any process with a
CPU burst of 8 milliseconds or less
• Processes that need more than 8 but less than 24 milliseconds are
also served quickly, although with lower priority than shorter
processes. Long processes automatically sink to queue 2 and are
served in FCFS order with any CPU cycles left over from queues 0 and
1.
Advantages and Disadvantages of Multilevel
Feedback Queue Scheduling
• Advantages:
• Low scheduling overhead.
• Allows aging, thus no starvation.
• Disadvantages:
• It’s not flexible.
• It also requires some means of selecting values for all the parameters
to define the best scheduler, thus it is also the most complex
GATE QUESTIONS ON SCHEDULING

You might also like