Skip to content
Snippets Groups Projects
Commit e9653178 authored by Peter Faber's avatar Peter Faber
Browse files

add versions w/ context switch overhead

parent d0aab2f7
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python3
# CC-by-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
# MODIFIED FROM https://www.geeksforgeeks.org/program-for-fcfs-cpu-scheduling-set-2-processes-with-different-arrival-times/
# Python3 program for implementation of FCFS
# scheduling with different arrival time
# Function to find the waiting time
# for all processes
def findWaitingTime(processes, n, bt, wt, at, overhead):
service_time = [0] * n
service_time[0] = at[0]
wt[0] = 0
# calculating waiting time
for i in range(1, n):
# Add burst time of previous processes
# service_time = wait_time + arrival_time /
# i.e., according to FCFS (wait for i-1, then start):
# service_time = max(arrival_time(i),
# service_time(i-1) + burst(i-1)+overhead)
service_time[i] = max(at[i],
service_time[i - 1] + overhead
+ bt[i - 1])
# Find waiting time for current
# process = sum - at[i]
wt[i] = service_time[i] - at[i]
# Function to calculate turn around time
def findTurnAroundTime(processes, n, bt, wt, tat, overhead):
# Calculating turnaround time by
# adding bt[i] + wt[i]
for i in range(n):
tat[i] = bt[i] + wt[i]
# Function to calculate average waiting
# and turn-around times.
def findavgTime(processes, n, bt, at, overhead):
wt = [0] * n
tat = [0] * n
# Function to find waiting time
# of all processes
findWaitingTime(processes, n, bt, wt, at, overhead)
# Function to find turn around time for
# all processes
findTurnAroundTime(processes, n, bt, wt, tat, overhead)
# Display processes along with all details
print("Processes Burst Time Arrival Time Waiting",
"Time Turn-Around Time Completion Time \n")
total_wt = 0
total_tat = 0
for i in range(n):
total_wt = total_wt + wt[i]
total_tat = total_tat + tat[i]
compl_time = tat[i] + at[i]
print(" ", i + 1, "\t\t", bt[i], "\t\t", at[i],
"\t\t", wt[i], "\t\t ", tat[i], "\t\t ", compl_time)
print("Average waiting time = %.5f "%(total_wt /n))
print("\nAverage turn around time = ", total_tat / n)
# Driver code
if __name__ =="__main__":
# Process id's
processes = [1, 2, 3]
n = 3
# Burst time of all processes
burst_time = [5, 9, 6]
# Arrival time of all processes
arrival_time = [0, 3, 6]
# overhead for context switch
overhead = 1
findavgTime(processes, n, burst_time,
arrival_time, overhead)
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)
#!/usr/bin/python3
# CC-by-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
# TAKEN FROM https://www.geeksforgeeks.org/program-for-priority-cpu-scheduling-set-1/
# Python3 program for implementation of
# Priority Scheduling
# Function to find the waiting time
# for all processes
def findWaitingTime(processes, n, wt, overhead):
wt[0] = 0
# calculating waiting time
for i in range(1, n):
wt[i] = processes[i - 1][1] + wt[i - 1] + overhead
# Function to calculate turn around time
def findTurnAroundTime(processes, n, wt, tat, overhead):
# Calculating turnaround time by
# adding bt[i] + wt[i]
for i in range(n):
tat[i] = processes[i][1] + wt[i]
# Function to calculate average waiting
# and turn-around times.
def findavgTime(processes, n, overhead):
wt = [0] * n
tat = [0] * n
# Function to find waiting time
# of all processes
findWaitingTime(processes, n, wt, overhead)
# Function to find turn around time
# for all processes
findTurnAroundTime(processes, n, wt, tat, overhead)
# Display processes along with all details
print("\nProcesses 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)
def priorityScheduling(proc, n, overhead):
# Sort processes by priority
proc = sorted(proc, key = lambda proc:proc[2],
reverse = True);
print("Order in which processes gets executed")
for i in proc:
print(i[0], end = " ")
findavgTime(proc, n, overhead)
# Driver code
if __name__ =="__main__":
# Process id's
# (id, bt, prio)
proc = [(1, 10, 2),
(2, 5, 0),
(3, 8, 1)]
n = len(proc)
# overhead for context switch
overhead = 1
priorityScheduling(proc, n, overhead)
# This code is contributed
# Shubham Singh(SHUBHAMSINGH10)
#!/usr/bin/python3
# CC-by-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
# TAKEN FROM https://www.geeksforgeeks.org/program-round-robin-scheduling-set-1/
# Python3 program for implementation of
# RR scheduling
# Function to find the waiting time
# for all processes
def findWaitingTime(processes, n, bt,
wt, quantum, overhead):
rem_bt = [0] * n
# Copy the burst time into rt[]
#for i in range(n):
# rem_bt[i] = bt[i]
rem_bt[:] = bt[:]
t = 0 # Current time
# Keep traversing processes in round
# robin manner until all of them are
# not done.
while(1):
done = True
# Traverse all processes one by
# one repeatedly
for i in range(n):
# If burst time of a process is greater
# than 0 then it needs to be processed further
if (rem_bt[i] > 0) :
done = False # There is a pending process
if (rem_bt[i] > quantum) :
# Increase the value of t i.e. shows
# how much time a process has been processed
t += quantum + overhead
# Decrease the burst_time of current
# process by quantum
rem_bt[i] -= quantum
# If burst time is smaller than or equal
# to quantum. Last cycle for this process
else:
# Increase the value of t i.e. shows
# how much time a process has been processed
t = t + rem_bt[i] + overhead
# Waiting time is current time minus
# time used by this process
wt[i] = t - bt[i]
# As the process gets fully executed
# make its remaining burst time = 0
rem_bt[i] = 0
# If all processes are done
if (done == True):
break
# Function to calculate turn around time
def findTurnAroundTime(processes, n, bt, wt, tat, overhead):
# Calculating turnaround time
for i in range(n):
tat[i] = bt[i] + wt[i]
# Function to calculate average waiting
# and turn-around times.
def findavgTime(processes, n, bt, quantum, overhead):
wt = [0] * n
tat = [0] * n
# Function to find waiting time
# of all processes
findWaitingTime(processes, n, bt,
wt, quantum, overhead)
# Function to find turn around time
# for all processes
findTurnAroundTime(processes, n, bt,
wt, tat, overhead)
# Display processes along with all details
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))
# Driver code
if __name__ =="__main__":
# Process id's
proc = [1, 2, 3]
n = 3
# overhead for context switch
overhead = 1
# Burst time of all processes
burst_time = [10, 5, 8]
# Time quantum
quantum = 2;
findavgTime(proc, n, burst_time, quantum, overhead)
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment