-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFCFS.py
More file actions
67 lines (61 loc) · 2.99 KB
/
FCFS.py
File metadata and controls
67 lines (61 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class FCFS:
def processData(self, no_of_processes):
process_data = []
for i in range(no_of_processes):
temporary = []
process_id = int(input("Enter Process ID: "))
arrival_time = int(input("Enter Arrival Time: "))
burst_time = int(input("Enter Burst Time: "))
print()
temporary.extend([process_id, arrival_time, burst_time])
process_data.append(temporary)
FCFS.schedulingProcess(self, process_data)
def schedulingProcess(self, process_data):
process_data.sort(key=lambda x: x[1]) #sorting based on arrival time
start_time = []
exit_time = []
s_time = 0
for i in range(len(process_data)):
if s_time < process_data[i][1]:
s_time = process_data[i][1]
start_time.append(s_time)
s_time = s_time + process_data[i][2]
e_time = s_time
exit_time.append(e_time)
process_data[i].append(e_time)
t_time = FCFS.calculateTurnaroundTime(self, process_data)
w_time = FCFS.calculateWaitingTime(self, process_data)
FCFS.printData(self, process_data, t_time, w_time)
def calculateTurnaroundTime(self, process_data):
total_turnaround_time = 0
for i in range(len(process_data)):
turnaround_time = process_data[i][3] - process_data[i][1] #turnaround_time = completion_time - arrival_time
total_turnaround_time = total_turnaround_time + turnaround_time
process_data[i].append(turnaround_time)
average_turnaround_time = total_turnaround_time / len(process_data)
return average_turnaround_time
def calculateWaitingTime(self, process_data):
total_waiting_time = 0
for i in range(len(process_data)):
waiting_time = process_data[i][4] - process_data[i][2] #waiting_time = turnaround_time - burst_time
total_waiting_time = total_waiting_time + waiting_time
process_data[i].append(waiting_time)
average_waiting_time = total_waiting_time / len(process_data)
return average_waiting_time
def printData(self, process_data, average_turnaround_time, average_waiting_time):
seq=[]
for i in process_data:
seq.append(i[0])
process_data.sort(key=lambda x: x[0]) #sort based on process ID
print("Process_ID Arrival_Time Burst_Time Completion_Time Turnaround_Time Waiting_Time")
for i in range(len(process_data)):
for j in range(len(process_data[i])):
print(process_data[i][j], end=" ")
print()
print('Average Turnaround Time:',average_turnaround_time)
print('Average Waiting Time:',average_waiting_time)
print('Gantt chart:',seq)
if __name__ == "__main__":
no_of_processes = int(input("Enter number of processes: "))
fcfs = FCFS()
fcfs.processData(no_of_processes)