You are on page 1of 15

ITEC55 Platform Technologies

Module 4 Scheduling Algorithm

Prepared by:
MAC JOHN T. POBLETE
Faculty, Information Technology Department
Cavite State University Naic
Email: nc.macjohn.poblete@cvsu.edu.ph
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

In this module, the students must be able to:


a. Determine the different CPU scheduling
b. Explain the purpose of CPU scheduling
c. Differentiate preemptive and non-preemptive scheduling
d. Make a gantt chart based from the processes
e. Compute for average waiting time, average turnaround time, throughput and CPU Utilization

Instructions:
1. Read carefully all instruction for each task before working on the requirements.
2. All questions related to requirements shall be coursed through email only. Replies will be sent
during office hours.

Pretest:
Assume you have the following jobs to execute with one processor, with the jobs arriving in the order listed
here:

a. Suppose a system uses FCFS scheduling. Create a Gantt chart illustrating the execution of
these processes?
b. What is the turnaround time for process p3?
c. What is the average wait time for the processes?

LET US LEARN!
What is Preemptive Scheduling?
Preemptive Scheduling is a scheduling method where 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.
At that time, the lower priority task holds for some time and resumes when the higher priority task finishes its
execution.

Advantages of Preemptive Scheduling


➢ Preemptive scheduling method is more robust, approach so one process cannot monopolize the CPU
➢ Choice of running task reconsidered after each interruption.
➢ Each event cause interruption of running tasks
➢ The OS makes sure that CPU usage is the same by all running process.
➢ In this, the usage of CPU is the same, i.e., all the running processes will make use of CPU equally.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

➢ This scheduling method also improvises the average response time.


➢ Preemptive Scheduling is beneficial when we use it for the multi-programming environment.

Disadvantages of Preemptive Scheduling


➢ Need limited computational resources for Scheduling
➢ Takes a higher time by the scheduler to suspend the running task, switch the context, and dispatch the new
incoming task.
➢ The process which has low priority needs to wait for a longer time if some high priority processes arrive
continuously.

What is 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
specialized hardware (for example, a timer) like preemptive Scheduling.
Non-Preemptive Scheduling occurs when a process voluntarily enters the wait state or terminates.

Advantages of Non-preemptive Scheduling


➢ Offers low scheduling overhead
➢ Tends to offer high throughput
➢ It is conceptually very simple method
➢ Less computational resources need for Scheduling

Disadvantages of Non-preemptive Scheduling


➢ It can lead to starvation especially for those real-time tasks
➢ Bugs can cause a machine to freeze up
➢ It can make real-time and priority Scheduling difficult
➢ Poor response time for processes

Difference Between Preemptive and Non-Preemptive Scheduling in OS


Preemptive Scheduling Non-preemptive Scheduling
A processor can be preempted to execute the Once the processor starts its execution, it must finish it
different processes in the middle of any current before executing the other. It can’t be paused in the
process execution. middle.
CPU utilization is more efficient compared to Non- CPU utilization is less efficient compared to preemptive
Preemptive Scheduling. Scheduling.
Waiting and response time of preemptive Scheduling Waiting and response time of the non-preemptive
is less. Scheduling method is higher.
Preemptive Scheduling is prioritized. The highest When any process enters the state of running, the state of
priority process is a process that is currently utilized. that process is never deleted from the scheduler until it
finishes its job.
Preemptive Scheduling is flexible. Non-preemptive Scheduling is rigid.
Examples: – Shortest Remaining Time First, Round Examples: First Come First Serve, Shortest Job First,
Robin, etc. Priority Scheduling, etc.
Preemptive Scheduling algorithm can be pre-empted In non-preemptive scheduling process cannot be
that is the process can be Scheduled Scheduled
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

In this process, the CPU is allocated to the processes In this process, CPU is allocated to the process until it
for a specific time period. terminates or switches to the waiting state.
Preemptive algorithm has the overhead of switching Non-preemptive Scheduling has no such overhead of
the process from the ready state to the running state switching the process from running into the ready state.
and vice-versa.

First Come First Serve (FCFS)


First Come First Serve (FCFS) is an operating system scheduling algorithm that automatically
executes queued requests and processes in order of their arrival. It is the easiest and simplest CPU
scheduling algorithm. In this type of algorithm, processes which requests the CPU first get the CPU allocation
first. This is managed with a FIFO queue. The full form of FCFS is First Come First Serve.
As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of the
queue and, when the CPU becomes free, it should be assigned to the process at the beginning of the queue.

Characteristics of FCFS method


➢ It supports non-preemptive and pre-emptive scheduling algorithm.
➢ Jobs are always executed on a first-come, first-serve basis.
➢ It is easy to implement and use.
➢ This method is poor in performance, and the general wait time is quite high.

FCFS Example
Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time

P1 3 4

P2 5 3

P3 0 2

P4 5 1

P5 4 3

If the CPU scheduling policy is FCFS, calculate the average waiting time and average turnaround time,
throughput.
Gantt Chart
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

Completion Turnaround
Process ID Arrival time Burst time Waiting Time
Time Time

P1 3 4 7 4 0

P2 5 3 13 8 5

P3 0 2 2 2 0

P4 5 1 14 9 8

P5 4 3 10 6 3

Average 5.8 3.2


Throughput: (4+3+2+1+3)/5=2.6
CPU Utilization: (13/14)*100=92.86%
Note:
➢ Turn Around time = Completion time – Arrival time
➢ Waiting time = Turn Around time – Burst time
➢ Throughput = Sum of Burst time/ # of processes
➢ CPU Utilization = (Sum of Burst Time/ Completion Time) * 100

Shortest Job First (SJF)


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. The full form of SJF is Shortest Job
First.
There are basically two types of SJF methods:
➢ Non-Preemptive SJF
➢ Preemptive SJF (Shortest Remaining Time First)

Characteristics of SJF method


➢ 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.
➢ It improves job output by offering shorter jobs, which should be executed first, which mostly have a shorter
turnaround time.

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 ID Burst time Arrival time
P1 6 2
P2 2 5
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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) Complete the table in order to solve for AWT, ATA, throughput and CPU utilization.
Completion Turnaround
Process ID Arrival time Burst time Waiting Time
Time Time

P1 2 6 9 7 1

P2 5 2 11 6 4

P3 1 8 23 22 14

P4 0 3 3 3 0

P5 4 4 15 11 7

Average 9.8 5.2


ITEC55 Platform Technologies Module 4 Scheduling Algorithm

Throughput: 4.6
CPU Utilization: 100%

Preemptive SJF (Shortest Remaining Time First - SRTF)


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.
Process Queue Burst time Arrival time
P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 1) At time= 1, Process P3 arrives. But, P4 has a shorter burst time. 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 lower.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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
P1 5 out of 6 is remaining 2
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
P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4

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
P1 5 out of 6 is remaining 2
P2 2 5
P3 8 1
P4 3 0
P5 3 out of 4 is remaining 4
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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) Complete the table in order to solve for AWT, ATA, throughput and CPU utilization.
Completion Turnaround
Process ID Arrival time Burst time Waiting Time
Time Time

P1 2 6 15 13 7

P2 5 2 7 2 0

P3 1 8 23 22 14

P4 0 3 3 3 0

P5 4 4 10 6 2

Average 9.2 4.6


Throughput: 4.6
CPU Utilization: 100%

Round Robin (RR)


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.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

Characteristics of Round Robin


➢ 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.
➢ Round robin is one of the oldest, fairest, and easiest algorithms.
➢ Widely used scheduling method in traditional OS.

Round Robin 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.
Process ID Arrival Time Burst Time
P1 0 5
P2 1 6
P3 2 3
P4 3 1
P5 4 5
P6 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

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
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

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

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

GANTT chart
P5 will get executed till completion.
ITEC55 Platform Technologies Module 4 Scheduling Algorithm

The completion time, Turnaround time and waiting time will be calculated as shown in the table below.
Process ID Arrival Time Burst Time Completion Turn Around Waiting Time
Time Time
P1 0 5 17 17 12
P2 1 6 23 22 16
P3 2 3 11 9 6
P4 3 1 12 9 8
P5 4 5 24 20 15
P6 6 4 21 15 11
Average 15.33 11.33

Throughput: 4
CPU Utilization: 100%

Post-Test
Solve for average waiting time, average turnaround time, throughput and CPU Utilization using the table
below. Use FCFS, SJF, SRTF and Round Robin (time slice=1). Show your solution.
Process Id Arrival time Burst time

P1 3 4

P2 5 3

P3 0 2

P4 5 1

P5 4 3

You might also like