Skip to content

Commit 3d5e05d

Browse files
committed
first and whole commit
1 parent bb93071 commit 3d5e05d

File tree

2 files changed

+200
-0
lines changed

2 files changed

+200
-0
lines changed

Shortest-remaining-time-first.py

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# fatemetayebi
2+
3+
# Shortest-remaining-time-first (Shortest remaining time next)
4+
5+
# first line of input: number of processes
6+
# second line of input : needed time to execute of each process
7+
# third line of input : arrival time of processes
8+
9+
10+
import numpy as np
11+
import matplotlib.pyplot as plt
12+
import random
13+
14+
# Get inputs
15+
n = int(input(""))
16+
BurstT = input("")
17+
ArrivalT = input("")
18+
19+
# Transform entries to int array
20+
AT = ArrivalT.split(", ")
21+
AT = np.array(AT, dtype='i')
22+
23+
# Transform entries to int array
24+
BT = BurstT.split(", ")
25+
BT = np.array(BT, dtype='i')
26+
27+
# What process are in ready queue now?
28+
i = 0
29+
30+
31+
def RQ(i, AT):
32+
rq = np.where(AT <= i, True, False) # if arrival time it smaller than i then set that element true
33+
return rq # an array of true and false to know what processes are in ready queue now
34+
35+
36+
# Count executed time
37+
zero = np.zeros(n)
38+
ExtT = np.array(zero, dtype='i') # an array for executed time for each processes
39+
40+
41+
def ET(arr, inArr): # function of ET add to executed time of processes
42+
arr[inArr] += 1
43+
return arr
44+
45+
46+
# Count remaining time for each process
47+
ReT = BT.copy() # an array of remaining time of each processes
48+
49+
50+
def RT(ReT, n): # function to subtracting remaining time
51+
ReT[n] -= 1
52+
return ReT
53+
54+
55+
# Creat a list of lists
56+
process = [[]]
57+
58+
59+
# Record executed time i = time , n = array index (process id)
60+
def Record(i, n):
61+
while n > len(process) - 1: # if the input index is larger than length of list
62+
process.append([]) # then add i as a list
63+
else:
64+
process[n].append(i) # if list has the index n so add i in the list with index n
65+
return process
66+
67+
68+
# Find the shortest remaining time
69+
def min(ReT, inRq):
70+
sort = np.sort(ReT) # sort ReT array
71+
newRT = np.where(sort > 0, sort,
72+
10000000) # if there is finished process so change the remaining time to 1... to recognize minimum correctly
73+
newRT = np.sort(newRT)
74+
i = 0
75+
while newRT.any != 10000000:
76+
min = newRT[i] # we sorted the remaining time so the first element is the smallest
77+
search = np.where(ReT == min)[0] # get the index of the shortest remaining time in the ReT array
78+
se = int(search[0])
79+
if se in inRq: # if the index is in array of arrived processes (inRq)
80+
return se # so return it
81+
else: # otherwise check the next smallest element
82+
i += 1
83+
84+
85+
# Check if after a unit of time
86+
while (ReT.any(0)):
87+
Rq = RQ(i, AT)
88+
inRq = np.where(Rq == True)[0] # return the index of processes which are arrived
89+
a = min(ReT, inRq) # return the index of a process with the shortest remaining time
90+
ExtT = ET(ExtT, a) # add to executed time
91+
ReT = RT(ReT, a) # subtract from remain time
92+
process = Record(i, a)
93+
# print(ReT)
94+
# print(inRq)
95+
# print(i)
96+
# print(a)
97+
# print(process)
98+
i += 1
99+
print('\n', process, '\n')
100+
101+
# Count exit time
102+
ExitT = [] # exit time
103+
for i in range(n):
104+
ExitT.append(max(process[i]) + 1) # exit time = the last element of process[index]
105+
ExitT = np.array(ExitT)
106+
# print(ExitT)
107+
108+
# Count turnaround time
109+
TAT = np.subtract(ExitT, AT) # turnaround time = exit time - arrival time
110+
print(TAT)
111+
112+
# Count average of turnaround time
113+
sumTAT = 0
114+
for i in TAT:
115+
sumTAT += i
116+
print('\nAverage of turnaround time : ', sumTAT / n, '\n')
117+
118+
# Count waiting time
119+
WT = np.subtract(TAT, BT) # waiting time = turnaround time - burst time
120+
print(WT)
121+
122+
# Count average waiting time
123+
sumWT = 0
124+
for i in WT:
125+
sumWT += i
126+
print('\nAverage of waiting time : ', sumWT / n, '\n')
127+
128+
# Gantt chart
129+
fig, gnt = plt.subplots()
130+
131+
132+
def Gantt(n, process):
133+
gnt.set_ylim(0, (10 * n) + 20)
134+
# Setting X-axis limits
135+
max = []
136+
for i in process:
137+
max.append(np.max(i))
138+
139+
max = np.max(max) + 1
140+
gnt.set_xlim(0, max)
141+
142+
# Setting labels for x-axis and y-axis
143+
gnt.set_xlabel('seconds')
144+
gnt.set_ylabel('Processor')
145+
146+
# Setting ticks on y-axis
147+
ytick = [15]
148+
sumy = 15
149+
for i in range(n - 1):
150+
sumy += 10
151+
ytick.append(sumy)
152+
gnt.set_yticks(ytick)
153+
154+
# Labelling tickes of y-axis
155+
ytickL = []
156+
for i in range(1, n + 1):
157+
ytickL.append(i)
158+
ytickLa = map(str, ytickL)
159+
gnt.set_yticklabels(ytickLa)
160+
161+
# Setting graph attribute
162+
gnt.grid(True)
163+
164+
# Declaring a bar in schedule
165+
# [(fasele az chap , andze tul bar)], (fasele az paiin, andaze arze bar)
166+
color = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray',
167+
'tab:olive', 'tab:cyan', '#1f77b4',
168+
'#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']
169+
wi = []
170+
171+
# print(wi)
172+
173+
li = ()
174+
175+
# print(i)
176+
for i in process:
177+
c = int(random.randint(0, 19))
178+
for j in i:
179+
wi = []
180+
wi.append((j, 1))
181+
182+
for a in range(1, n + 1):
183+
d = process.index(i) + 1
184+
if (d == a):
185+
li = (a * 10, 10)
186+
gnt.broken_barh(wi, li, facecolors=(color[c]))
187+
# print(li)
188+
# print(wi)
189+
190+
plt.savefig("gantt1.png")
191+
192+
193+
Gantt(n, process)
194+
195+
####inputs
196+
5
197+
3, 6, 4, 5, 2
198+
0, 2, 4, 6, 8
199+
200+

gantt1.png

10.8 KB
Loading

0 commit comments

Comments
 (0)