Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
liannekiarra authored Jan 28, 2025
0 parents commit d3f14c2
Show file tree
Hide file tree
Showing 3 changed files with 504 additions and 0 deletions.
215 changes: 215 additions & 0 deletions RoundRobin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# Round Robin - Different Arrival Times

import array
import tabulate

# Building a class so processes can be expressed as objects and not as a list of dictionaries.
# Having all attrivuts initiated at the beginning and to store useful boolean attributes for the Process objects.

class Process:
#object contructor
def __init__(self):
self.complete = False
self.insideQueue = False
self.identifier = 0
self.arrivalTime = 0
self.burstTime = 0
self.remainingBurstTime = 0
self.completionTime = 0
self.turnaroundTime = 0
self.waitingTime = 0


#declaring processes

def declare_processes(id: list, arrival_time: list, burst_time: list):
initial_processes = []

identification = id
arrival = arrival_time
burstTime = burst_time

c = 0
for x in identification:
id = identification[c]
arrival_time = arrival[c]
burst_time = burstTime[c]

initial_processes.append({
'Process ID': id,
'Arrival Time': arrival_time,
'Burst Time': burst_time

})
c = c+1

return initial_processes

#building process objects from the declared processes

def makeProcesses(initial_processes: list):
#process object list
liveProcesses = [] #store process objects inside here


for i in initial_processes:
#extract information back
id = i['Process ID']
arrival_time = i['Arrival Time']
burst_time = i["Burst Time"]

# build object of Process class for each process
process = Process()
process.identifier = id
process.arrivalTime = arrival_time
process.burstTime = burst_time

liveProcesses.append(process)
return liveProcesses


def find_new_arrivals(liveProcesses: list, n: int, current_time: int, readystate:int):

currentTime = current_time
#finding the number of processes declared by user - using the id list


for i in range(n):
process = liveProcesses[i]
# if condition to find new arrivals by checking their arrival time against current time
# and checking if they are inside the queue and that they havent been terminated (not Completed yet)
if process.arrivalTime <= currentTime and not process.insideQueue and not process.complete:
liveProcesses[i].insideQueue = True
readystate.append(i) #adding Process object intp the queue

#this function updates the ready queue
def turnOn(liveProcesses: list, n: int ,quantum: int, readystate: list,current_time:int, terminatedProcesses: int ):

#findind first element in the ready queue
x = readystate[0]
#deleteing first element
readystate.pop(0)
#finding remaining burst time
rbt = liveProcesses[x].remainingBurstTime
#finding complete status
isComplete = liveProcesses[x].complete
#finding arrival time
arrivalTime = liveProcesses[x].arrivalTime



#Comparing Remaining Brust Time against Quantum Value
if liveProcesses[x].remainingBurstTime <= quantum:
liveProcesses[x].complete = True
current_time += liveProcesses[x].remainingBurstTime
liveProcesses[x].completionTime = current_time
liveProcesses[x].waitingTime = liveProcesses[x].completionTime - liveProcesses[x].arrivalTime - liveProcesses[x].burstTime
liveProcesses[x].turnaroundTime = liveProcesses[x].waitingTime + liveProcesses[x].burstTime
#if waiting time becomes negative
if liveProcesses[x].waitingTime < 0:
liveProcesses[x].waitingTime = 0

liveProcesses[x].burstTimeRemaining = 0

#if not all processes are in the ready state queue, find new arrivals
if terminatedProcesses != n:
find_new_arrivals(liveProcesses, n, current_time, readystate)
else:
#Process hasnt completed

liveProcesses[x].burstTimeRemaining -= quantum
current_time += quantum

# if not all processes are in the ready state queue, find new arrivals
# t
if terminatedProcesses != n:
find_new_arrivals(liveProcesses, n, current_time, readystate)
# inserting the unfinished process back into the ready state queue
readystate.append(x)


return

def display (liveProcesses:list, n:int):


averagewaitingtime = 0
averageturnaroundtime = 0
# sort the processes array by processes.PID
liveProcesses.sort(key=lambda x: x.identifier)

for i in range(n):
print("Process ", liveProcesses[i].identifier, ": Waiting Time: ", liveProcesses[i].waitingTime,
" Turnaround Time: ", liveProcesses[i].turnaroundTime, sep="")
averagewaitingtime += liveProcesses[i].waitingTime
averageturnaroundtime += liveProcesses[i].turnaroundTime
print("Average Waiting Time: ", averagewaitingtime / n)
print("Average Turnaround Time: ", averageturnaroundtime / n)

return


def RoundRobin(liveProcesses: list, n:int, quantum: int):

#making a queue for all the ready processes
readystate = []
readystate.append(0)#pushing first ready process

liveProcesses[0].insideQueue = True #indicate the process is inside the queue

currentTime = 0 #current time set

terminatedProcesses = 0

while len(readystate) != 0:
turnOn(liveProcesses, n, quantum, readystate, currentTime, terminatedProcesses)


if __name__ == "__main__":

#enter preferred quantum
quantum = 3


# example values for round robin scheduling
id = [1,2,3,4,5]
arrival_time = [5,2,7,3,1]
burst_time = [34,12,40,8,13]

#find n
n = len(id)

#Step One: Declare Processes
processDict = declare_processes(id,arrival_time,burst_time)

#Step Two: Build Process objects and store them in a list
liveProcesses = makeProcesses(processDict) #list of objects

#Step Three: Sort out Process Objects based on arrival time
liveProcesses.sort(key=lambda x: x.arrivalTime)

#Step Four - run round robin function

RoundRobin(liveProcesses, n, quantum)

#Step Five - display the results from round robin
display(liveProcesses, n)



















137 changes: 137 additions & 0 deletions fcfs_allzero_arrival.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#COMPLETED VERSION - all zero arrival time

import tabulate


def fcfs (processes):
#this function returns a new dictionary containing new information of the processses

#declare output new dictionary

scheduled_processes = []

#counter is to keep track of the current time that has passed for every loop
counter = 0

#looping around the dictionary
for i in processes:
#find the first process
if i['order'] == 1:

#find burst_time
burst_time = i['burst_time']
id = i['id']
order = i['order']

#default waiting time is equal to zero
waiting_time = 0
turnaround_time = burst_time
counter = turnaround_time

#add attributes to new dictionary
scheduled_processes.append({

'id':id,
'order': order,
'burst time': burst_time,
'waiting time': waiting_time,
'turnaround time': turnaround_time,
})


else:

#declaring initial attributes
order = i['order']
process_id = i['id']
arrival_time = i['arrival_time']


#find burst_time
burst_time = i['burst_time']
waiting_time = counter
turnaround_time = waiting_time + burst_time
counter = turnaround_time

# add attributes to new dictionary
scheduled_processes.append({

'id':process_id,
'order': order,
'burst time': burst_time,
'waiting time': waiting_time,
'turnaround time': turnaround_time
})

return scheduled_processes

#find averaage waiting time

def findAverageWaitingTime(scheduled_processes):
n = len(scheduled_processes)
total_waiting_time = 0

for i in scheduled_processes:
waiting_time = i['waiting time']
total_waiting_time =+ waiting_time

average = (total_waiting_time)/n

return average

# find average turnaround time
def findAverageTurnaroundTime(scheduled_processes):
n = len(scheduled_processes) #finding the number of processes
total_turnaround_time = 0 #declare and intiallise total turnaround time

#loop to sum up all turnaround times for processes

for i in scheduled_processes:
turnaround_time = i['turnaround time']
total_turnaround_time =+ turnaround_time
#calculating average turnaround time
average = (total_turnaround_time)/n


return average

def main():
processes = [

{"order": 1,"id": "P1", "arrival_time": 0, "burst_time": 10},
{"order": 2,"id": "P2", "arrival_time": 0, "burst_time": 5},
{"order": 3,"id": "P3", "arrival_time": 0, "burst_time": 8},
{"order": 4,"id": "P4", "arrival_time": 0, "burst_time": 15},
{"order": 5,"id": "P5", "arrival_time": 0, "burst_time": 25},
{"order": 6,"id": "P6", "arrival_time": 0, "burst_time": 13},
{"order": 7,"id": "P7", "arrival_time": 0, "burst_time": 56},
{"order": 8,"id": "P8", "arrival_time": 0, "burst_time": 2},
{"order": 9,"id": "P9", "arrival_time": 0, "burst_time": 76},
{"order": 10,"id": "P10", "arrival_time": 0, "burst_time": 45},
{"order": 11,"id": "P11", "arrival_time": 0, "burst_time": 12}

]

output = fcfs(processes)
header = output[0].keys()
rows = [x.values() for x in output]
print(tabulate.tabulate(rows, header))

averageWaitingTime = str(round(findAverageWaitingTime(output),2))
averageTurnaroundTime = str(round(findAverageTurnaroundTime(output),2))

print("Average Waiting Time:",averageWaitingTime)
print("Average Turnaround Time:",averageTurnaroundTime)

main()











Loading

0 comments on commit d3f14c2

Please sign in to comment.