OS-Module2.2-Process Scheduling

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 93

Chapter 5: Process Scheduling

Chapter 5: Process Scheduling

Basic Concepts

Scheduling Criteria

Scheduling Algorithms

Thread Scheduling

Multiple-Processor Scheduling
To introduce CPU scheduling, which is the basis for
multiprogrammed operating systems.

To describe various CPU-scheduling algorithms.

To discuss evaluation criteria for selecting a CPU-scheduling


algorithm for a particular system.
Basic Concepts
In a single-processor system,
Only one process may run at a time.

Other processes must wait until the CPU is


rescheduled.

Objective of multiprogramming:
To have some process running at all times, in order to
maximize CPU utilization.
Basic Concepts
Scheduling of processes/work is done to finish the work on time.

Whenever the CPU becomes idle, the operating system must select one of
the processes in the line ready for launch.

The selection process is done by a temporary (CPU) scheduler.

The Scheduler selects between memory processes ready to launch and


assigns the CPU to one of them.

CPU Scheduling is a process that allows one process to use the CPU while
another process is delayed (in standby) due to unavailability of any
resources such as I / O etc, thus making full use of the CPU.
Why do we need to schedule processes?

•Scheduling is important in many different computer environments. One of the


most important areas is scheduling which programs will work on the CPU.

•This task is handled by the Operating System (OS) of the computer and there
are many different ways in which we can choose to configure programs.

•Process Scheduling allows the OS to allocate CPU time for each process.

• Another important reason to use a process scheduling system is that it keeps


the CPU busy at all times. This allows you to get less response time for
programs.

The purpose of CPU Scheduling is to make the system more efficient,


faster, and fairer.
Alternating Sequence of CPU And I/O Bursts
CPU Scheduler
CPU scheduling is the process of deciding which process will own the
CPU to use while another process is suspended.

CPU scheduling decisions may take place when a process:


1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.

Scheduling under 1 and 4 is non-preemptive.

All other scheduling is preemptive.


Categories of Scheduling in OS

There are two categories of scheduling:

1. Non-preemptive: In non-preemptive, the resource can’t be taken from a


process until the process completes execution.
The switching of resources occurs when the running process terminates and
moves to a waiting state.

2. Preemptive: In preemptive scheduling, the OS allocates the resources to a


process for a fixed amount of time.

During resource allocation, the process switches from running state to ready
state or from waiting state to ready state.

This switching occurs as the CPU may give priority to other processes and
replace the process with higher priority with the running process.
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible.
Theoretically, CPU usage can range from 0 to 100 but in a real-time
system, it varies from 40 to 90 percent depending on the system load.

Throughput – # of processes that complete their execution per time unit.


The output may vary depending on the length or duration of the
processes.

Turnaround time – amount of time to execute a particular process.

Waiting time – amount of time a process has been waiting in the ready
queue.

Response time – amount of time it takes from when a request was


submitted until the first response is produced, not output (for time-sharing
environment).
Scheduling Algorithm Optimization Criteria
Objectives of Process Scheduling Algorithm:

• Utilization of CPU at maximum level. Keep CPU as busy as possible.


Allocation of CPU should be fair.

• Throughput should be Maximum. i.e. Number of processes that


complete their execution per time unit should be maximized.

• Minimum turnaround time, i.e. time taken by a process to finish


execution should be the least.

• There should be a minimum waiting time and the process should not
starve in the ready queue.

• Minimum response time. It means that the time when a process


produces the first response should be as less as possible.
Different terminologies to take care of in any CPU Scheduling algorithm

• Arrival Time: Time at which the process arrives in the ready queue.

• Completion Time: Time at which process completes its execution.

• Burst Time: Time required by a process for CPU execution.

• Turn Around Time: Time Difference between completion time and arrival time.

Turn Around Time = Completion Time – Arrival Time

• Waiting Time(W.T): Time Difference between turn around time and burst time.

Waiting Time = Turn Around Time – Burst Time


Different types of CPU Scheduling Algorithms
First-Come, First-Served (FCFS) Scheduling
FCFS considered to be the simplest of all operating system scheduling
algorithms.

First come first serve scheduling algorithm states that the process that requests
the CPU first is allocated the CPU first and is implemented by using FIFO queue.

Characteristics of FCFS:

• FCFS supports non-preemptive and preemptive CPU scheduling algorithms.

• Tasks are always executed on a First-come, First-serve concept.

• FCFS is easy to implement and use.

• This algorithm is not much efficient in performance, and the wait time is quite
high.
First-Come, First-Served (FCFS) Scheduling
Advantages of FCFS:

• Easy to implement.

• First come, first serve method.

Disadvantages of FCFS:

• FCFS suffers from Convoy effect.

• The average waiting time is much higher than the other algorithms.

• FCFS is very simple and easy to implement and hence not much efficient.
First-Come, First-Served (FCFS) Scheduling

Process Burst Time


P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

Waiting time for P1 = 0; P2 = 24; P3 = 27


Average waiting time: (0 + 24 + 27)/3 = 17
First-Come, First-Served (FCFS) Scheduling

Suppose that the processes arrive in the order


P2 , P3 , P1
The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

Waiting time for P1 = 6; P2 = 0; P3 = 3


Average waiting time: (6 + 0 + 3)/3 = 3
Much better than previous case
Convoy effect short process behind long process
Consider the following table of arrival time and burst time for five
processes P1, P2, P3, P4 and P5.

Processes Arrival Time Burst Time

P1 0 4

P2 1 3

P3 2 1

P4 3 2

P5 4 5
Shortest Job First (SJF) Scheduling

Shortest Job First (SJF) is an algorithm in which the process having the
smallest execution time is chosen for the next execution.

This scheduling method can be preemptive or non-preemptive.

It significantly reduces the average waiting time for other processes awaiting
execution.
Characteristics of SJF Scheduling

•It is associated with each job as a unit of time to complete.

•This algorithm method is helpful for batch-type processing, where waiting for
jobs to complete is not critical.

•It can improve process throughput by making sure that shorter jobs are
executed first, hence possibly have a short turnaround time.

There are basically two types of SJF methods:

• Non-Preemptive SJF

• Preemptive SJF
Non-Preemptive SJF

In non-preemptive scheduling, once the CPU cycle is allocated to process, the


process holds it till it reaches a waiting state or terminated.

Consider the following five processes each having its own unique burst time and
arrival time.

Process Queue Burst time Arrival time

P1 6 2

P2 2 5

P3 8 1

P4 3 0

P5 4 4
Step 0) At time=0, P4 arrives and starts execution.

Step 1) At time= 1, Process P3 arrives. But, P4 still needs 2 execution units to


complete. It will continue execution.
Step 2) At time =2, process P1 arrives and is added to the waiting queue. P4 will
continue execution.

Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1
is compared. Process P1 is executed because its burst time is less compared to P3.
Step 4) At time = 4, process P5 arrives and is added to the waiting queue. P1 will
continue execution.

Step 5) At time = 5, process P2 arrives and is added to the waiting queue. P1


will continue execution.
Step 6) At time = 9, process P1 will finish its execution. The burst time of P3, P5,
and P2 is compared. Process P2 is executed because its burst time is the lowest.

Step 7) At time=10, P2 is executing and P3 and P5 are in the waiting queue.


Step 8) At time = 11, process P2 will finish its execution. The burst time of P3 and
P5 is compared. Process P5 is executed because its burst time is lower.

Step 9) At time = 15, process P5 will finish its execution.


Step 10) At time = 23, process P3 will finish its execution.

Step 11) Let’s calculate the average waiting time for above example.

Wait time
Average Waiting Time=
P4= 0-0=0
P1= 3-2=1 0+1+4+7+14/5 = 26/5 = 5.2

P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
Preemptive 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.

Consider the following five process:

process Queue Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 0) At time=0, P4 arrives and starts execution.

Step 1) At time= 1, Process P3 arrives. But, P4 still needs 2 execution units to


complete. It will continue execution.
Step 2) At time = 2, process P1 arrives with burst time = 6. The burst time is
more than that of P4. Hence, P4 will continue execution.

Step 3) At time = 3, process P4 will finish its execution. The burst time of P3 and P1
is compared. Process P1 is executed because its burst time is less compared to P3.
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.

Process Queue Burst time Arrival time


5 out of 6 is
P1 2
remaining
P2 2 5
P3 8 1
P4 3 0
P5 4 4
Step 5) At time = 5, process P2 will arrive. The burst time of P1, P2, P3, and P5
is compared. Process P2 is executed because its burst time is least. Process P5
is preempted.

Process Queue Burst time Arrival time


5 out of 6 is
P1 2
remaining
P2 2 5
P3 8 1
P4 3 0
3 out of 4 is
P5 4
remaining
Step 6) At time =6, P2 is executing.

Step 7) At time =7, P2 finishes its execution. The burst time of P1, P3, and P5 is
compared. Process P5 is executed because its burst time is lesser.
Process Queue Burst time Arrival time
5 out of 6 is
P1 2
remaining
P2 2 5
P3 8 1
P4 3 0
3 out of 4 is
P5 4
remaining
Step 8) At time =10, P5 will finish its execution. The burst time of P1 and P3 is
compared. Process P1 is executed because its burst time is less.
Step 9) At time =15, P1 finishes its execution. P3 is the only process left. It will
start execution.

Step 10) At time =23, P3 finishes its execution.


Step 11) Let’s calculate the average waiting time for above example.

Wait time Average Waiting Time =


0+7+0+2+14/5 = 23/5 =4.6
P4= 0-0=0
P1= (3-2) + 6 =7
P2= 5-5 = 0
P5= 4-4+2 =2
P3= 15-1 = 14
Advantages of SJF
Here are the benefits/pros of using SJF method:

• SJF is frequently used for long term scheduling.

• It reduces the average waiting time over FIFO (First in First Out) algorithm.

• SJF method gives the lowest average waiting time for a specific set of
processes.

• It is appropriate for the jobs running in batch, where run times are known in
advance.

•For the batch system of long-term scheduling, a burst time estimate can be
obtained from the job description.

•For Short-Term Scheduling, we need to predict the value of the next burst time.

•Probably optimal with regard to average turnaround time.


Disadvantages/Cons of SJF
Here are some drawbacks/cons of SJF algorithm:

• Job completion time must be known earlier, but it is hard to predict.

• It is often used in a batch system for long term scheduling.

• SJF can’t be implemented for CPU scheduling for the short term. It is because
there is no specific method to predict the length of the upcoming CPU burst.

• This algorithm may cause very long turnaround times or starvation.

• Requires knowledge of how long a process or job will run.

• It leads to the starvation that does not reduce average turnaround time.

• It is hard to know the length of the upcoming CPU request.

• Elapsed time should be recorded, that results in more overhead on the processor.
we have 4 processes with process Id P0, P1, P2, and P3. The arrival time and
burst time of the processes are given in the following table.

Process ID Burst Time Arrival Time

P0 8 5

P1 5 0

P2 9 4

P3 2 1
Non-Preemptive SJF Scheduling:

Process Burst Arrival Completion Waiting Turnaround


ID Time Time time Time Time

P0 8 5 21 8 16
P1 5 0 5 0 5
P2 9 4 16 3 12
P3 2 1 7 4 6
The waiting time and turnaround time are calculated with the help of the
following formula.

Waiting Time = Turnaround time – Burst Time

Turnaround Time = Completion time – Arrival time

Process waiting time:


P0= 16-8=8
P1= 5-5=0
P2=12-9=3
P3=6-2=4 Average waiting time= 8+0+3+4/4
=15/4
=3.75

Process turnaround time:


P0=21-5=16
P1=5-0=5
P2=16-4=12
P3=7-1=6 Average turnaround time=16+5+12+6/4
=39/4
=9.75
Preemptive SJF Scheduling

we have 4 processes with process ID P1, P2, P3, and P4. The arrival time and
burst time of the processes are given in the following table.

Process Burst time Arrival time

P1 18 0

P2 4 1

P3 7 2

P4 2 3
Preemptive SJF Scheduling
we have 4 processes with process ID P1, P2, P3, and P4. The arrival time and
burst time of the processes are given in the following table.

Arrival Completion Waiting


Process Burst time Turnaround time
time time time

P1 18 0 31 31 13

P2 4 1 5 4 0

P3 7 2 14 12 5

P4 2 3 7 4 2

The GANTT chart of preemptive shortest job first scheduling is:


The waiting time and turnaround time are calculated with the help of the
following formula.
Waiting Time = Turnaround time – Burst Time

Turnaround Time = Completion time – Arrival time

Process waiting time:


P1=31-18=13
P2=4-4=0
P3=12-7=5
P4=4-2=2 Average waiting time= 13+0+5+2/4
=20

Process Turnaround Time:


P1=31-0=31
P2=5-1=4
P3=14-2=12
P4=7-3=4 Average turnaround time= 31+4+12+4/4
=12.75
Priority Scheduling

Priority Scheduling is a method of scheduling processes that is based on


priority.

In this algorithm, the scheduler selects the tasks to work as per the priority.

The processes with higher priority should be carried out first, whereas jobs with
equal priorities are carried out on a round-robin or FCFS basis.

Priority depends upon memory requirements, time requirements, etc.


Types of Priority Scheduling
Priority scheduling divided into two main types:

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.

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.
Characteristics of Priority Scheduling

•A CPU algorithm that schedules processes based on priority.

•It used in Operating systems for performing batch processes.

•If two jobs having the same priority are READY, it works on a FIRST COME,
FIRST SERVED basis.

•In priority scheduling, a number is assigned to each process that indicates its
priority level.

•Lower the number, higher is the priority.

•In this type of scheduling algorithm, if a newer process arrives, that is having a
higher priority than the currently running process, then the currently running
process is preempted.
Example of Priority Scheduling

Consider following five processes P1 to P5. Each process has its unique
priority, burst time, and arrival time.

Process Priority Burst time Arrival time

P1 1 4 0

P2 2 3 0

P3 1 7 6

P4 3 4 11

P5 2 2 12
Step 0) At time=0, Process P1 and P2 arrive. P1 has higher priority than P2.
The execution begins with process P1, which has burst time 4.

Step 1) At time=1, no new process arrive. Execution continues with P1.


Step 2) At time 2, no new process arrives, so you can continue with P1. P2 is
in the waiting queue.

Step 3) At time 3, no new process arrives so you can continue with P1. P2
process still in the waiting queue.
Step 4) At time 4, P1 has finished its execution. P2 starts execution.

Step 5) At time= 5, no new process arrives, so we continue with P2.


Step 6) At time=6, P3 arrives. P3 is at higher priority (1) compared to P2
having priority (2). P2 is preempted, and P3 begins its execution.
Burst Arrival
Process Priority
time time
P1 1 4 0
1 out of 3
P2 2 0
pending
P3 1 7 6
P4 3 4 11
P5 2 2 12
Step 7) At time 7, no-new process arrives, so we continue with P3. P2 is in
the waiting queue.

Step 8) At time= 8, no new process arrives, so we can continue with P3.


Step 9) At time= 9, no new process comes so we can continue with P3.

Step 10) At time interval 10, no new process comes, so we continue with
P3
Step 11) At time=11, P4 arrives with priority 4. P3 has higher priority, so it
continues its execution.

Burst Arrival
Process Priority
time time
P1 1 4 0
1 out of 3
P2 2 0
pending
2 out of 7
P3 1 6
pending
P4 3 4 11
P5 2 2 12
Step 12) At time=12, P5 arrives. P3 has higher priority, so it continues
execution.

Step 13) At time=13, P3 completes execution. We have P2,P4,P5 in ready


queue. P2 and P5 have equal priority. Arrival time of P2 is before P5. So P2
starts execution.
Burst Arrival
Process Priority
time time
P1 1 4 0
1 out of
P2 2 3 0
pending
P3 1 7 6
P4 3 4 11
P5 2 2 12
Step 14) At time =14, the P2 process has finished its execution. P4 and P5
are in the waiting state. P5 has the highest priority and starts execution.

Step 15) At time =15, P5 continues execution.


Step 16) At time= 16, P5 is finished with its execution. P4 is the only process
left. It starts execution.

Step 17) At time =20, P5 has completed execution and no process is left.
Step 18) Let’s calculate the average waiting time for the above example.

Waiting Time = start time – arrival time + wait time for next burst

P1 = o - o = o

P2 =4 - o + 7 =11

P3= 6-6=0

P4= 16-11=5

Average Waiting time = (0+11+0+5+2)/5 = 18/5= 3.6


Advantages of priority scheduling
• Easy to use scheduling method.

• Processes are executed on the basis of priority so high priority does not need to
wait for long which saves time.

• This method provides a good mechanism where the relative important of each
process may be precisely defined.

• Suitable for applications with fluctuating time and resource requirements.

Disadvantages of priority scheduling


• If the system eventually crashes, all low priority processes get lost.

• If high priority processes take lots of CPU time, then the lower priority processes
may starve and will be postponed for an indefinite time.

• scheduling algorithm may leave some low priority processes waiting indefinitely.

• If a new higher priority process keeps on coming in the ready queue, then the
process which is in the waiting state may need to wait for a long duration of
time.
Round Robin Scheduling
• The name of this algorithm comes from the round-robin principle, where each
person gets an equal share of something in turns.

• It is the oldest, simplest scheduling algorithm, which is mostly used for


multitasking.

• In Round-robin scheduling, each ready task runs turn by turn only in a cyclic
queue for a limited time slice.

• This algorithm also offers starvation free execution of processes.


Characteristics of Round-Robin Scheduling

• Round robin is a pre-emptive algorithm.

• The CPU is shifted to the next process after fixed interval time, which is called
time quantum/time slice.

• The process that is preempted is added to the end of the queue.

• Round robin is a hybrid model which is clock-driven.

• Time slice should be minimum, which is assigned for a specific task that needs
to be processed. However, it may differ OS to OS.

• It is a real time algorithm which responds to the event within a specific time
limit.

•Widely used scheduling method in traditional OS.


Example of Round-robin Scheduling
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.

Process ID Arrival Time Burst Time


1 0 5
2 1 6
3 2 3
4 3 1
5 4 5
6 6 4

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.
Ready Queue:

Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4
units. Hence in the ready queue, there will be only one process P1 at starting with
CPU burst time 5 units.

P1
5

GANTT chart
The P1 will be executed for 4 units first.
Ready Queue

Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives
in the ready queue.

P1 has not completed yet, it needs another 1 unit of time hence it will also be
added back to the ready queue.

P2 P3 P4 P5 P1
6 3 1 5 1

GANTT chart

After P1, P2 will be executed for 4 units of time which is shown in the Gantt
chart.
Ready Queue

During the execution of P2, one more process P6 is arrived in the ready queue.
Since P2 has not completed yet hence, P2 will also be added back to the ready
queue with the remaining burst time 2 units.

P3 P4 P5 P1 P6 P2
3 1 5 1 4 2

GANTT chart

After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time
is only 3 seconds.
Ready Queue
Since P3 has been completed, hence it will be terminated and not be added to
the ready queue. The next process will be executed is P4.

P4 P5 P1 P6 P2
1 5 1 4 2

GANTT chart
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
The next process in the ready queue is P5 with 5 units of burst time. Since P4 is
completed hence it will not be added back to the queue.

P5 P1 P6 P2
5 1 4 2

GANTT chart
P5 will be executed for the whole time slice because it requires 5 units of burst
time which is higher than the time slice.
Ready Queue

P5 has not been completed yet; it will be added back to the queue with the
remaining burst time of 1 unit.

P1 P6 P2 P5
1 4 2 1

GANTT Chart
The process P1 will be given the next turn to complete its execution. Since it only
requires 1 unit of burst time hence it will be completed.
Ready Queue

P1 is completed and will not be added back to the ready queue. The next process
P6 requires only 4 units of burst time and it will be executed next.

P6 P2 P5
4 2 1

GANTT chart
P6 will be executed for 4 units of time till completion.
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.

P2 P5
2 1

GANTT Chart

P2 will get executed again, since it only requires only 2 units of time hence this will
be complete
Ready Queue

Now, the only available process in the queue is P5 which requires 1 unit of burst
time. Since the time slice is of 4 units hence it will be completed in the next burst.

P5
1

GANTT chart

P5 will get executed till completion.


The completion time, Turnaround time and waiting time will be calculated as
shown in the table below.

Process ID Arrival Burst Time Completion Turn Waiting


Time Time Around Time
Time

1 0 5 17 17 12
2 1 6 23 22 16
3 2 3 11 9 6
4 3 1 12 9 8
5 4 5 24 20 15
6 6 4 21 15 11

Avg Waiting Time = (12+16+6+8+15+11)/6 = 68/6 units


we have 4 processes with process ID P1, P2, P3, and P4. The arrival time and
burst time of the proceses are given in the following table. (The Quantum
time is 6).

Process ID Arriving Time Burst Time

P1 0 8

P2 1 5

P3 2 10

P4 3 11
we have 4 processes with process ID P1, P2, P3, and P4. The arrival time and
burst time of the proceses are given in the following table. (The Quantum
time is 6).

Arriving Burst Completion Turnaround


Process ID Waiting
Time Time Time Time
Time

P1 0 8 25 25
17

P2 1 5 11 10
5

P3 2 10 29 27
17

3
P4 11 34 31
20
Process Turnaround Time
P1= 25-0 =0
P2= 11-1=10
P3= 29-2 = 27
P4= 34-3=31 Average Turnaround Time= 0+10+27+31/4
= 68/4
=17
Process Waiting Time
P1= 25-8=17
P2=10-5=5
P3=27-10=17
P4=31-11=20 Average Waiting Time=17+5+17+20/4
=59/4
=14.75
Multilevel Queue Scheduling
Each algorithm supports a different process, but in a general system, some
processes require scheduling using a priority algorithm.

• While some processes want to stay in the system (interactive processes),


others are background processes whose execution can be delayed.

• There are two sorts of processes that require different scheduling algorithms
because they have varying response times and resource requirements.

• The foreground (interactive) and background processes (batch process)


are distinguished. Background processes take priority over foreground
processes.
• The ready queue has been partitioned into seven different queues using the
multilevel queue scheduling technique.

• These processes are assigned to one queue based on their priority, such as
memory size, process priority, or type.

• The method for scheduling each queue is different.

• Some queues are utilized for the foreground process, while others are used
for the background process.

• The foreground queue may be scheduled using a round-robin


method, and the background queue can be scheduled using
an FCFS strategy.
Advantages and Disadvantages of Multilevel Queue
Scheduling

Advantages

• You can use multilevel queue scheduling to apply different scheduling methods
to distinct processes.

• It will have low overhead in terms of scheduling.

Disadvantages

• There is a risk of starvation for lower priority processes.

• It is rigid in nature.
Example

Let's take an example of a multilevel queue-scheduling


algorithm with five queues to understand how this
scheduling works:
1.System process:
The OS has its process to execute, which is referred to as the System
Process.

2.Interactive processes:
It is a process in which the same type of interaction should occur.

3.Interactive editing processes:

4.Batch processes:
Batch processing is an operating system feature that collects programs and
data into a batch before processing starts.

5.Student processes:
The system process is always given the highest priority, whereas the student
processes are always given the lowest.
Every queue would have an absolute priority over the low-priority queues. No
process may execute until the high-priority queues are empty.

In the above instance, no other process may execute until and unless the
queues for system, interactive, and editing processes are empty.

If an interactive editing process enters the ready queue while a batch


process is underway, the batch process will be preempted.
Example Problem

Let's take an example of a multilevel queue-scheduling (MQS) algorithm that


shows how the multilevel queue scheduling work.

Consider the four processes listed in the table below under multilevel queue
scheduling. The queue number denotes the process's queue.

Process Arrival Time CPU Burst Time Queue Number

P1 0 4 1
P2 0 3 1
P3 0 8 2
P4 10 5 4

Queue 1 has a higher priority than queue 2.

Round Robin is used in queue 1 (Time Quantum = 2),

while FCFS is used in queue 2.


Working:

1.Both queues have been processed at the start. Therefore, queue 1 (P1, P2) runs
first (due to greater priority) in a round-robin way and finishes after 7 units.

2.The process in queue 2 (Process P3) starts running (since there is no process
in queue 1), but while it is executing, P4 enters queue 1 and interrupts P3, and
then P3 takes the CPU and finishes its execution.
Multilevel Feedback Scheduling
Each algorithm supports a different process, but some processes require
scheduling using a priority algorithm in a general system.

There is a different queue for foreground or background operations, but they


do not switch between queues or change their foreground or background
nature; this type of organization benefits from low scheduling but is inflexible.

This strategy prioritizes operations that require I/O and are interactive.

It is a distinct process with a distinct CPU burst time.

It enables a process to switch between queues.

If a process consumes too much processor time, it will be switched to the lowest
priority queue.

A process waiting in a lower priority queue for too long may be shifted to a higher
priority queue. This type of aging prevents starvation.
Multilevel Feedback Scheduling

The parameters of the multilevel feedback queue scheduler are as follows:

The scheduling algorithm for every queue in the system.

The queues number in the system.

The method for determining when a queue should be demoted to a lower-priority


queue.

When a process is upgraded to a higher-priority queue, this process determines


when it gets upgraded.

The method for determining which processes will enter the queue and when those
processes will require service
Multilevel Feedback Scheduling
Example of MFQS:
Taking three queues: Q1, Q2, and Q3.

Time quantum Q1= 9 milliseconds


Time quantum Q2= 18 milliseconds
Q3 FCFS

Criteria of Multilevel Feedback Queue :

Firstly, all processes in Q1 are executed.

When Q1 is empty, processes in Q2 are executed.

When Q1 and Q2 are empty, processes in Q3 will be executed.

If a process arrives for Q2, it will preempt the process in Q3. Similarly, if a process
that arrives for Q3 will preempt the current process in Q3.

Processes in Q3 run on an FCFS basis.


Advantages
• It is more flexible.

• It allows different processes to move between different queues.

• It prevents starvation by moving a process that waits too long for the lower
priority queue to the higher priority queue.

Disadvantages

• For the selection of the best scheduler, it requires some other means to select
the values.

• It produces more CPU overheads.

• It is the most complex algorithm.


Multiple Processor Scheduling
• If multiple CPUs are available, the scheduling problem becomes more
complex.
Two approaches:
Asymmetric Multiprocessing
• A master server is a single processor responsible for all scheduling
decisions, I/O processing and other system activities.

• The other processors execute only user code.

• Advantage: This is simple because only one processor accesses the system
data structures, reducing the need for data sharing.

Symmetric Multiprocessing
• Each processor is self-scheduling.

• To do scheduling, the scheduler for each processor.

• Examines the ready-queue and Selects a process to execute.


Multiple Processor Scheduling
• Restriction: We must ensure that two processors do not choose the same
process and that processes are not lost from the queue.

Processor Affinity
• In SMP systems,
1. Migration of processes from one processor to another are avoided and
2. Instead processes are kept running on same processor. This is known as
processor affinity.
• Two forms:
Soft Affinity
When an OS try to keep a process on one processor because of policy,
but cannot guarantee it will happen.

It is possible for a process to migrate between processors.

Hard Affinity
When an OS have the ability to allow a process to specify that it is not to
migrate to other processors. Eg: Solaris OS
Multiple Processor Scheduling
Load Balancing
This attempts to keep the workload evenly distributed across all
processors in an SMP system.
•Two approaches:

1. Push Migration
A specific task periodically checks the load on each processor and if it finds an
imbalance, it evenly distributes the load to idle processors.
2. Pull Migration
An idle processor pulls a waiting task from a busy processor.

Symmetric Multithreading
1. Create multiple logical processors on the same physical processor.

2. Present a view of several logical processors to the OS.


• Each logical processor has its own architecture state, which includes
general- purpose and machine-state registers.
• Each logical processor is responsible for its own interrupt handling.
• SMT is a feature provided in hardware, not software.

You might also like