Skip to content

Commit 49013d7

Browse files
authored
Add files via upload
1 parent 7130083 commit 49013d7

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

AdvCompSys-FinalProject.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import heapq
2+
3+
#rudimentary process class
4+
class Process:
5+
def __init__(self, pid, arriveTime, burstTime):
6+
self.pid = pid
7+
self.arriveTime = arriveTime
8+
self.burstTime = burstTime
9+
self.remainingTime = burstTime
10+
self.completionTime = 0
11+
self.waitingTime = 0
12+
self.turnaroundTime = 0
13+
14+
def __lt__(self, other):
15+
return self.burstTime < other.burstTime
16+
17+
def firstComeFirstServe(processList):
18+
#sorts the list by the arrival time
19+
processList.sort(key=lambda x: x.arriveTime)
20+
currentTime = 0
21+
#iterates through the list of processes, advancing time as it does so
22+
for p in processList:
23+
#checks if the current time is less than the arrival time, setting the former equal to the later if true
24+
if (currentTime < p.arriveTime):
25+
currentTime = p.arriveTime
26+
p.completionTime = currentTime + p.burstTime
27+
currentTime = p.completionTime
28+
p.turnaroundTime = p.completionTime - p.arriveTime
29+
p.waitingTime = p.turnaroundTime - p.burstTime
30+
31+
def shortestJobNext(processes):
32+
#copies and sorts the processes by their arrival time
33+
processes_copy = sorted(processes, key=lambda x: x.arriveTime)
34+
ready_queue = []
35+
currentTime = 0
36+
37+
while processes_copy or ready_queue:
38+
#adds all the processes that have arrived by the current time to the ready queue
39+
while processes_copy and processes_copy[0].arriveTime <= currentTime:
40+
heapq.heappush(ready_queue, processes_copy.pop(0))
41+
42+
if (ready_queue):
43+
#executes the shortest job in the ready queue
44+
p = heapq.heappop(ready_queue)
45+
currentTime = max(currentTime, p.arriveTime) + p.burstTime
46+
p.completionTime = currentTime
47+
p.turnaroundTime = p.completionTime - p.arriveTime
48+
p.waitingTime = p.turnaroundTime - p.burstTime
49+
else:
50+
#if there is no process is ready, it advances time to the next arrival
51+
currentTime = processes_copy[0].arriveTime
52+
53+
# function to perform round robin algorithm
54+
def roundRobin(processList, timeQuantum):
55+
readyQueue = []
56+
currentTime = 0
57+
processList.sort(key=lambda x: x.arriveTime)
58+
processesLeft = len(processList)
59+
# iterates through every process
60+
while processesLeft > 0:
61+
#iterates through processList and adds non-expired processes to a queue
62+
for p in processList:
63+
if (p.arriveTime <= currentTime and p.remainingTime > 0):
64+
readyQueue.append(p)
65+
#checks if there are processes that still need to be run
66+
if (readyQueue):
67+
p = readyQueue.pop(0)
68+
#checks if the remaining time is less than quantum time and advances current time
69+
if (p.remainingTime > timeQuantum):
70+
currentTime += timeQuantum
71+
p.remainingTime -= timeQuantum
72+
#advances time and sets remaining time to 0, sets values of other time variables
73+
else:
74+
currentTime += p.remainingTime
75+
p.remainingTime = 0
76+
p.completionTime = currentTime
77+
p.turnaroundTime = p.completionTime - p.arriveTime
78+
p.waitingTime = p.turnaroundTime - p.burstTime
79+
processesLeft -= 1
80+
else:
81+
currentTime += 1
82+
83+
84+
#prints all the member variables of each process in a list of processes
85+
def displayResults(processList):
86+
print(f"{'PID':<10}{'Arrival':<10}{'Burst':<10}{'Completion':<15}{'Turnaround':<15}{'Waiting':<10}")
87+
for p in processList:
88+
print(f"{p.pid:<10}{p.arriveTime:<10}{p.burstTime:<10}"
89+
f"{p.completionTime:<15}{p.turnaroundTime:<15}{p.waitingTime:<10}")
90+
91+
#main code
92+
if __name__ == "__main__":
93+
processList = [Process(1, 0, 8), Process(2, 1, 4), Process(3, 2, 9), Process(4, 3, 5), Process(5, 2, 6), Process(6, 9, 7)]
94+
print("Choose CPU Scheduling Algorithm:")
95+
print("1. First-Come-First-Serve (FCFS)")
96+
print("2. Shortest Job Next (SJN)")
97+
print("3. Round Robin (RR)")
98+
choice = int(input("Enter your choice: "))
99+
100+
if (choice == 1):
101+
firstComeFirstServe(processList)
102+
print("\n--- FCFS Results ---")
103+
displayResults(processList)
104+
elif (choice == 2):
105+
shortestJobNext(processList)
106+
print("\n--- SJN Results ---")
107+
displayResults(processList)
108+
elif (choice == 3):
109+
timeQuantum = int(input("Enter the time quantum: "))
110+
roundRobin(processList, timeQuantum)
111+
print("\n--- Round Robin Results ---")
112+
displayResults(processList)
113+
114+

0 commit comments

Comments
 (0)