0% found this document useful (0 votes)
94 views28 pages

Job Scheduling

H potential engineering final projects

Uploaded by

Nakibinge Steven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views28 pages

Job Scheduling

H potential engineering final projects

Uploaded by

Nakibinge Steven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

APPLICATION OF OPERATING SYSTEM SCHEDULING ALGORITHIMS TO AUTOMOTIVE JOB SCHEDLING

NAKIBNGE STEVEN ADRIAN

17/U/4400/1PE/PE

0761223933

What is a job scheduler?

A Process Scheduler schedules different processes to be assigned to the CPU based


on particular scheduling algorithms. There are six popular process scheduling
algorithms which we are going to discuss in this chapter −

 First-Come, First-Served (FCFS) Scheduling


 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Shortest Remaining Time
 Round Robin(RR) Scheduling
 Multiple-Level Queues Scheduling

Types of algorithms
There are two types of algorithms namely non-preemptive or preemptive.
 Non-Preemptive algorithms
Non-preemptive algorithms are designed so that once a process enters the running
state, it cannot be preempted until it completes its allotted time.
 Preemptive algorithms
The preemptive scheduling is based on priority where a scheduler may preempt a low
priority running process anytime when a high priority process enters into a ready state.
THE ALGORITHMS
First Come First Serve (FCFS)

 Jobs are executed on first come, first serve basis.


 It is a non-preemptive, pre-emptive scheduling algorithm.
 Easy to understand and implement.
 Its implementation is based on FIFO(First In First Out) queue.
 Poor in performance as average wait time is high.

Wait time of each process is as follows −

Proces Wait Time : Service Time - Arrival Time


s

P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13
Average Wait Time: (0+4+6+13) / 4 = 5.75
Shortest Job Next (SJN)
 This is also known as shortest job first, or SJF
 This is a non-preemptive, pre-emptive scheduling algorithm.
 Best approach to minimize waiting time.
 Easy to implement in large lists of jobs systems where required executing time is
known in advance.
 Impossible to implement in interactive systems where required CPU time is not
known.
 The processer should know in advance how much time process will take.
Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8

Waiting time of each process is as follows −


Proces Waiting Time
s

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25

Priority Based Scheduling


 Priority scheduling is a non-preemptive algorithm and one of the most common
scheduling algorithms in batch systems.
 Each process is assigned a priority. Process with highest priority is to be
executed first and so on.
 Processes with same priority are executed on first come first served basis.
 Priority can be decided based on memory requirements, time requirements or
any other resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we
are considering 1 is the lowest priority.

Proces Arrival Execution Priority Service


s Time Time Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14
P3 3 6 3 5

Waiting time of each process is as follows −

Proces Waiting Time


s

P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6

Shortest Remaining Time


 Shortest remaining time (SRT) is the preemptive version of the SJN algorithm.
 The processor is allocated to the job closest to completion but it can be
preempted by a newer ready job with shorter time to completion.
 Impossible to implement in interactive systems where required CPU time is not
known.
 It is often used in batch environments where short jobs need to give preference.

Round Robin Scheduling

 Round Robin is the preemptive process scheduling algorithm.


 Each process is provided a fix time to execute, it is called a quantum.
 Once a process is executed for a given time period, it is preempted and other
process executes for a given time period.
 Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

Proces Wait Time : Service Time - Arrival Time


s

P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5


Multiple-Level Queues Scheduling
Multiple-level queues are not an independent scheduling algorithm. They make use of
other existing algorithms to group and schedule jobs with common characteristics.

 Multiple queues are maintained for processes with common characteristics.


 Each queue can have its own scheduling algorithms.
 Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs
in another queue. The Process Scheduler then alternately selects jobs from each
queue and assigns them to the CPU based on the algorithm assigned to the queue.

Program for Shortest Job First (SJF) scheduling | Set 2 (Preemptive)

Difficulty Level : Medium

Last Updated : 06 Jul, 2021

In previous post, we have discussed Set 1 of SJF i.e. non-preemptive. In this post we
will discuss the preemptive version of SJF known as Shortest Remaining Time First
(SRTF).

Shortest Remaining Time First (SRTF) scheduling

In the Shortest Remaining Time First (SRTF) scheduling algorithm, the process with
the smallest amount of time remaining until completion is selected to execute. Since
the currently executing process is the one with the shortest amount of time remaining
by definition, and since that time should only reduce as execution progresses,
processes will always run until they complete or a new process is added that requires a
smaller amount of time.

Shortest Remaining Time First (Preemptive SJF): Example


Process Duration Order Arrival Time

P1 9 1 0

P2 2 2 2

Preemptive-SJF-Diagram

P1 waiting time: 4-2 = 2

P2 waiting time: 0

The average waiting time(AWT): (0 + 2) / 2 = 1

Advantage:

1- Short processes are handled very quickly.

2- The system also requires very little overhead since it only makes a decision when a
process completes or a new process is added.

3- When a new process is added the algorithm only needs to compare the currently
executing process with the new process, ignoring all other processes currently waiting
to execute.

Disadvantage:

1- Like shortest job first, it has the potential for process starvation.

2- Long processes may be held off indefinitely if short processes are continually added.

Source:Wiki
Recommended: Please try your approach on {IDE} first, before moving on to the
solution.

Implementation:

1- Traverse until all process gets completely

executed.

a) Find process with minimum remaining time at

every single time lap.

b) Reduce its time by 1.

c) Check if its remaining time becomes 0

d) Increment the counter of process completion.

e) Completion time of current process =

current_time +1;

e) Calculate waiting time for each completed

process.

wt[i]= Completion time - arrival_time-burst_time

f)Increment time lap by one.

2- Find turnaround time (waiting_time+burst_time).

C++

Java

Python3
C#

Javascript

<script>

// Javascript program to implement

// Shortest Remaining Time First

// Shortest Remaining Time First (SRTF)

class Process

constructor(pid,bt,art)

this.pid = pid; // Process ID

this.bt = bt; // Burst Time

this.art = art; // Arrival Time

// Method to find the waiting time for all

// processes

function findWaitingTime( proc,n,wt)

let rt = new Array(n);

// Copy the burst time into rt[]

for (let i = 0; i < n; i++)

rt[i] = proc[i].bt;
let complete = 0, t = 0, minm = Number.MAX_VALUE;

let shortest = 0, finish_time;

let check = false;

// Process until all processes gets

// completed

while (complete != n) {

// Find process with minimum

// remaining time among the

// processes that arrives till the

// current time`

for (let j = 0; j < n; j++)

if ((proc[j].art <= t) &&

(rt[j] < minm) && rt[j] > 0) {

minm = rt[j];

shortest = j;

check = true;

if (check == false) {

t++;

continue;
}

// Reduce remaining time by one

rt[shortest]--;

// Update minimum

minm = rt[shortest];

if (minm == 0)

minm = Number.MAX_VALUE;

// If a process gets completely

// executed

if (rt[shortest] == 0) {

// Increment complete

complete++;

check = false;

// Find finish time of current

// process

finish_time = t + 1;

// Calculate waiting time

wt[shortest] = finish_time -

proc[shortest].bt -

proc[shortest].art;
if (wt[shortest] < 0)

wt[shortest] = 0;

// Increment time

t++;

// Method to calculate turn around time

function findTurnAroundTime(proc,n,wt,tat)

// calculating turnaround time by adding

// bt[i] + wt[i]

for (let i = 0; i < n; i++)

tat[i] = proc[i].bt + wt[i];

// Method to calculate average time

function findavgTime(proc,n)

let wt = new Array(n), tat = new Array(n);

let total_wt = 0, total_tat = 0;

// Function to find waiting time of all

// processes
findWaitingTime(proc, n, wt);

// Function to find turn around time for

// all processes

findTurnAroundTime(proc, n, wt, tat);

// Display processes along with all

// details

document.write("Processes " +

" Burst time " +

" Waiting time " +

" Turn around time<br>");

// Calculate total waiting time and

// total turnaround time

for (let i = 0; i < n; i++) {

total_wt = total_wt + wt[i];

total_tat = total_tat + tat[i];

document.write(" " + proc[i].pid +

"      "

+ proc[i].bt + "    " + wt[i]

+ "    " + tat[i]+"<br>");

document.write("Average waiting time = " +

total_wt / n+"<br>");
document.write("Average turn around time = " +

total_tat / n+"<br>");

// Driver Method

let proc=[new Process(1, 6, 1),

new Process(2, 8, 1),

new Process(3, 7, 2),

new Process(4, 3, 3)];

findavgTime(proc, proc.length);

// This code is contributed by rag2127

</script>

Output:

Processes Burst time Waiting time Turn around time

1 6 3 9

2 8 16 24

3 7 8 15

4 3 0 3

Average waiting time = 6.75

Average turn around time = 12.75


Skip to content

Guru99

CPU Scheduling Algorithms in Operating Systems

Lawrence Williams

Lawrence Williams

January 1, 2022

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

In this CPU scheduling tutorial, you will learn:

What is CPU scheduling?

Types of CPU Scheduling

Important CPU scheduling Terminologies

CPU Scheduling Criteria

Interval Timer

What is Dispatcher?

Types of CPU scheduling Algorithm


First Come First Serve

Shortest Remaining Time

Priority Based Scheduling

Round-Robin Scheduling

Shortest Job First

Multiple-Level Queues Scheduling

The Purpose of a Scheduling algorithm

Types of CPU Scheduling

Here are two kinds of Scheduling methods:

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.

When scheduling is Preemptive or Non-Preemptive?

To determine if scheduling is preemptive or non-preemptive, consider these four


parameters:
A process switches from the running to the waiting state.

Specific process switches from the running state to the ready state.

Specific process switches from the waiting state to the ready state.

Process finished its execution and terminated.

Only conditions 1 and 4 apply, the scheduling is called non- preemptive.

All other scheduling are preemptive.

Important CPU scheduling Terminologies

Burst Time/Execution 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

Multiprogramming: A number of programs which can be present in memory at the


same time.

Jobs: It is a type of program without any kind of user interaction.

User: It is a kind of program having user interaction.

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

CPU/IO burst cycle: Characterizes process execution, which alternates between CPU
and I/O activity. CPU times are usually shorter than the time of I/O.

CPU Scheduling Criteria

A CPU scheduling algorithm tries to maximize and minimize the following:


Maximize:

CPU utilization: CPU utilization is the main task in which the operating system needs to
make sure that CPU remains as busy as possible. It can range from 0 to 100 percent.
However, for the RTOS, it can be range from 40 percent for low-level and 90 percent
for the high-level system.

Throughput: The number of processes that finish their execution per unit time is known
Throughput. So, when the CPU is busy executing the process, at that time, work is
being done, and the work completed per unit time is called Throughput.

Minimize:

Waiting time: Waiting time is an amount that specific process needs to wait in the ready
queue.

Response time: It is an amount to time in which the request was submitted until the first
response is produced.

Turnaround Time: Turnaround time is an amount of time to execute a specific process.


It is the calculation of the total time spent waiting to get into the memory, waiting in the
queue and, executing on the CPU. The period between the time of process submission
to the completion time is the turnaround time.

Interval Timer

Timer interruption is a method that is closely related to preemption. When a certain


process gets the CPU allocation, a timer may be set to a specified interval. Both timer
interruption and preemption force a process to return the CPU before its CPU burst is
complete.

Most of the multi-programmed operating system uses some form of a timer to prevent
a process from tying up the system forever.

What is Dispatcher?
It is a module that provides control of the CPU to the process. The Dispatcher should
be fast so that it can run on every context switch. Dispatch latency is the amount of
time needed by the CPU scheduler to stop one process and start another.

Functions performed by Dispatcher:

Context Switching

Switching to user mode

Moving to the correct location in the newly loaded program.

Types of CPU scheduling Algorithm

There are mainly six types of process scheduling algorithms

First Come First Serve (FCFS)

Shortest-Job-First (SJF) Scheduling

Shortest Remaining Time

Priority Scheduling

Round Robin Scheduling

Multilevel Queue Scheduling

Scheduling Algorithms

First Come First Serve

First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU
scheduling algorithm. In this type of algorithm, the process which requests the CPU
gets the CPU allocation first. This scheduling method can be managed with a FIFO
queue.
As the process enters the ready queue, its PCB (Process Control Block) is linked with
the tail of the queue. So, when CPU becomes free, it should be assigned to the
process at the beginning of the queue.

Characteristics of FCFS method:

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

However, this method is poor in performance, and the general wait time is quite high.

Shortest Remaining Time

The full form of SRT is Shortest remaining time. It is also known as SJF preemptive
scheduling. In this method, the process will be allocated to the task, which is closest to
its completion. This method prevents a newer ready state process from holding the
completion of an older process.

Characteristics of SRT scheduling method:

This method is mostly applied in batch environments where short jobs are required to
be given preference.

This is not an ideal method to implement it in a shared system where the required CPU
time is unknown.

Associate with each process as the length of its next CPU burst. So that operating
system uses these lengths, which helps to schedule the process with the shortest
possible time.

Priority Based Scheduling

Priority scheduling is a method of scheduling processes based on priority. In this


method, the scheduler selects the tasks to work as per the priority.

Priority scheduling also helps OS to involve priority assignments. 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 can be decided based on memory
requirements, time requirements, etc.
Round-Robin Scheduling

Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm
comes from the round-robin principle, where each person gets an equal share of
something in turn. It is mostly used for scheduling algorithms in multitasking. This
algorithm method helps for starvation free execution of processes.

Characteristics of Round-Robin Scheduling

Round robin is a hybrid model which is clock-driven

Time slice should be minimum, which is assigned for a specific task to be processed.
However, it may vary for different processes.

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

Shortest Job First

SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process
with the shortest execution time should be selected for execution next. 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.

In this method, when the CPU is available, the next process or job with the shortest
completion time will be executed first.

It is Implemented with non-preemptive policy.

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

It improves job output by offering shorter jobs, which should be executed first, which
mostly have a shorter turnaround time.

Multiple-Level Queues Scheduling

This algorithm separates the ready queue into various separate queues. In this
method, processes are assigned to a queue based on a specific property of the
process, like the process priority, size of the memory, etc.
However, this is not an independent scheduling OS algorithm as it needs to use other
types of algorithms in order to schedule the jobs.

Characteristic of Multiple-Level Queues Scheduling:

Multiple queues should be maintained for processes with some characteristics.

Every queue may have its separate scheduling algorithms.

Priorities are given for each queue.

The Purpose of a Scheduling algorithm

Here are the reasons for using a scheduling algorithm:

The CPU uses scheduling to improve its efficiency.

It helps you to allocate resources among competing processes.

The maximum utilization of CPU can be obtained with multi-programming.

The processes which are to be executed are in ready queue.

Summary:

CPU scheduling is a process of determining which process will own CPU for execution
while another process is on hold.

In Preemptive Scheduling, the tasks are mostly assigned with their priorities.

In the Non-preemptive scheduling method, the CPU has been allocated to a specific
process.

The burst time is the time required for the process to complete execution. It is also
called running time.

CPU utilization is the main task in which the operating system needs to ensure that the
CPU remains as busy as possible.

The number of processes that finish their execution per unit time is known Throughput.

Waiting time is an amount that a specific process needs to wait in the ready queue.

It is the amount of time in which the request was submitted until the first response is
produced.

Turnaround time is the amount of time to execute a specific process.


Timer interruption is a method that is closely related to preemption.

A dispatcher is a module that provides control of the CPU to the process.

Six types of process scheduling algorithms are: First Come First Serve (FCFS), 2)
Shortest-Job-First (SJF) Scheduling, 3) Shortest Remaining Time, 4) Priority
Scheduling, 5) Round Robin Scheduling, 6) Multilevel Queue Scheduling.

In the First Come First Serve method, the process which requests the CPU gets the
CPU allocation first.

In the Shortest Remaining time, the process will be allocated to the task closest to its
completion.

In Priority Scheduling, the scheduler selects the tasks to work as per the priority.

Round robin scheduling works on the principle where each person gets an equal share
of something in turn.

In the Shortest job first, the shortest execution time should be selected for execution
next.

The multilevel scheduling method separates the ready queue into various separate
queues. In this method, processes are assigned to a queue based on a specific
property.

The CPU uses scheduling to improve its efficiency.

You Might Like:

RAM vs ROM: What is the Difference Between RAM and ROM?

Multithreading vs Multiprocessing: What’s the difference?

Priority Scheduling Algorithm: Preemptive, Non-Preemptive EXAMPLE

Difference between Microprocessor and Microcontroller

DDR3 vs DDR4: Must Know Differences

Post navigation

Report a Bug

Prev

Next
About

About Us

Advertise with Us

Write For Us

Contact Us

Career Suggestion

SAP Career Suggestion Tool

Software Testing as a Career

Interesting

eBook

Blog

Quiz

SAP eBook

Execute online

Execute Java Online

Execute Javascript

Execute HTML

Execute Python

© Copyright - Guru99 2022 Privacy Policy | 9 Affiliate Disclaimer | ToS


Difference between Arrival Time and Burst Time in CPU
Scheduling
CPU scheduling algorithms require CPU time and IO time required for its execution.
CPU time is time taken by CPU to carry out the process while I/O time illustrates the
time required for I/O operation by the process.

The execution of multiple processes in an optimised way is based on different kinds of


algorithms, like FCFS, Shortest Job First etc., which depend on the time frame values,
like the Arrival Time, Burst Time, Waiting Time etc.

1. Arrival Time (AT) :


Arrival time is the point of time in milli seconds at which a process arrives at the
ready queue to begin the execution. It is merely independent of the CPU or I/O time
and just depicts the time frame at which the process becomes available to complete its
specified job.The process is independent of which process is there in the Running
state. Arrival Time can be calculated as the difference of the Completion Time and the
Turn Around Time of the process.
Arrival Time (A.T.)
= Completion Time (C.T.) - Turn Around Time (T.A.T)
2. Burst Time (BT) :
Burst Time refers to the time required in milli seconds by a process for its execution.
The Burst Time takes into consideration the CPU time of a process. The I/O time is
not taken into consideration. It is called as the execution time or running time of the
process. The process makes a transition from the Running state to the Completion
State during this time frame. Burst time can be calculated as the difference of the
Completion Time of the process and the Waiting Time, that is,
Burst Time (B.T.)
= Completion Time (C.T.) - Waiting Time (W.T.)
The following table illustrates the Arrival and Burst time of three processes P1, P2
and P3. A single CPU is allocated for the execution of these processes.
First cme first serve

Implementation:

1- Input the processes along with their burst time (bt).

2- Find waiting time (wt) for all processes.

3- As first process that comes need not to wait so

waiting time for process 1 will be 0 i.e. wt[0] = 0.

4- Find waiting time for all other processes i.e. for

process i ->

wt[i] = bt[i-1] + wt[i-1] .

5- Find turnaround time = waiting_time + burst_time

for all processes.

6- Find average waiting time =

total_waiting_time / no_of_processes.

7- Similarly, find average turnaround time =

total_turn_around_time / no_of_processes
Rouns robin
1- Create an array rem_bt[] to keep track of remaining

burst time of processes. This array is initially a

copy of bt[] (burst times array)

2- Create another array wt[] to store waiting times

of processes. Initialize this array as 0.

3- Initialize time : t = 0

4- Keep traversing the all processes while all processes

are not done. Do following for i'th process if it is

not done yet.

a- If rem_bt[i] > quantum

(i) t = t + quantum

(ii) rem_bt[i] -= quantum;

c- Else // Last cycle for this process

(i) t = t + rem_bt[i];

(ii) wt[i] = t - bt[i]

(ii) rem_bt[i] = 0; // This process is over

Once w

You might also like