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

Module OLASO

Uploaded by

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

Module OLASO

Uploaded by

Mark Me
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

MODULE: EXPLORING CPU SCHEDULING ALGORITHMS

Module
Submitted to the Faculty of
Department of Information Technology
Cavite State University General Trias City Campus
General Trias City, Cavite

In Partial Fulfillment
Of the requirements for the degree
Bachelor of Science in Information Technology

ALEXIS JUSTINE A. OLASO


February 2024
Table of Content:

What is CPU Scheduling?.............................................................................................................................1


Types of CPU Scheduling.............................................................................................................................2
There are mainly two types of CPU Scheduling:..........................................................................................2
First Come First Serve (FCFS).......................................................................................................................2
Shortest Job First (SJF).................................................................................................................................7
Priority Scheduling.....................................................................................................................................15
Round-robin (RR).......................................................................................................................................17
The Purpose of CPU Scheduling.................................................................................................................19
REFERENCES..............................................................................................................................................20
What is CPU Scheduling?
CPU Scheduling is a process of determining which process will own CPU for execution
while another process is on hold. The main task of CPU scheduling is to make sure that
whenever the CPU remains idle the OS at least selects one of the processes available in the
ready queue for execution. The selection process will be carried out by the CPU scheduler. It
selects one of the processes in memory that are ready for execution.
CPU scheduling is a method process or task that the CPU will run at any given moment.
It is an essential part of modern operating systems as it enables multiple processes to run at the
same time on the same processor. In short, the CPU scheduler decides the order and priority of
the processes to run and allocates the CPU time based on various parameters such as CPU
usage, throughput, turnaround, waiting time, and response time.
CPU scheduling is essential for the system’s performance and ensures that processes
are executed correctly and on time. Different CPU scheduling algorithms have other properties,
and the choice of a particular algorithm depends on various factors. Many criteria have been
suggested for comparing CPU scheduling algorithms.

Important CPU scheduling Terminologies

Burst 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

Process: It is the reference that is used for both job and user.

Completion Time: When a process finishes execution and is no longer being processed by the

CPU.

Waiting Time: the total time a process takes while waiting in a ready queue before reaching

the CPU.

1
Types of CPU Scheduling

There are mainly two types of CPU Scheduling:

CPU Scheduling

PRE-EMPTIVE NON-PRE-EMPTIVE

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.

First Come First Serve (FCFS)


-It is an operating system scheduling algorithm that automatically executes
queued requests and processes in order of their arrival. It is the easiest and simplest

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

Process Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 1) The process begins with P4 which has arrival time 0

Step 2) At time=1, P3 arrives. P4 is still executing. Hence, P3 is kept in a queue.

Process Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 3) At time= 2, P1 arrives which is kept in the queue.

Process Burst time Arrival time


P1 6 2
P2 2 5

3
Process Burst time Arrival time
P3 8 1
P4 3 0
P5 4 4

Step 4) At time=3, P4 process completes its execution.

Step 5) At time=4, P3, which is first in the queue, starts execution.

Process Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

Step 6) At time =5, P2 arrives, and it is kept in a queue.

Process Burst time Arrival time

P1 6 2

P2 2 5

4
Process Burst time Arrival time

P3 8 1

P4 3 0

P5 4 4

Step 7) At time 11, P3 completes its execution.

Step 8) At time=11, P1 starts execution. It has a burst time of 6. It completes execution at time
interval 17

Step 9) At time=17, P5 starts execution. It has a burst time of 4. It completes execution at


time=21

5
Step 10) At time=21, P2 starts execution. It has a burst time of 2. It completes execution at time
interval 23

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

Waiting time = Start time - Arrival time


P4 = 0-0 = 0

P3 = 3-1 = 2

PI = 11-2 = 9

P5= 17-4 = 13

P2= 21-5= 16

Average Waiting Time

0+2+9+13+16

= 40/5= 8 ms

6
Shortest Job First (SJF)
-It 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

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.

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

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

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

Wait time
P4= 0-0=0
P1= 3-2=1
P2= 9-5=4
P5= 11-4=7
P3= 15-1=14
Average Waiting Time= 0+1+4+7+14/5 = 26/5 = 5.2ms

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.

Process Queue Burst time Arrival time


P1 6 2
P2 2 5
P3 8 1
P4 3 0
P5 4 4

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

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.

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

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

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

Wait time
P4= 0-0=0
P1= (3-2) + 6 =7
P2= 5-5 = 0
P5= 4-4+2 =2
P3= 15-1 = 14
Average Waiting Time = 0+7+0+2+14/5 = 23/5 =4.6

14
Priority Scheduling
-It is a process scheduling algorithm based on priority where the scheduler selects tasks
according to priority. Thus, processes with higher priorities are executed first followed by
processes with lower priorities.

If two jobs have the same priorities, then the process that should be executed first is chosen on
the basis of round-robin or FCFS. Which process should have priority depends on a process’
memory requirements, time requirements, the ratio of I/O burst to CPU burst 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.

Process Id Arrival time Burst time Priority

P1 0 4 2

P2 1 3 3

P3 2 1 4

P4 3 5 5

P5 4 2 5

15
P1 P4 P5 P3 P2

Gantt Chart:
0 4 9 11 12 15
Now, we know-
 Turn Around time = Completion time – Arrival time
 Waiting time = Turn Around time – Burst time

Process Id Completion time Turn Around time Waiting time

P1 4 4–0=4 4–4=0

P2 15 15 – 1 = 14 14 – 3 = 11

P3 12 12 – 2 = 10 10 – 1 = 9

P4 9 9–3=6 6–5=1

P5 11 11 – 4 = 7 7–2=5

 Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 ms


 Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 ms

16
Round-robin (RR)
-It is one of the algorithms employed by process and network schedulers in
computing.As the term is generally used, time slices are assigned to each process in equal
portions and in circular order, handling all processes without priority. Round-robin scheduling is
simple, easy to implement, and starvation-free. Round-robin scheduling can be applied to other
scheduling problems, such as data packet scheduling in computer networks. It is an operating
system concept. The Round robin scheduling is one of the CPU scheduling in which every
process gets a fixed amount of time quantum to execute the process. In this scheduling, every
process gets executed cyclically. This means that processes that have their burst time
remaining after the expiration of the time quantum are sent back to the ready state and wait for
their next turn to complete the execution until it terminates. This processing is done in FIFO
order which suggests that processes are executed on a first-come, first-serve basis.
In Round-Robin Scheduling,
 CPU is assigned to the process based on FCFS for a fixed amount of time.
 This fixed amount of time is called as time quantum or time slice.
 After the time quantum expires, the running process is preempted and sent to the ready
queue.
 Then, the processor is assigned to the next arrived process.
 It is always preemptive in nature.

Process Id Arrival time Burst time

P1 0 5

P2 1 3

P3 2 1

P4 3 2

P5 4 3

17
Gantt Chart:

P1 P2 P3 P1 P4 P5 P2 P1 P5

0 2 4 5 7 9 11 12 13 14

Now, we know-
 Turn Around time = Completion Time– Arrival time
 Waiting time = Turn Around time – Burst time

Completion
Process Id Turn Around time Waiting time
time

P1 13 13 – 0 = 13 13 – 5 = 8

P2 12 12 – 1 = 11 11 – 3 = 8

P3 5 5–2=3 3–1=2

P4 9 9–3=6 6–2=4

P5 14 14 – 4 = 10 10 – 3 = 7

Now,
 Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 ms
 Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 ms

18
The Purpose of CPU Scheduling

Scheduling the access of processes to non-sharable resources is a fundamental part of


an operating system's job. The CPU is the most important among this resource because it
typically has the highest contention. The high cost of additional CPUs, both in terms of the price
of the processor and the cost of the technology in massively parallel systems, ensures that most
systems don't have a sufficient surpless of CPUs to allow for their wasteful use.

The primary objective of CPU scheduling is to ensure that as many jobs are running at a
time as is possible. On a single-CPU system the goal is to keep one job always running.

19
REFERENCES

CPU Scheduling Criteria. (2020, June 16). GeeksforGeeks; GeeksforGeeks.

https://www.geeksforgeeks.org/cpu-scheduling-criteria/?

fbclid=IwAR2uiiHt5_2Loe9J0HhHHHEP-Fz5DxI4P1Ysj3RqR6VuuRB4QxhAxddo02M

CPU Scheduling Algorithms in Operating Systems. (2024, February 3). Guru99.

https://www.guru99.com/cpu-scheduling-algorithms.html?fbclid=IwAR0G_4baq-

1xpGYPtMDt65-CjvWE-yQ53PFyuV22OyWfXNO3uTrFtzy8fQI

‌FCFS Scheduling Algorithm: What is, Example Program. (2024, February 3). Guru99.

https://www.guru99.com/fcfs-scheduling.html?fbclid=IwAR3bYhYMs6mWBNLDOb7-

5VMq9jkMYvskgiAr4oG26pIa4FQBEjnIURDBzHM

Shortest Job First (SJF): Preemptive, Non-Preemptive Example. (2024, February 3). Guru99.

https://www.guru99.com/shortest-job-first-sjf-scheduling.html?fbclid=IwAR2RTJJ1lV-

83p9yBj2qLPcHJgEeopwQwHJXJzdWIRTf4MyN0CaafBq0vKg

(2018, October 19). Gatevidyalay.com. https://www.gatevidyalay.com/priority-scheduling-cpu-

scheduling-examples/?

fbclid=IwAR2OZkdGvotRt7DBQX7vVyZqC35_TBeuM2T4gyM91AA8CbMKaLuafiQn

exA

‌Sharma, V. (2022, August 29). What is Round Robin Scheduling in OS? - Scaler Topics. Scaler

Topics; Scaler Topics. https://www.scaler.com/topics/round-robin-scheduling-in-os/?

fbclid=IwAR0nbf2ciJoia6jq9mLCUPnUeew5fM6-6aLgX8LCg1KwaZb24cc-SwgwvJ0

‌(2018, October 19). Gatevidyalay.com. https://www.gatevidyalay.com/round-robin-round-robin-

scheduling-examples/?

fbclid=IwAR0CpVvge3NOKosYF5J0wKhlD1WioGX4PEKTifwjDFpTOlaOiQMtuz0OR

zQ

20

You might also like