OS LAB
OS LAB
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
def findTurnAroundTime(processes, n,
bt, wt,
tat): for i in range(n):
tat[i] = bt[i] + wt[i]
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]))
# process id's
processes = [ 1, 2, 3, 4,
5] n = len(processes)
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])
avg_turnaround_time = sum(turnaround_time)
/ n avg_waiting_time = sum(waiting_time) /
n
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 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):
# 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
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
INPUT/OUTPUT SCREEN: