Skip to content

Commit

Permalink
Merge pull request #1571 from braceritchie/master
Browse files Browse the repository at this point in the history
Round Robin CPU Scheduling algorithm written in python3
  • Loading branch information
ZoranPandovski authored Oct 19, 2018
2 parents 63e0a21 + 2b66c38 commit 0b94171
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
63 changes: 63 additions & 0 deletions operating_system/round_robin/python3/round_robin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#Python program for implementation of RR Scheduling

print("Enter Total Process Number: ")
total_p_no = int(input())
total_time = 0
total_time_counted = 0
#proc is process list
proc = []
wait_time = 0
turnaround_time = 0

for _ in range(total_p_no):
#Getting the input for process
print("Enter process arrival time and burst time")
input_info = list(map(int, input().split(" ")))
arrival, burst, remaining_time = input_info[0], input_info[1], input_info[1]
#processes are appended to the proc list in following format
proc.append([arrival, burst, remaining_time,0])
#total_time gets incremented with burst time of each process
total_time += burst



print("Enter time quantum")
time_quantum = int(input())
print(total_time)

#Keep traversing in round robin manner until the total_time == 0
while(total_time != 0):
#traverse all the processes
for i in range(len(proc)):
#proc[i][2] here refers to remaining_time for each process i.e "i"

if(proc[i][2] <= time_quantum and proc[i][2] >= 0):
total_time_counted += proc[i][2]
total_time -= proc[i][2]
#the process has completely ended here thus setting it's remaining time to 0.
proc[i][2] = 0



elif(proc[i][2] >0):
#if process has not finished, decrementing it's remaining time by time_quantum
proc[i][2] -= time_quantum
total_time -= time_quantum
total_time_counted += time_quantum

if(proc[i][2] == 0 and proc[i][3]!=1):
'''
if remaining time of process is 0
and
individual waiting time of process has not been calculated i.e flag
'''
wait_time += total_time_counted - proc[i][0] - proc[i][1]
turnaround_time += total_time_counted - proc[i][0]
#flag is set to 1 once wait time is calculated
proc[i][3] = 1



print("\nAvg Waiting Time is ",(wait_time*1.0)/total_p_no)
print("Avg Turnaround Time is ",(turnaround_time*1.0)/total_p_no,"\n")

63 changes: 63 additions & 0 deletions process_scheduling/round robin/python3/round_robin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#Python program for implementation of RR Scheduling

print("Enter Total Process Number: ")
total_p_no = int(input())
total_time = 0
total_time_counted = 0
#proc is process list
proc = []
wait_time = 0
turnaround_time = 0

for _ in range(total_p_no):
#Getting the input for process
print("Enter process arrival time and burst time")
input_info = list(map(int, input().split(" ")))
arrival, burst, remaining_time = input_info[0], input_info[1], input_info[1]
#processes are appended to the proc list in following format
proc.append([arrival, burst, remaining_time,0])
#total_time gets incremented with burst time of each process
total_time += burst



print("Enter time quantum")
time_quantum = int(input())
print(total_time)

#Keep traversing in round robin manner until the total_time == 0
while(total_time != 0):
#traverse all the processes
for i in range(len(proc)):
#proc[i][2] here refers to remaining_time for each process i.e "i"

if(proc[i][2] <= time_quantum and proc[i][2] >= 0):
total_time_counted += proc[i][2]
total_time -= proc[i][2]
#the process has completely ended here thus setting it's remaining time to 0.
proc[i][2] = 0



elif(proc[i][2] >0):
#if process has not finished, decrementing it's remaining time by time_quantum
proc[i][2] -= time_quantum
total_time -= time_quantum
total_time_counted += time_quantum

if(proc[i][2] == 0 and proc[i][3]!=1):
'''
if remaining time of process is 0
and
individual waiting time of process has not been calculated i.e flag
'''
wait_time += total_time_counted - proc[i][0] - proc[i][1]
turnaround_time += total_time_counted - proc[i][0]
#flag is set to 1 once wait time is calculated
proc[i][3] = 1



print("\nAvg Waiting Time is ",(wait_time*1.0)/total_p_no)
print("Avg Turnaround Time is ",(turnaround_time*1.0)/total_p_no,"\n")

0 comments on commit 0b94171

Please sign in to comment.