0% found this document useful (0 votes)
9 views39 pages

Unit 5 (CPU Scheduling)

The document discusses CPU scheduling, a critical function of operating systems that determines the order of process execution to optimize CPU utilization and system efficiency. It outlines two main types of scheduling: preemptive and non-preemptive, along with various algorithms such as FCFS, SJF, and Round Robin, detailing their advantages and disadvantages. Additionally, it covers important scheduling criteria, terminologies, and the complexities of multiprocessor and thread scheduling.

Uploaded by

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

Unit 5 (CPU Scheduling)

The document discusses CPU scheduling, a critical function of operating systems that determines the order of process execution to optimize CPU utilization and system efficiency. It outlines two main types of scheduling: preemptive and non-preemptive, along with various algorithms such as FCFS, SJF, and Round Robin, detailing their advantages and disadvantages. Additionally, it covers important scheduling criteria, terminologies, and the complexities of multiprocessor and thread scheduling.

Uploaded by

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

CPU Scheduling

Operating System
(BCA 4th Sem)
Er. Preeti Sharma
(Assistant professor)
Scheduling
•Scheduling is an fundamental operating system function. It is a set of
policies and mechanisms built into the operating system that
governs the order in which the work to be done by computer
system.
CPU scheduling
•CPU scheduling is a process which allows one process to use the CPU
while the execution of another process is on hold(in waiting state)
due to unavailability of any resource like I/O etc, thereby making full
use of CPU. The aim of CPU scheduling is to make the system efficient,
fast and fair.
•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 from among the processes in
memory that are ready to execute, and allocates the CPU to one
of them.
Types of CPU scheduling
• Preemptive Scheduling
•In Preemptive Scheduling, the tasks are mostly assigned with their priorities.
Sometimes it is important to run a task with a higher priority before another lower
priority task, even if the lower priority task is still running. The lower priority task
holds for some time and resumes when the higher priority task finishes its
execution.eg. SRTF, LRTF, ROUND ROBIN, PRIORITY BASED.
• Non-Preemptive Scheduling
•In this type of scheduling method, the CPU has been allocated to a specific
process. The process that keeps the CPU busy will release the CPU either
by
switching context or terminating. It is the only method that can be used
for
various hardware platforms. That's because it doesn't need special hardware (for
example, a timer) like preemptive scheduling.EG. FCFS
•SJF, LJF, HRRN,MULTILEVEL QUEUE, PRIORITY BASED.
Preemptive versus Non- preemptive
preemptive Non-preemptive
A scheduling discipline is preemptive if the CPU can A scheduling discipline is non- preemptive if once a
be taken away from a process after being allocated. process has been given the CPU, the CPU cannot be
taken away from that process.
In other words, even if the CPU has been allocated to a In other words, we can say in non preemptive
certain process, it can be snatched from his process scheduling once the CPU has been allocated to a
any time either due to time constraint or due to process, the process keeps the CPU until it release the
priority reason. CPU either by terminating or by switching to the
waiting stage.

Its costly as compared to non preemptive scheduling. Its not costly as compared to preemptive scheduling.
Context switching also involves overhead in
preemptive scheduling.
Higher priority jobs are processed before the lower In non-preemptive systems, short jobs are made to
priority jobs. wait by longer jobs.
When scheduling is Preemptive or
Non-Preemptive?
To determine if scheduling is preemptive or non-preemptive, consider
these four parameters:
•A process switches from the running to the waiting state.
•Specific process switches from the running state to the ready state.
•Specific process switches from the waiting state to the ready state.
•Process finished its execution and terminated.
• Only conditions 1 and 4 apply, the scheduling is called non-
preemptive.
• All other scheduling are preemptive.
Important CPU scheduling Terminologies

• Burst Time/Execution Time: It is a time required by the process to complete


execution. It is also called running time.
• Arrival Time: when a process enters in a ready state
• Finish Time: when process complete and exit from a system
• Multiprogramming: A number of programs which can be present in memory at
the same time.
• Jobs: It is a type of program without any kind of user interaction.
• User: It is a kind of program having user interaction.
• Process: It is the reference that is used for both job and user.
• CPU/IO burst cycle: Characterizes process execution, which alternates between
CPU and I/O activity. CPU times are usually shorter than the time of I/O.
CPU Scheduling Criteria
A CPU scheduling algorithm tries to maximize and minimize the following:
• Maximize:
• CPU utilization: CPU utilization is the main task in which the operating
system needs to make sure that CPU remains as busy as possible. The level
of CPU utilization depends on the load on the system. It can range from 0 to
100 percent. However, for the RTOS, it can be range from 40 percent for
low-level and 90 percent for the high-level system.
• Throughput: The number of processes that finish their execution per unit
time is known Throughput. So, when the CPU is busy executing the
process, at that time, work is being done, and the work completed per unit
time is called Throughput. For long processes it may be 1 process per hour,
for short processes it may be 10 process per second.
Ctnd…..
• Minimize:
• Waiting time: Waiting time is an amount that specific process needs to wait in the ready queue. It
presents more accurate measure of performance as compared to turnaround time.
• Response time: It is a time interval between the job submission and the first response produced by
the job.
• Turnaround Time: Turnaround time is an amount of time to execute a specific process. It is the
calculation of the total time spent waiting to get into the memory, waiting in the queue and,
executing on the CPU. The period between the time of process submission to the completion time is
the turnaround time. It is inversely proportional to throughput.
• Predictability: A given job should run in about the same amount of time and at about the same cost
regardless of the load on system.
• Fairness: fairness refers to the degree to which all processes are given equal opportunity to
execute.
• Most of the multi-programmed operating system uses some form of a timer to prevent a process
from tying up the system forever.
What is Dispatcher?
•It is a module that provides control of the CPU to the process. The
Dispatcher should be fast so that it can run on every context
switch. Dispatch latency is the amount of time needed by the
CPU scheduler to stop one process and start another.
•Functions performed by Dispatcher:
•Context Switching
•Switching to user mode
•Moving to the correct location in the newly loaded program.
Scheduling Algorithms/Schemes
•CPU scheduling algorithm deal with the problem of deciding which of
the processes in the ready queue is to be allocated the CPU.
•Six commonly used scheduling algorithms are:
1. First-come-First-Served (FCFS)
2. Shortest job first (SJF)
3. Priority Scheduling
4. Round-Robin Scheduling (RR)
5. Multi-level Queue Scheduling (MLQ)
6. Multi-level Feedback Queue Scheduling (MFQ)
1. First-come-First-Served (FCFS)
• FCFS is very simple - Just a FIFO queue, like customers waiting in line at the
bank or the post office or at a copying machine.
• In this scheduling, the process that request the CPU first is allocated the
CPU first.
• We can say that in FCFS scheduling, a process is allocated CPU
time according to the arrival time of a process.
• The FCFS policy is easily managed with a FIFO queue.
• When CPU is free, it is allocated to the process at the head of ready queue.
The running process is then removed from the queue.
• FCFS scheduling algorithm is non-preemptive. Once the CPU is allocated to
a process, that process keeps the CPU until it releases the CPU, either by
terminating or by requesting I/O.
• Advantages of FCFS Scheduling
•It is simple to understand and code.
•It is suitable for batch system where one process execute after the other.
• Disadvantages of FCFS Scheduling
•Waiting time is larger if short request wait behind the longer process.
•FCFS is not suited for time sharing system.
The average waiting time depend on the order in which processes arrive.
2. Shortest Job First (SJF)
•SJF is also known as Shortest job next (SJN) algorithm and is
faster than FCFS.
•In SJF, the process with less execution time is selected from the
ready queue for execution.
•If two processes have the same length then FCFS scheduling
algorithm is used to break the tie.
•SJF algorithm can be preemptive or non-preemptive.
•In Non-Preemptive SJF Scheduling, CPU is assigned to the
process and the process keeps the CPU with it until it
terminates.
• Preemptive SJF Scheduling is also known as Shortest
Remaining time (SRT) scheduling and useful in time sharing
system.
•In Preemptive SJF Scheduling or SRT, if new process time is
less, then currently running process is preempted and CPU is
allocated to new process.
•The execution time of process must be known in advance. Calculating execution
time of a process without executing it is difficult.
•SRT favours short jobs, the mean waiting time for longer processes is more
longer.
•SRT has high overhead than SJF because it must keep track of the elapsed service
time of the running job.
Example: Non-preemptive SJF
Example: Preemptive SJF
3. Priority Scheduling
• In Priority scheduling, a priority is associated with all processes.
• Process are executed in sequence according to their priority.
• The CPU is allocated to the process with highest priority.
• When two or more processes have same priority then FCFS
scheduling is performed.
• Highest number represents the highest priority or in some system low
number indicates a high priority.
• Priority can be defined in two ways: internally or externally.
• Priority of a process can be defined by the user or an application or by
the OS itself.
• When a user or an application process defined the priority of a
process is called externally defined priority.
• When an OS assigns the priority of a process is called internally
defined priority. It depend upon time limits, memory requirements
etc.
•Priority scheduling can be preemptive or non-
preemptive.
•In Preemptive Priority scheduling, allocates the CPU
to the new process, if the priority of new process is
higher than the priority of running process.
•In Non- Preemptive Priority scheduling, the running
process is not interrupted even if the new process has a
higher priority. In this case the new process is placed at
the head of ready queue.
• Advantage-
• Processes are execute according to their importance.
• Disadvantage-
• A low priority processes can be blocked infinitely if high priority
process arrive in the ready queue.
4. Round Robin Scheduling (RR)
•Round Robin Scheduling is similar to FCFS but preemption
is added to switch between processes.
•In RR Scheduling, processes are dispatched in FIFO but are
given a small amount of CPU time. This small amount of
time
is known as time quantum or time slice. Time quantum
is from 10 to 100 milliseconds.
•If a process does not complete before its time slice
expires,
the CPU is preempted and is given to next waiting process
in ready queue.
•The preempted process is then placed at the tail of the
ready queue.
•If a process is completed before its time slice expires,
the process itself releases the CPU.
•Whenever a new process arrives in the system it is added
to the tail of the ready queue.
•Size of time quantum or slice
•If the time slice is large then this algorithm become
same as FCFS and performance
degrades
•If the time slice is very small then the number of context
switches increase and CPU will be busy in switching from
one process to another rather than performing calculations
.
•Thus, the time quantum should not be very large and also
should not be too small to achieve good system
performance.
5. Multi-Level Queue Scheduling(MLQ)
•In this scheduling, ready queue is divided into various
queue that are called subqueue.
•Each subqueue has its own scheduling algorithm. For e.g.
interactive processes at the foreground may use RR
scheduling while batch jobs at the background may use
FCFS method.
•It classifies the processes according to their types and
permanently assigned to a queue on entry to the
system.
•In addition, there is a scheduling algorithm that
works globally between the different subqueue.
•Each queue has absolute priority over low priority
queue. No
process in batch queue could run unless the queue for
system and interactive processes were all empty.
•If an interactive process enter the ready queue while a batch
process was running, the batch process would be
MLQ Scheduling
• Advantages
•Processes are permanently assigned to a queue and do not
change their queue so this algorithm has lower
scheduling overhead.

• Disadvantages
•It is inflexible as the processes can never change their
queue.
•Lower priority processes waits for indefinite time if some
higher priority queue are never becoming empty.
6. Multi-Level Feedback Queue Scheduling(MFQ)
•MFQ Scheduling is an enhancement of multi-level
queue scheduling. In this scheme, processes can
move between the different queues.
•The various processes are separated in different
queue on the basis of their CPU execution
time.
•If a process consumes a lot of CPU time, it is placed
into a lower priority queue.
•If a process waits too long in a lower priority queue
it is moved into higher priority queue.
•Each queue has given a different CPU time slice.
•If process does not finish within the time slice, it is
moved to the tail of next queue.
MFQ Scheduling
• Advantages
•More flexible
•Allow the processes to move between different queue
•A process that waits too long in lower priority queue may
be moved to a higher priority queue.
• Disadvantages
•Most complex scheduling algorithm
•Moving the processes around the queue produces
more CPU overhead.
Multiprocessor Scheduling
• Symmetric Multiprocessing System in which CPUs share system resources, and CPU
communicate to one another by sharing information in same memory space. This
is also known as Tightly Coupled Architecture.
• Parallel Processing System in which each CPU has its own resources and own memory
space. Thus CPU communicate externally using some type of network. This is known
as Loosely Coupled Architecture.
• In some type of multiprocessor system, classification is done on the basis of CPU used.
When Multiprocessor system uses different types of CPU, it is called as
Heterogeneous System whereas Homogeneous System uses identical CPUs.
•CPU scheduling in symmetric Multiprocessor systems is
almost similar to that of uniprocessor system. In such a
system, a single global ready queue is used by all the
CPUs.
•In addition to this, each CPU may have its own local
ready queue in order to avoid traffic.
•The processes from the global ready queue are
distributed to local queue.
•There are various way in which multiprocessor system
can be connected and operate. These are:
1. Separate system
2. Coordinated job scheduling
3. Master/Slave scheduling
4. Homogeneous scheduling
1. Separate system
• In this multiprocessor system, there are two or more separate system, each
with one processor, some main memory and peripheral devices.
This is just like having two or more separate computer system.
2. Coordinated job scheduling
• It is also known as loosely coupled multiprocessing, each processor is
associated with a separate system.
• When a job arrive it assign to any processor which is having less load.
3. Master/Slave scheduling
• There is one main processor called master and rest of all processor are
slaves.
• Master processor maintain the status of all processes and processor in the
system.
• Master processor selects a process to be run on available processor and
slave start execution, when process need I/O resources , slave
generate interrupt to the master processor and stop to await further
orders.
• It is not fair as it does not give equal importance to all processes.
4. Homogeneous scheduling
• In this system all processors are treated equally i.e. there is no master no
slave.
• Each processor uses the same scheduling algorithm to select the next
process to run.
Thread Scheduling
•A thread is an independent sequence of instruction
that can only be executed within the scope of a
process.
•A process can have more than one threads.
•There are two different methods to schedule
the execution of the thread:
1. The CPU is directly assigned to thread is called one-level
scheduling and same as CPU scheduling.
2. The CPU is first assigned to process and then the thread
within each process share the CPU for the time period
allocated to the process and is called two-level scheduling.
•Thread management and scheduling is done at
two different levels:
1. At Application level (User-level)
2. At Operating System level (Kernel-level)
• User Level Thread Scheduling ( Two-Level Scheduling)
•Each process gets the CPU for specific amount
of time.
•All the threads of a process are scheduled within this
amount of time.
•When there is no ready threads CPU is taken
away from that process.
•No preemption is necessary during
thread scheduling within a process.
Factor FSFC Round robin SJF SRT
Definition Process that Processes are Shortest process i.e. Process with
requests CPU first is allocated CPU turn with least burst time smallest estimated
allocated the CPU by turn one after gets CPU first. run time to
first the other. completion gets CPU
first

Decision mode Non- preemptive Non- preemptive Non- preemptive Non- preemptive
Throughput Not emphasized May be low if High High
quantum is too
small
Response time May be high Provides good Provides good Provides good
especially if there is response time for response time for response time
a large variance in short processes short processes
process execution
time.

Overhead Minimum Minimum Can be high Can be high


Effect and process Penalizes short Fair treatment Penalizes long Penalizes long
process; processes processes
Starvation No No Possible possible

You might also like