Skip to content

Instantly share code, notes, and snippets.

@Anan5a
Created November 13, 2022 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Anan5a/bb18151d8439ea02ff33e444ff5a51af to your computer and use it in GitHub Desktop.
Save Anan5a/bb18151d8439ea02ff33e444ff5a51af to your computer and use it in GitHub Desktop.
class Process:
def __init__(self, pid, at, bt, priority=None):
self.pid = pid
self.at = at
self.bt = bt
self.priority = priority
self.ct = None
self.tat = None
self.wt = None
self.bt_remaining = bt
self.completed = 0
self.start_time = None
self.rt = None
def runScheduler(processes: "list[Process]"):
n = len(processes)
completed = 0
current_time = 0
current_p = -1
while completed != n:
current_p = -1
mn = 1000000000
for i in range(n):
if processes[i].at <= current_time and processes[i].completed == 0:
if processes[i].bt_remaining < mn:
mn = processes[i].bt_remaining
current_p = i
if processes[i].bt_remaining == mn:
if processes[i].at < processes[current_p].at:
mn = processes[i].bt_remaining
current_p = i
if current_p != -1:
if processes[current_p].bt_remaining == processes[current_p].bt:
processes[current_p].start_time = current_time
processes[current_p].bt_remaining = processes[current_p].bt_remaining - 1
current_time = current_time + 1
if processes[current_p].bt_remaining == 0:
processes[current_p].ct = current_time
processes[current_p].completed = 1
completed = completed + 1
else:
current_time = current_time + 1
def findTurnAroundTime(processes):
total_tat = 0
for i in range(len(processes)):
# tat = ct[i] - at[i]
tat = processes[i].ct - processes[i].at
total_tat = total_tat + tat
processes[i].tat = tat
return total_tat
def findWaitingTime(processes):
total_wait = 0
for i in range(len(processes)):
# wt[i] = tat[i] - bt[i]
wt = processes[i].tat - processes[i].bt
processes[i].wt = wt
total_wait = total_wait + wt
return total_wait
def findAvgTime(processes):
n = len(processes)
# # run scheduling algorithm: priority scheduling
runScheduler(processes)
processes.sort(key=lambda x: x.ct)
# Function to find turn around time for all processes
total_tat = findTurnAroundTime(processes)
# Function to find wait time for all processes
total_wt = findWaitingTime(processes)
# Display processes along with all details
print("PID\tAT\tBT" + "\tCT" + "\tTAT\tWT")
for i in range(n):
print(str(processes[i].pid) + "\t" + str(processes[i].at) + "\t" + str(processes[i].bt) + "\t" + str(
processes[i].ct) + "\t" + str(processes[i].tat) + "\t" + str(processes[i].wt) + "\t"
# str(processes[i].priority) + "\t"
)
print("Average waiting time = " + str(total_wt / n))
print("Average turn around time = " + str(total_tat / n))
if __name__ == "__main__":
process_list = [
Process(pid=1, at=2, bt=6),
Process(pid=2, at=5, bt=2),
Process(pid=3, at=1, bt=8),
Process(pid=4, at=0, bt=3),
Process(pid=5, at=4, bt=4)
]
findAvgTime(process_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment