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

OS LAB

The document outlines a course on Operating Systems, specifically focusing on CPU scheduling algorithms implemented in Python. It includes experiments for First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), and Round Robin (RR), detailing their aims, descriptions, algorithms, and source code. Each experiment provides a structured approach to simulating the respective scheduling algorithm with example processes.

Uploaded by

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

OS LAB

The document outlines a course on Operating Systems, specifically focusing on CPU scheduling algorithms implemented in Python. It includes experiments for First Come First Serve (FCFS), Shortest Job First (SJF), Shortest Remaining Time First (SRTF), and Round Robin (RR), detailing their aims, descriptions, algorithms, and source code. Each experiment provides a structured approach to simulating the respective scheduling algorithm with example processes.

Uploaded by

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

Course Title: Operating System

Course Code: CSE3003

Faculty Name: Dr. Virendra Singh Kushwah

Submitted by:
VAIBHAV KUMAR
21BCE10729
Experiment Experiment Name Page No.
No.
Write a Python program to simulate the CPU scheduling
1 3
algorithm First Come First Serve (FCFS).
Write a Python program to simulate the CPU scheduling
2 6
algorithm Shortest Job First (SJF).
Write a Python program to simulate the CPU scheduling
3 9
algorithm Shortest Job First Remaining (SRTF).
Write a Python program to simulate the CPU scheduling
4 12
algorithm Round Robin (RR).
EXPERIMENT: 1

AIM: Write a Python program to simulate the CPU scheduling algorithm First Come
First Serve (FCFS).

Process BT Priority AT
P1 10 3 0
P2 1 1 1
P3 2 5 2
P4 1 4 3
P5 5 2 4

DESCRIPTION:
1. FCFS supports non-preemptive and preemptive CPU scheduling algorithms.
2. Tasks are always executed on a First-come, First-serve concept.
3. FCFS is easy to implement and use.
4. This algorithm is not very efficient in performance, and the wait time is
quite high.

ALGORITHM:
 The waiting time for the first process is 0 as it is executed first.
 The waiting time for the upcoming process can be calculated by:
wt[i] = (at[i – 1] + bt[i – 1] + wt[i – 1]) – at[i]
where,
wt[i] = waiting time of current process
at[i-1] = arrival time of previous process
bt[i-1] = burst time of previous process
wt[i-1] = waiting time of previous process
at[i] = arrival time of current process

The Average waiting time can be calculated by:


Average Waiting Time = (sum of all waiting time)/(Number of processes)
SOURCE CODE:
# Python3 program for implementation of FCFS
scheduling def findWaitingTime(processes, n,
bt, wt):
wt[0] = 0
for i in range(1, n ):
wt[i] = bt[i - 1] + wt[i - 1]

def findTurnAroundTime(processes, n,
bt, wt,
tat): for i in range(n):
tat[i] = bt[i] + wt[i]

def findavgTime( processes, n,


bt): wt = [0] * n
tat = [0] *
n total_wt =
0
total_tat = 0

findWaitingTime(processes, n, bt, wt)

findTurnAroundTime(processes, n, bt, wt, tat)

print( "Processes Burst time " + " Waiting time " + " Turn around time")

for i in range(n):
total_wt = total_wt + wt[i]
total_tat = total_tat + tat[i]
print(" " + str(i + 1) + "\t\t" + str(bt[i]) + "\t " + str(wt[i]) + "\
t\t "
+ str(tat[i]))

print( "Average waiting time = "+ str(total_wt / n))


print("Average turn around time = "+ str(total_tat / n))

if name ==" main ":

# process id's
processes = [ 1, 2, 3, 4,
5] n = len(processes)

# Burst time of all


processes burst_time =
[10, 1, 2, 1, 5]

findavgTime(processes, n, burst_time)
INPUT/OUTPUT SCREEN:
EXPERIMENT: 2

AIM: Write a Python program to simulate the CPU scheduling algorithm Shortest Job
First (SJF).

Process BT Priority AT
P1 10 3 0
P2 1 1 1
P3 2 5 2
P4 1 4 3
P5 5 2 4

DESCRIPTION:
1. Shortest Job first has the advantage of having a minimum average waiting
time among all scheduling algorithms.
2. It is a Greedy Algorithm.
3. It may cause starvation if shorter processes keep coming. This problem can
be solved using the concept of ageing.
4. It is practically infeasible as Operating System may not know burst times and
therefore may not sort them. While it is not possible to predict execution time,
several methods can be used to estimate the execution time for a job, such as
a weighted average of previous execution times.
5. SJF can be used in specialized environments where accurate estimates
of running time are available.

ALGORITHM:
To implement the Shortest-Job-First with Arrival Time Scheduling algorithm, we will
implement the following steps:
 Sort all the processes according to their time of arrival.
 Then sort all the processes according to their burst time and choose the
one with both minimum arrival time and burst time.
 After the completion of the process, make a pool of processes that execute
till the completion of the previous process and then select the process from
the pool having minimum Burst Time.

SOURCE CODE:
def sjf(processes, n):
processes.sort(key=lambda x:
x[1]) completion_time = [0] * n
remaining_burst_time = [process[2] for process in
processes] current_time = 0
sequence = []

while True:
shortest_burst_time =
float('inf') shortest_index =
None
for i in range(n):
if remaining_burst_time[i] > 0 and processes[i][1] <=
current_time: if remaining_burst_time[i] <
shortest_burst_time:
shortest_burst_time =
remaining_burst_time[i] shortest_index =
i

if shortest_index is
None: break
remaining_burst_time[shortest_index] -= 1
current_time += 1

if remaining_burst_time[shortest_index] == 0:
completion_time[shortest_index] =
current_time
sequence.append(processes[shortest_index]
[0])

turnaround_time = [completion_time[i] - processes[i][1] for i in


range(n)] waiting_time = [turnaround_time[i] - processes[i][2] for
i in range(n)]

avg_turnaround_time = sum(turnaround_time)
/ n avg_waiting_time = sum(waiting_time) /
n

print("Process\t Arrival Time\t Burst Time\t Completion Time\t


if name == " main ":
# Example processes: (Process ID, Arrival Time, Burst Time)
processes = [(1, 0, 10), (2, 1, 1), (3, 2, 2), (4, 3, 1), (5,
4, 5)]
n = len(processes)

INPUT/OUTPUT SCREEN:
EXPERIMENT: 3

AIM: Write a Python program to simulate the CPU scheduling algorithm Shortest
Job First Remaining (SRTF).

Process BT Priority AT
P1 10 3 0
P2 1 1 1
P3 2 5 2
P4 1 4 3
P5 5 2 4

DESCRIPTION:
SRTF is a preemptive algorithm, which means that the currently running process can
be interrupted if a new process arrives with a shorter burst time. This helps in
ensuring that the processes with the shortest burst times are executed first.

ALGORITHM:
 Traverse until all process gets completely executed.
Find process with minimum remaining time at every single time lap.
Reduce its time by 1.
Check if its remaining time becomes 0
Increment the counter of process completion.
Completion time of current process = current_time + 1;
Calculate waiting time for each completed process.
wt[i]= Completion time – arrival_time-burst_time
 Increment time lap by one.
 Find turnaround time (waiting_time + burst_time).
SOURCE CODE:
# Python3 program to implementation Shortest Remaining Time First

def findWaitingTime(processes, n,
wt): rt = [0] * n
for i in range(n):
rt[i] = processes[i]
[1] complete = 0
t = 0
minm = 999999999
short = 0
check =
False

while (complete !=
n): for j in
range(n):
if ((processes[j][2] <= t) and (rt[j] < minm) and rt[j]
> 0): minm = rt[j]
short = j
check = True
if (check ==
False): t += 1
continue
rt[short] -= 1
minm =
rt[short] if
(minm == 0):
minm = 999999999
if (rt[short] ==
0): complete
+= 1 check =
False fint = t
+ 1
wt[short] = (fint - proc[short][1] -
proc[short][2]) if (wt[short] < 0): wt[short] =
0
t += 1

def findTurnAroundTime(processes, n, wt,


tat): for i in range(n):
tat[i] = processes[i][1] + wt[i]

def findavgTime(processes,
n): wt = [0] * n
tat = [0] * n
findWaitingTime(processes, n,
wt)
findTurnAroundTime(processes, n, wt,
tat) print("Processes Burst Time
Waiting",
"Time Turn-Around Time")
total_wt = 0
total_tat = 0
for i in range(n):

total_wt = total_wt + wt[i]


total_tat = total_tat + tat[i]
print(" ", processes[i][0], "\t\t", processes[i][1], "\t\t", wt[i],
"\t\t",
tat[i])

print("\nAverage waiting time = %.5f "%(total_wt


/n) ) print("Average turn around time = ",
total_tat / n)

# Driver code
if name ==" main ":
proc = [[1, 10, 0], [2, 1, 1], [3, 2, 5], [4, 1, 4], [5, 2, 4]]
n = 5
findavgTime(proc,

INPUT/OUTPUT SCREEN:
EXPERIMENT: 4

AIM: Write a Python program to simulate the CPU scheduling algorithm Round
Robin (RR).

Process BT Priority AT
P1 10 3 0
P2 1 1 1
P3 2 5 2
P4 1 4 3
P5 5 2 4

DESCRIPTION:
1. It is simple, easy to implement, and starvation-free as all processes get a fair
share of CPU.
2. One of the most commonly used techniques in CPU scheduling is a core.
3. It is preemptive as processes are assigned CPU only for a fixed slice of time
at most.
4. The disadvantage of it is more overhead of context switching.

ALGORITHM:
 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)
 Create another array wt[] to store waiting times of processes. Initialize
this array as 0.
 Initialize time : t = 0
 Keep traversing all the processes while they are not done. Do
following for i’th process if it is not done yet.
 If rem_bt[i] > quantum
 t = t + quantum
 rem_bt[i] -= quantum;
 Else // Last cycle for this process
 t = t + rem_bt[i];
 wt[i] = t – bt[i]
 rem_bt[i] = 0;

SOURCE CODE:
# Python3 program for implementation of RR scheduling

def findWaitingTime(processes, n, bt, wt,


quantum): rem_bt = [0] * n
for i in range(n):
rem_bt[i] =
bt[i]
t = 0
while(1)
:
done = True
for i in range(n):

if (rem_bt[i] > 0)
: done = False

if (rem_bt[i] >
quantum) : t +=
quantum rem_bt[i] -=
quantum

else:
t = t +
rem_bt[i] wt[i]
= t - bt[i]
rem_bt[i] = 0
if (done ==
True): break

def findTurnAroundTime(processes, n, bt, wt,


tat): for i in range(n):
tat[i] = bt[i] + wt[i]

def findavgTime(processes, n, bt,


quantum): wt = [0] * n
tat = [0] * n
findWaitingTime(processes, n, bt, wt,
quantum) findTurnAroundTime(processes, n,
bt, wt, tat)
print("Processes Burst Time Waiting", "Time Turn-Around
Time") total_wt = 0
total_tat = 0
for i in range(n):
total_wt = total_wt + wt[i]
total_tat = total_tat + tat[i]
print(" ", i + 1, "\t\t", bt[i], "\t\t", wt[i], "\t\t", tat[i])

print("\nAverage waiting time = %.5f "%(total_wt /n) )


print("Average turn around time = %.5f "% (total_tat /
n))

if name ==" main ":


proc = [1, 2, 3, 4,
5]
n = 5
burst_time = [10, 1, 2, 1, 5]
quantum = 2;
findavgTime(proc, n, burst_time, quantum)

INPUT/OUTPUT SCREEN:

You might also like