-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfcfs.py
157 lines (111 loc) · 3.76 KB
/
fcfs.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env python3
import time
import shutil
from enum import Enum
from typing import List, Dict
from openpyxl import load_workbook
from tabulate import tabulate
class State(Enum):
IDLE = 0
ACTIVE = 1
EXPIRED = 2
# Make the output colorised
colors = {
"green": "\033[1;32m",
"off": "\033[0m",
"red": "\033[0;31m",
"blue": "\033[0;34m",
"magenta": "\033[0;35m",
}
prefix = colors["green"] + "[+] " + colors["off"]
prefix_r = colors["red"] + "[-] " + colors["off"]
prefix_b = colors["blue"] + "[*] " + colors["off"]
def fcfs(tasks: List[Dict]):
""" run tasks according to the FIFO scheme"""
tu, nr_finished = 0, 0
runqueue = [] # active tasks' list
tasks = sorted(tasks, key=lambda i: i["arrival_time"])
while nr_finished < len(tasks):
if (nr_running := len(tasks)):
for task in tasks:
if task["arrival_time"] == tu:
runqueue.append(task)
nr_running += 1
if len(runqueue) > 0 and runqueue[0]["burst_time"] == 0:
print(prefix_r + "TU={}\tProcess {} finished execution".format(
tu, runqueue[0]["pid"]))
runqueue.pop(0)
nr_finished += 1
state = State.EXPIRED
elif len(runqueue) > 0:
runqueue[0]["burst_time"] -= 1
time.sleep(0.05)
for task in runqueue[1:]:
task["waiting_time"] += 1
state = State.ACTIVE
else:
state = State.IDLE
display_execution(state, runqueue, tu)
tu += 1
def display_execution(state, tasks, time):
# case when CPU runs the Idle loop
if state == State.IDLE or (state == State.EXPIRED and len(tasks) == 0):
print(prefix_b + "TU={}\tCPU executing idle loop ...".format(time))
# case when there is a process in queue
elif state == State.ACTIVE or (state == State.EXPIRED and len(tasks) > 0):
waiting = []
for task in tasks[1:]:
waiting.append("pid={}, wait={}".format(task["pid"], task["waiting_time"]))
wait_que = ' '.join(waiting) if waiting else "<empty>"
curr_pid = tasks[0]["pid"]
rem_bt = tasks[0]["burst_time"]
print(prefix +
'TU={}\tPID={} executing - instructions left={}\tQueue: {}'.
format(time, curr_pid, rem_bt, wait_que))
def display_tasks(tasks):
headers = [
"ID",
"Arrival Time",
"Burst Time",
"Priority",
"Waiting Time",
"Turnaround Time",
]
tasks_mat = []
for task in tasks:
tasks_mat.append(
[
task["pid"],
f"{task['arrival_time']}",
f"{task['burst_time']}",
task["priority"],
f"{task['waiting_time']}",
f"{task['turnaround_time']}",
]
)
print(
"\n"
+ tabulate(tasks_mat, headers=headers, tablefmt="fancy_grid", floatfmt=".3f")
)
def get_task_list() -> List[Dict]:
tasks = []
wb = load_workbook('cpu-scheduling.xlsx') # assumes file is in cwd
sheet = wb.active
for row in sheet.iter_rows(min_row=2, values_only=True):
tasks.append({
"pid": row[0],
"arrival_time": row[1],
"burst_time": row[2],
"priority": row[3],
"waiting_time": 0,
})
return tasks
if __name__ == "__main__":
print("\n\n",u"\u001b[1m\u001b[4m\u001b[7m\t{}\t\u001b[0m\n" .format(
"FIRST COME FIRST SERVE ALGORITHM"))
term = shutil.get_terminal_size()
print(prefix + "Loading tasks..\n")
print('-' * term.columns)
TASKS = get_task_list()
fcfs(TASKS)
display_tasks(TASKS)