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

Unit3 - Scheduling Algorithms

Os scheduling algorithm

Uploaded by

vishnurajangs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit3 - Scheduling Algorithms

Os scheduling algorithm

Uploaded by

vishnurajangs
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT 2

CPU SCHEDULING

CPU SCHEDULING

CPU scheduling is the basis of multiprogrammed OS. If a process is waiting for an I/O request,
the CPU switches from that job to another job and so CPU is made busy always. By switching CPU
among processes, the OS makes the computer productive. CPU scheduling is fundamental and all
resources are scheduled before use. CPU Scheduling algorithms decides how the CPU will be allocated to
the process

CPU Scheduling algorithms are of 2 types

1. Non preemptive scheduling algorithms 2. Preemptive scheduling algorithms

1. Non preemptive scheduling algorithms

• When the process is assigned to a processor, the processor don’t release until the completion of
that process

• The CPU will be assigned to another process(job) only after the previous jobs gets completed

2. Preemptive scheduling algorithms

• The CPU can release the process even in the middle of the execution

Schedulers

The OS selects processes from these queues through a mechanism called scheduling and it is
done by a scheduler.

Different types of schedulers

1. Long-term scheduler (or job scheduler) 2.Short-term scheduler (or CPU scheduler)
3. Medium term Scheduler

1. Long-term scheduler (or job scheduler)

Long-term scheduler (or job scheduler) selects the jobs from the pool of jobs and load these
jobs into main memory(ready queue) of a computer

Eg: Consider a computer lab in a college. The lab has 30 dummy nodes and a server, and they are
connected by a LAN. Out of 30, 20 are trying to execute their jobs and these 20 are loaded into a spool
spool is a kind of buffer that holds the jobs for a device till the device is ready to accept the job)

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


• Long-term scheduler selects the jobs from the spool disk and loads these jobs in the ready queue
in the main memory

• A ready queue is a data structure with 2 ends namely front and rear

The jobs are loaded into the ready queue through the front and the jobs are deleted from the ready
queue through the rear

2. Short-term scheduler (or CPU scheduler)

• Short-term scheduler (or CPU scheduler) selects a job from the ready queue and give the
control of the CPU to that process with the help of ‘Dispatcher’

• The method of selecting a process from the ready queue by the short term scheduler depends on
the CPU scheduling algorithm

• Dispatcher is a module which connects the CPU to the process selected by the short term
scheduler

• The main function of the Dispatcher is switching the CPU from one process to another process

3. Medium-term scheduler

• If a process request an I/O in the middle of the execution, the process gets removed from the main
memory and loaded into the waiting queue

• When the I/O operation is completed, the job will be moved from the waiting queue to the ready
queue

• These 2 functions are done by the medium-term scheduler

Scheduling Criteria (Performance metrics of scheduling algorithms)

Scheduling Criteria

1. CPU utilization

CPU utilization denotes the % of time that the processor is busy. CPU utilization ranges from 0
to 100

2. Throughput

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


Throughput denotes the number of processes completed by the CPU within a time period

Throughput = Number of processes completed / Unit time

3. Turnaround time
Turnaround time is the amount of time to execute a particular process from the time of
submission through the time of completion

It is the sum of all periods spent waiting to get into memory, waiting in the ready queue,
executing on the CPU and doing I/O

4. Waiting time

Waiting time is the amount of time spent by a process in the ready queue

5. Response time

Response time is the amount of time it takes from when a request was submitted until the first
response occurs. It is the time duration between the submission and first response. Example: A process
enters into ready queue at 10:10 am. But the process got the first response from the CPU at 10:15am.
Response time is 5 minutes

Scheduling Algorithms

1. First-Come, First-Served (FCFS) Scheduling

2. Shortest-Job-First (SJF) Scheduling

 Preemptive SJF with varied arrival times called Shortest-remaining time First (SRT)
Scheduling

3. Priority Scheduling

 Preemptive & non preemptive

4. Round robin scheduling

5. Multi-level Queue Scheduling

6. Multilevel Feedback Queue Scheduling

1. First Come First Served Scheduling

 “The process that enters the ready queue first is served first”.

 The OS maintains a data structure that is a ready queue.

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


 If a process requests the CPU , then it is loaded into the ready queue, which process is the head of
the ready queue, connect the CPU to that process

Example:

Consider the following set of processes that arrive at time 0

1. Calculate the waiting time

[Burst time is the time required for the CPU to execute the jobs]

Gantt chart:

1. Average Turn around time

Turn around time (TAT) = Finish time – Arrival time

TAT for P1: 5-0=5

TAT for P2: 29-0=29

TAT for P3: 45-0=45

TAT for P4: 55-0=55

TAT for P5: 58-0=58

Average Turn Around Time = (5+29+45+55+58/5)= 38.4 ms


J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


2.Average Waiting time

Waiting time (WT) = Starting time – Arrival time

WT for P1: 0-0 =0

WT for P2: 5-0=5

WT for P3: 29-0=29

WT for P4: 45-0=45

WT for P5: 55-0=55

Average Waiting Time = (0+5+29+45+55/5)= 26.8 ms

3. Average Response time

Response time = First Response – Arrival time

RT for P1: 0-0 =0

RT for P2: 5-0=5

RT for P3: 29-0=29

RT for P4: 45-0=45

RT for P5: 55-0=55

Average Response Time = (0+5+29+45+55/5)= 26.8 ms

Note:

Average waiting time and average response time are same as it is a non preemptive scheduling
algorithm. In case of non-preemptive scheduling, once the CPU has been allocated to a process, that
process keeps the CPU till it releases the CPU. In the above example, the arrival time of all processes is 0

With arrival time

Consider the following set of processes that arrive at different times

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


1. Calculate the waiting time, response time and turn around time. [Burst time is the time required
for the CPU to execute the jobs]

Gantt chart:

1. Average Turn around time

Turn around time = Finish time – Arrival time

TAT for P1: 3-0=3

TAT for P2: 9-2=7

TAT for P3: 13-4=9

TAT for P4: 18-6=12

TAT for P5: 20-8=12

Average Turn Around Time = (3+7+9+12+12/5) = 8.6 ms

2.Average Waiting time

Waiting time = Starting time – Arrival time

WT for P1: 0-0 =0

WT for P2: 3-2=1

WT for P3: 9-4=5


J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


WT for P4: 13-6=7

WT for P5: 20-8=12

Average Waiting Time = (0+1+5+7+12/5)= 5 ms

3. Average Response time

Response time = First Response – Arrival time

RT for P1: 0-0 =0

RT for P2: 3-2=1

RT for P3: 9-4=5

RT for P4: 13-6=7

RT for P5: 18-8=10

Average Response Time = (0+1+5+7+10/5)= 4.6 ms

2. Shortest Job First Scheduling (SJF)

In Shortest Job First Scheduling (SJF), CPU will be assigned to a process with smallest CPU
burst . If 2 processes having the same CPU burst time, FCFC is used to break the tie.

Example:

Consider the following set of processes that arrive at time 0

1. Calculate the average waiting time and average turn around time.

Gantt chart:

Here p5 is assigned to CPU first as it has the least burst time, followed by P1,P4,P3 and P2

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


1. Average Turn around time

Turn around time = Finish time – Arrival time

TAT for P1: 8-0=8

TAT for P2: 58-0=58

TAT for P3: 34-0=34

TAT for P4: 18-0=18

RT for P5: 3-0=3

Average Turn Around Time = (8+58+34+18+3/5)= 24.2 ms

2. Average Waiting time

Waiting time = Starting time – Arrival time

WT for P1: 3-0 =3

WT for P2: 34-0=34

WT for P3: 18-0=18

WT for P4: 8-0=8

WT for P5: 0-0=0

Average Waiting Time = (3+34+18+8+0/5)= 12.6 ms

3. Average Response time

Response time = First Response time – Arrival time

RT for P1: 3-0 =3

RT for P2: 34-0=34

RT for P3: 18-0=18

RT for P4: 8-0=8

RT for P5: 0-0=0

Average Waiting Time = (3+34+18+8+0/5)= 12.6 ms

Shortest Remaining Time First (SRTF)

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


• It is the preemptive scheduling algorithm where the short term scheduler chooses the process
which has the shortest Remaining Processing Time

• When a new process joins the ready queue, the short term scheduler compares the remaining time
of executing process and new process

• If the new process has the least CPU burst time, the scheduler select that job and connect to CPU

Gantt chart:

Process P1 arrives at time 0 and gets executed first. Process P2 arrives at time 2 ms. When P2 arrives, the
remaining time for P1 to complete is 7 ms. But P2 needs 5 ms. As 5<7, p2 will be executed. P3 arrives at
time 3 ms. Now the RT of P2 =4 ms, As 4<6, P2 will continue. P5 & P6 arrive at time 5 and 6 ms. P4
needs 4 ms and hence executed. Then P3 will be executed as it needs 6 ms. Then P 1 and P5

1. Average Turn around time

Turn around time = Finish time – Arrival time

TAT for P1: 24-0=24

TAT for P2: 7-2=5

TAT for P3: 17-3=14

TAT for P4: 11-5=6

TAT for P5: 32-6=26

Average Turn Around Time = (24+5+14+6+26/5)= 15 ms

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


2. Average Waiting time

Waiting time = Starting time – Arrival time

WT for P1: (0-0) + (17-2) =15

WT for P2: 2-0=2

WT for P3: 11-3=8

WT for P4: 7-5=2

WT for P5: 24-6=18

Average Waiting Time = (15+2+8+2+18/5)= 9 ms

3. Round Robin Scheduling

• It is designed for time sharing systems

• The CPU switches between the processes

• When the time quantum is expired, the CPU switched to another job

• A small unit of time is called quantum or time slice and is generally from 10 to 100ms

Example:

Consider the set of processes p1,p2 and p3

1. Average Turn around time

Turn around time = Finish time – Arrival time

TAT for P1: 44-0=44

TAT for P2: 21-0=21

TAT for P3: 24-0=24


J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


Average Turn Around Time = (44+21+24)/3)= 29.67 ms

2. Average Waiting time

Waiting time = Starting time – Arrival time

WT for P1: (0-0)+(15-5)+(24-20)=14

WT for P2: (5-0)-(20-10)=15

WT for P3: (10-0) + (21-15) =16

Average Waiting Time =(14+15+16)/3)= 15 ms

3.Average Response time

Response time = First Response time – Arrival time

RT for P1: 0-0 =0

RT for P2: 5-0=5

RT for P3: 10-0=10

Average Waiting Time = (0+5+10/3) = 5 ms

4. Priority 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)

• Each priority process is scheduled in FCFS order

• Here, low numbers represent high priority

1. Priority Scheduling (Non preemptive) algorithm

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


Waiting time = Starting time – Arrival time

WT of P1: 3

WT of P2: 13

WT of P3: 25

WT of P4: 0

WT of P5:9

Average Waiting Time =(3+13+25+0+9)/5)= 10 ms

2. Average Turn around time

Turn around time = Finish time – Arrival time

TAT of P1: 9-0=0

TAT of P2: 25-0=25

TAT of P3: 26-0=26

TAT of P4: 3-0=3

TAT of P5: 13-0=13

Average Turn Around Time = (57/5)= 11.2 ms

2. Priority Scheduling (Preemptive) algorithm

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


1. Average Waiting time

Waiting time = Starting time – Arrival time

P1: (0-0) + (8-1) = 7

P2: (1-1) + (4-2) =2

P3: (2-2)=0

Average Waiting Time =9/3 = 3 ms

2. Average Turn around time

Turn around time = Finish time – Arrival time

P1: 9-0=0

P2: 25-0=25

P3: 26-0=26

P4: 3-0=3

P5: 13-0=13

Average Turn Around Time = (67/5)= 13.4 ms

Note:

Starvation:

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


The problem is indefinite blocking of a low priority process by a high priority process. This is
called starvation

Solution for avoiding starvation

Aging:

It is a technique of gradually increasing the priority of processes that wait in the system for a long
time. Thus the older processes attain high priority and are ensured of completion in a finite time.

5. Multi-level Queue Scheduling

• The ready queue is partitioned into a number of ready queues

• Each ready queue is capable to load same type of jobs and each ready queue has its own
scheduling algorithm

Example:

Ready queue is partitioned into 4 ready queues.

1 – system process (may follow FCFS algorithm)

2 – for foreground process(Interactive process, may follow SJF)

3– for background process(Batch process, may follow Round Robin)

4– for student process(may follow SRT

In Fig., the system process queue has the highest priority. The student process queue has the
lowest priority. No process in the student queue can run unless the queues for system process, foreground

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only


process and background process are all empty. If any foreground process entered the ready queue and a
background process was running, the batch process would be preempted.

6. Multi-level Feedback Queue Scheduling

• In multi-level feedback queue scheduling, a process can move between the various queues

• In this algorithm, processes are not permanently assigned to queues, instead they are allowed to
move between the queues

• The decision to move a process between queues is based on the time of its execution and waiting
time

• If a process uses more CPU time, it is moved to a lower priority queue

• A process which waits for a long time in a lower priority queue is moved to a higher priority
queue in order to avoid starvation

Consider a multilevel feedback queue scheduler with 3 queues say Q1, Q2 and Q3. Q1, Q2
employ RR scheduling algorithm with time quantum of 8 ms and 16 ms, while in Q3, the processes are
scheduled in FCFS order. The scheduler first executes all processes in Q1, followed by Q2 and Q3. While
executing processes in Q2, if a new process arrives in Q1, Q2 is preempted and Q1 starts executed. Also a
process arriving in Q2 preempts a process executing in Q3

J. Paul Rajasingh, Assistant Professor / CSE, SRM Institute of Science & Technology, Tiruchirappalli Campus

For Internal Circulation Only

You might also like