65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
|
'''
|
||
|
Every process is an obj with it's own wt,bt,at etc.
|
||
|
the table is an obj of table class.
|
||
|
Our table is just a list of process objects
|
||
|
wt,tat,ct are calculated by waitCalculator()
|
||
|
createTable() displays table neatly
|
||
|
'''
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class ProcessClass:
|
||
|
def __init__(self): #constructor in Py
|
||
|
self.name =input("Enter Process Name: ")
|
||
|
self.at = int(input("Enter Arrival Time: "))
|
||
|
self.bt = int(input("Enter Burst Time: "))
|
||
|
self.wt = 0
|
||
|
self.tat = 0
|
||
|
self.ct = 0
|
||
|
def display(self):
|
||
|
print(f"{self.name}:\t{self.at}\t{self.bt}\t{self.wt}\t{self.ct}\t\t{self.tat}\n")
|
||
|
|
||
|
class Table_class:
|
||
|
|
||
|
def __init__(self):
|
||
|
self.table = []
|
||
|
self.table1 = []
|
||
|
print("Enter Processes:\n")
|
||
|
while True:
|
||
|
ch = int(input("\n\nAdd a new process?\t"))
|
||
|
if ch:
|
||
|
p = ProcessClass()
|
||
|
self.table.append(p)
|
||
|
else: break
|
||
|
def fcfs(self):
|
||
|
time = 0
|
||
|
self.table1 = sorted(self.table, key= lambda p: p.at) #sorts array based on arrival time
|
||
|
for cp in self.table1:
|
||
|
cp.ct = cp.bt + time
|
||
|
cp.wt = time - cp.at
|
||
|
cp.tat = cp.wt + cp.bt
|
||
|
time+= cp.bt
|
||
|
def createTable(self):
|
||
|
print(f"\n\nThe Table is:")
|
||
|
print(f"Process\tArrival\tBurst\tWaiting\tCompletedAt\tT.A.T\n")
|
||
|
for p in self.table1:
|
||
|
p.display()
|
||
|
|
||
|
def sjf(self):
|
||
|
time = 0
|
||
|
self.table1 = sorted(self.table,key= lambda p: p.bt) #sorts array based on arrival time
|
||
|
for cp in self.table1:
|
||
|
cp.ct = cp.bt + time
|
||
|
if time:
|
||
|
cp.wt = time - cp.at
|
||
|
else:
|
||
|
cp.wt = 0
|
||
|
cp.tat = cp.wt + cp.bt
|
||
|
time+= cp.bt
|
||
|
|
||
|
# Code by Afan Shaikh.
|
||
|
tab = Table_class()
|
||
|
print("Using sjf!!\n\n")
|
||
|
tab.sjf()
|
||
|
tab.createTable()
|