-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1571 from braceritchie/master
Round Robin CPU Scheduling algorithm written in python3
- Loading branch information
Showing
2 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
|