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

CPU SCHEDULING

CPU scheduling is essential in multiprogramming environments where multiple processes compete for CPU time, necessitating a scheduler and various scheduling algorithms. These algorithms can be categorized into preemptive and non-preemptive types, with criteria such as CPU utilization, throughput, turnaround time, and fairness influencing their effectiveness. Different environments, such as batch, interactive, and real-time systems, require tailored scheduling approaches to optimize performance and user satisfaction.

Uploaded by

kemi29204
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)
27 views

CPU SCHEDULING

CPU scheduling is essential in multiprogramming environments where multiple processes compete for CPU time, necessitating a scheduler and various scheduling algorithms. These algorithms can be categorized into preemptive and non-preemptive types, with criteria such as CPU utilization, throughput, turnaround time, and fairness influencing their effectiveness. Different environments, such as batch, interactive, and real-time systems, require tailored scheduling approaches to optimize performance and user satisfaction.

Uploaded by

kemi29204
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/ 21

CPU SCHEDULING

Brief introduction

In multiprogramming environments, multiple processes or threads compete for

the CPU at the same time. When two or more of them are in the ready state, the

CPU has to choose which to run next. This is referred to as scheduling.

The part of the OS that does this is the scheduler and the algorithm it uses is

called the scheduling algorithm. Because the CPU is a scarce resource, a good

scheduler can make a big difference in perceived performance and user

satisfaction.

The thing with scheduling however is that there is no one size fits all approach.

Different computing environments call for different ways to go about

scheduling. Personal computers and handheld devices (e.g. mobile phones) may

not be as concerned about scheduling as networked systems or mainframes.

When to schedule

Processes can be CPU-bound (meaning that they require more CPU time or

have long CPU bursts, and lesser I/O waits) or I/O bound (meaning they have

short CPU bursts and frequent I/O waits). Therefore, the scheduler is saddled
with the responsibility of making scheduling decisions, which could be in any

of the following cases:

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 a child process)

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

Preemptive and Non-preemptive scheduling algorithms

Under non-preemptive scheduling, once the CPU has been allocated to a

process, it gets to keep the CPU until it releases it either by terminating or by

switching to the waiting state. In effect, no scheduling decisions are made

during clock interrupts because after clock interrupt processing has been

finished, the process that was running before the interrupt is resumed unless

there is one with higher priority.


Preemptive scheduling algorithms pick a process and let it run for a maximum

of some fixed time. If the process has not finished executing, it is suspended

and the scheduler picks another available process to run. A clock interrupt

occurs at the end of the time interval to give the control of the CPU back to the

scheduler.

Scheduling criteria

CPU scheduling goals are generally environment-dependent. However, some of

them are desirable in all cases. The choice of a particular algorithm may favor

one class of processes over another. However, scheduling criteria include but

are no limited to the following:

1. CPU utilization: This scheduling criteria measures how busy the CPU is.

CPU utilization is to be maximized because we want to keep the CPU as

busy as possible. It is usually expressed as a percentage.

2. Throughput: This is the measure of the number of processes that are

completed per unit time. This basically means that the through put measures

the number of jobs or processes processed by the CPU at a particular time.

This is particularly important in batch systems. The goal is usually to

maximize throughput.
3. Turnaround time: The interval from the arrival of a process until its

execution and exit is called its turnaround time. We seek to minimize the

turnaround time of a process.

4. Waiting time: This is the sum of the periods spent waiting in the ready

queue. The CPU scheduling algorithm should have a goal of minimizing the

waiting time of a process.

5. Response time: This is the measure of the time of submission of a process

until the time a first response is produced. It is a desirable feature to

minimize response time in interactive systems as turnaround time may not

be the best scheduling criterion for them.

Others scheduling criteria include:

6. Fairness: This implies that each process must be given a fair share of the

CPU.

7. Balance: This means that all parts of the system are to be kept busy as the

scheduling algorithm runs.

8. Policy enforcement: It is important for a scheduling algorithm to see that a

stated policy is carried out.


Categories of scheduling algorithms

Different environments require different scheduling algorithms. We will

consider three categories:

1. Batch systems: They are mainly used in the business world. They usually do

not have users waiting for a quick response at their terminal. Therefore, non-

preemptive or preemptive algorithms with long time periods for each

process are often acceptable

2. Interactive systems: In these systems, preemption is essential to avoid one

process holding onto the CPU indefinitely and preventing others from

accessing it.

3. Real time systems: In real time environments, processes may not run for

long periods of time as they usually do their work and block quickly.

Scheduling Algorithms

1. First-Come, First-Served Scheduling: This is the simplest scheduling

algorithm and it is non-preemptive. As the name implies, processes are assigned

the CPU in the order in which they arrive or request it. The processes are

managed with a first in first out queue. As more processes arrive, they are

attached to the end of the queue. Blocked processes that become ready are also

attached to the tail of the queue just like a newly arriving process. When the
CPU is free, it is assigned to the process at the head of the queue and the

process is run to completion.

Advantages

 It is simple to understand and implement

 It is suitable for batch systems.

Disadvantages

 The average waiting time of this algorithm is usually long.

 The average waiting time may also vary if the processes’ CPU burst

times vary greatly.

 When one big CPU bound process arrives first and gets hold of the CPU,

it hinders other processes from accessing the CPU until it gets off the

CPU, meaning that those processes outside the CPU will have to wait

indefinitely. This is called the convoy effect. This leads to poor CPU

utilization.

2. Shortest Job First Scheduling:

Shortest Job First scheduling works on the process with the shortest burst

time or duration first. This is the best approach to minimize waiting time. It is used

in Batch Systems.

It is of two types:
1. Non Pre-emptive

2. Pre-emptive

 To successfully implement it, the burst time/duration time of the processes

should be known to the processor in advance, which is practically not

feasible all the time.

 This scheduling algorithm is optimal if all the jobs/processes are available at

the same time.

Non Pre-emptive Shortest Job First

Consider the below processes available in the ready queue for execution,

with arrival time as 0 for all and given burst times.


As you can see in the GANTT chart above, the process P4 will be picked up first

as it has the shortest burst time, then P2, followed by P3 and at last P1.

Problem with Non Pre-emptive SJF

If the arrival time for processes are different, which means all the processes are not

available in the ready queue at time 0, and some jobs arrive after some time, in

such situation, sometimes process with short burst time have to wait for the current

process's execution to finish, because in Non Pre-emptive SJF, on arrival of a


process with short duration, the existing job/process's execution is not

halted/stopped to execute the short job first.

This leads to the problem of Starvation, where a shorter process has to wait for a

long time until the current longer process gets executed. This happens if shorter

jobs keep coming, but this can be solved using the concept of aging.

What is aging?

This a situation in which the priority of a process is increased based on its waiting

time. The longer the process waits, the higher its priority. At a point, the priority

would become high enough for the process to eventually be scheduled to run.

Pre-emptive Shortest Job First

In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as they

arrive, but as a process with short burst time arrives, the existing process is

preempted or removed from execution, and the shorter job is executed first.

3. Round Robin Scheduling:

Round Robin(RR) scheduling algorithm is mainly designed for time-sharing

systems. This algorithm is similar to FCFS scheduling, but in Round Robin(RR)


scheduling, preemption is added which enables the system to switch between

processes.

 A fixed time is allotted to each process, called a quantum, for execution.

 Once a process is executed for the given time period that process is

preempted and another process executes for the given time period.

 Context switching is used to save states of preempted processes.

 This algorithm is simple and easy to implement and the most important is

thing is this algorithm is starvation-free as all processes get a fair share of

CPU.

 It is important to note here that the length of time quantum is generally from

10 to 100 milliseconds in length.

Some important characteristics of the Round Robin(RR) Algorithm are as follows:

1. Round Robin Scheduling algorithm falls under the category of Preemptive

Algorithms.

2. This algorithm is one of the oldest, easiest, and fairest algorithms.

3. This Algorithm is a real-time algorithm because it responds to the event

within a specific time limit.


4. In this algorithm, the time slice should be the minimum that is assigned to a

specific task that needs to be processed. Though it may vary for different

operating systems.

5. This is a hybrid model and is clock-driven in nature.

6. This is a widely used scheduling method in the traditional operating system.

Important terms

1. Completion Time

It is the time at which any process completes its execution.

2. Turn Around Time

This mainly indicates the time Difference between completion time and

arrival time. The Formula to calculate the same is: Turn Around Time =

Completion Time – Arrival Time

3. Waiting Time(W.T):

It Indicates the time Difference between turnaround time and burst time.

And is calculated as Waiting Time = Turn Around Time – Burst Time

Example:
In the above diagram, arrival time is not mentioned so it is taken as 0 for all

processes.

Note: If arrival time is not given for any problem statement then it is taken as 0 for

all processes; if it is given then the problem can be solved accordingly.

Explanation

The value of time quantum in the above example is 5.Let us now calculate the

turnaround time and waiting time for the above example:


Turn Around Time Waiting Time
Burst
Processes
Turn Around Time = Completion Waiting Time = Turn
Time
Time – Arrival Time Around Time – Burst Time

P1 21 32-0=32 32-21=11

P2 3 8-0=8 8-3=5

P3 6 21-0=21 21-6=15

P4 2 15-0=15 15-2=13

Average waiting time is calculated by adding the waiting time of all processes and

then dividing them by number of processes.

Average waiting time = waiting time of all processes/ no.of processes

Average waiting time=11+5+15+13/4 = 44/4= 11ms

4. Priority Scheduling:

In priority scheduling, CPU scheduling is done based on priority. Priority in this

case can be internally or externally set. The process which is most urgent is
processed first, followed by the ones with lesser priority in order. Processes with

same priority are executed in FCFS manner.

The priority of process, when internally defined, can be decided based on memory

requirements, time limits, number of open files, ratio of I/O burst to CPU burst etc.

Whereas, external priorities are set based on criteria outside the operating system,

like the importance of the process, funds paid for the computer resource use,

market factors etc.

Types of Priority Scheduling Algorithm

Priority scheduling can be of two types:

1. Preemptive Priority Scheduling: If the new process arrived at the ready

queue has a higher priority than the currently running process, the CPU is

preempted, which means the processing of the current process is stopped and

the incoming new process with higher priority gets the CPU for its

execution.

2. Non-Preemptive Priority Scheduling: In case of non-preemptive priority

scheduling algorithm if a new process arrives with a higher priority than the

current running process, the incoming process is put at the head of the ready
queue, which means after the execution of the current process it will be

processed.

Example of Priority Scheduling Algorithm

Consider the below table for processes with their respective CPU burst times and

the priorities.

In the Gantt chart, we see that processes are given CPU time just on the basis of

their priorities.
Problem with Priority Scheduling Algorithm

Chances of indefinite blocking or starvation exist in priority scheduling algorithm.

A process is considered blocked when it is ready to run but has to wait for the CPU

as some other process is running currently.

However, in priority scheduling, if new higher priority processes keep coming in

the ready queue then the processes waiting in the ready queue with lower priority

may have to wait for long durations before getting the CPU for execution. This is

referred to as starvation.

Using Aging Technique with Priority Scheduling

To prevent starvation of any process, we can use the concept of aging where we

keep on increasing the priority of low-priority process based on its waiting time.

For example, if we decide the aging factor to be 0.5 for each day of waiting, then if

a process with priority 20 comes in the ready queue. After one day of waiting, its

priority is increased to 19.5 and so on.


Doing so, we can ensure that no process will have to wait for indefinite time for

getting CPU time for processing.

5. Multilevel Queue Scheduling:

Another class of scheduling algorithms has been created for situations in which

processes are easily classified into different groups.

For example, A common division is made between foreground (or interactive)

processes and background (or batch) processes. These two types of processes have

different response-time requirements, and so might have different scheduling

needs. In addition, foreground processes may have priority over background

processes.

Multi-level 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 the Round Robin

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.

Let us consider an example of a multilevel queue-scheduling algorithm with five

queues:

1. System Processes

2. Interactive Processes

3. Interactive Editing Processes

4. Batch Processes

5. Student Processes

Each queue has absolute priority over lower-priority queues. No process in the

batch queue, for example, could run unless the queues for system processes,

interactive processes, and interactive editing processes were all empty. If an

interactive editing process entered the ready queue while a batch process was

running, the batch process will be preempted.


In this case, if there are no processes on the higher priority queue only then the

processes on the low priority queues will run. For Example: Once processes on the

system queue, the Interactive queue, and Interactive editing queue become empty,

only then the processes on the batch queue will run.

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.

 Interactive Process

The Interactive Process is a process in which there should be the same kind

of interaction (basically an online game ).


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

 Student Process

The system process always gets the highest priority while the student

processes always get the lowest priority.

In an operating system, there are many processes, in order to obtain the result we

cannot put all processes in a queue; thus this process is solved by Multilevel queue

scheduling.

Advantages of Multilevel Queue Scheduling

With the help of this scheduling we can apply various kind of scheduling for

different kind of processes:

For System Processes: First Come First Serve(FCFS) Scheduling.

For Interactive Processes: Shortest Job First (SJF) Scheduling.

For Batch Processes: Round Robin(RR) Scheduling

For Student Processes: Priority Scheduling


Disadvantages of Multilevel Queue Scheduling

The main disadvantage of Multilevel Queue Scheduling is the problem of

starvation for lower-level processes.

Let us understand what is starvation?

Starvation:

Due to starvation lower-level processes either never execute or have to wait for a

long amount of time because of lower priority or higher priority process taking a

large amount of time.

You might also like