|
| 1 | +# fatemetayebi |
| 2 | + |
| 3 | +# Highest Response Ratio 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 | +import numpy as np |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import random |
| 12 | + |
| 13 | +# Get inputs |
| 14 | +n = int(input("")) |
| 15 | +BurstT = input("") |
| 16 | +ArrivalT = input("") |
| 17 | + |
| 18 | +# Transform entries to int array |
| 19 | +AT = ArrivalT.split(", ") |
| 20 | +AT = np.array(AT, dtype='i') |
| 21 | + |
| 22 | +# Transform entries to int array |
| 23 | +BT = BurstT.split(", ") |
| 24 | +BT = np.array(BT, dtype='i') |
| 25 | + |
| 26 | +# What process are in ready queue now? |
| 27 | +i = 0 |
| 28 | + |
| 29 | + |
| 30 | +def RQ(i, AT, a, BT, ExtT): |
| 31 | + rq = np.where(AT <= i, True, |
| 32 | + False) # if arrival time of a process is smaller than current time so add it to ready queue |
| 33 | + for i in range(n): |
| 34 | + if BT[i] <= ExtT[i]: # if executed time is equal to burt time remove the process because it was over |
| 35 | + rq[i] = False |
| 36 | + if BT[a] > ExtT[ |
| 37 | + a]: # the algorithm is non-preemptive so if the current process is not over add it to queue(continue it) |
| 38 | + rq[a] = True |
| 39 | + else: |
| 40 | + rq[a] = False # otherwise if it is over remove it from ready queue |
| 41 | + return rq |
| 42 | + |
| 43 | + |
| 44 | +# Count executed time |
| 45 | +zero = np.zeros(n) |
| 46 | +ExtT = np.array(zero, dtype='i') # an array for executed time for each processes |
| 47 | + |
| 48 | + |
| 49 | +def ET(arr, inArr): # function of ET add to executed time of processes |
| 50 | + arr[inArr] += 1 |
| 51 | + return arr |
| 52 | + |
| 53 | + |
| 54 | +# Count waiting time |
| 55 | +WTi = np.array(zero, dtype='i') |
| 56 | + |
| 57 | + |
| 58 | +def WT(inRq, WTi): |
| 59 | + for i in inRq: |
| 60 | + WTi[i] += 1 |
| 61 | + return WTi |
| 62 | + |
| 63 | + |
| 64 | +# Count response ratio for each processes in RQ |
| 65 | +RR = np.array(zero, dtype='f') |
| 66 | + |
| 67 | + |
| 68 | +def HRR(RR, inRq, WTi): |
| 69 | + for i in inRq: |
| 70 | + RR[i] = WTi[i] / BT[i] |
| 71 | + return RR |
| 72 | + |
| 73 | + |
| 74 | +# Creat a list of lists |
| 75 | +process = [[]] |
| 76 | + |
| 77 | + |
| 78 | +# Record executed time i = time , n = array index (process id) |
| 79 | +def Record(i, n): |
| 80 | + while n > len(process) - 1: # if the input index is larger than length of list |
| 81 | + process.append([]) # then add i as a list |
| 82 | + else: |
| 83 | + process[n].append(i) # if list has the index n so add i in the list with index n |
| 84 | + return process |
| 85 | + |
| 86 | + |
| 87 | +# Find the highest response ratio |
| 88 | +def HighestRR(RR, ExtT, BT): |
| 89 | + for i in range(n): |
| 90 | + if ExtT[i] >= BT[i]: # if a process is over check another one |
| 91 | + continue |
| 92 | + else: |
| 93 | + max = np.max(RR) # find the highest response ratio |
| 94 | + search = np.where(RR == max)[0] |
| 95 | + return int(search[0]) # return the index of process with the highest response ratio |
| 96 | + |
| 97 | + |
| 98 | +# Check if after a unit of time |
| 99 | +a = 0 |
| 100 | +j = 0 |
| 101 | +while not ((ExtT == BT).all()): |
| 102 | + Rq = RQ(i, AT, a, BT, ExtT) # ready queue |
| 103 | + inRq = np.where(Rq == True)[0] # return the index of processes which are arrived |
| 104 | + RR = HRR(RR, inRq, WTi) # response ratio array |
| 105 | + if len(inRq) == 1: # if we have just one process it doesn't need to calculate the highest response ratio |
| 106 | + a = inRq[0] # return the only process index |
| 107 | + elif ExtT[a] == BT[a]: # if the current process is over- |
| 108 | + a = HighestRR(RR, ExtT, BT) # choes another one |
| 109 | + # otherwise it doesn't need to change the process |
| 110 | + ExtT = ET(ExtT, a) # add to executed time |
| 111 | + WTi = WT(inRq, WTi) # if there is a process in ready queue add to its waiting time |
| 112 | + process = Record(i, a) |
| 113 | + |
| 114 | + # print('\n','response ratio:', RR) |
| 115 | + # print('in Rq:', inRq) |
| 116 | + # print('i = ', i) |
| 117 | + # print('Rq =', Rq) |
| 118 | + # print('WTi =', WTi) |
| 119 | + # print('highest RR:', a) |
| 120 | + # print('executed time:', ExtT) |
| 121 | + # print('process:',process, '\n') |
| 122 | + i += 1 |
| 123 | + j += BT[a] |
| 124 | + |
| 125 | +# Calculate average of turnaround time |
| 126 | +sumTT = sum(WTi) |
| 127 | +print('\nAverage of turnaround time : ', sumTT / n, '\n') |
| 128 | + |
| 129 | +# Calculate average of waiting time |
| 130 | +wating = np.subtract(WTi, BT) |
| 131 | +sumWT = sum(wating) |
| 132 | +print('\nAverage of wating time : ', sumWT / n, '\n') |
| 133 | + |
| 134 | +# Gantt chart |
| 135 | +fig, gnt = plt.subplots() |
| 136 | + |
| 137 | + |
| 138 | +def Gantt(n, process): |
| 139 | + gnt.set_ylim(0, (10 * n) + 20) |
| 140 | + # Setting X-axis limits |
| 141 | + max = [] |
| 142 | + for i in process: |
| 143 | + max.append(np.max(i)) |
| 144 | + |
| 145 | + max = np.max(max) + 1 |
| 146 | + gnt.set_xlim(0, max) |
| 147 | + |
| 148 | + # Setting labels for x-axis and y-axis |
| 149 | + gnt.set_xlabel('seconds') |
| 150 | + gnt.set_ylabel('Processor') |
| 151 | + |
| 152 | + # Setting ticks on y-axis |
| 153 | + ytick = [15] |
| 154 | + sumy = 15 |
| 155 | + for i in range(n - 1): |
| 156 | + sumy += 10 |
| 157 | + ytick.append(sumy) |
| 158 | + gnt.set_yticks(ytick) |
| 159 | + |
| 160 | + # Labelling tickes of y-axis |
| 161 | + ytickL = [] |
| 162 | + for i in range(1, n + 1): |
| 163 | + ytickL.append(i) |
| 164 | + ytickLa = map(str, ytickL) |
| 165 | + gnt.set_yticklabels(ytickLa) |
| 166 | + |
| 167 | + # Setting graph attribute |
| 168 | + gnt.grid(True) |
| 169 | + |
| 170 | + # Declaring a bar in schedule |
| 171 | + # [(fasele az chap , andze tul bar)], (fasele az paiin, andaze arze bar) |
| 172 | + color = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown', 'tab:pink', 'tab:gray', |
| 173 | + 'tab:olive', 'tab:cyan', '#1f77b4', |
| 174 | + '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf'] |
| 175 | + wi = [] |
| 176 | + |
| 177 | + # print(wi) |
| 178 | + |
| 179 | + li = () |
| 180 | + |
| 181 | + # print(i) |
| 182 | + for i in process: |
| 183 | + c = int(random.randint(0, 19)) |
| 184 | + for j in i: |
| 185 | + wi = [] |
| 186 | + wi.append((j, 1)) |
| 187 | + |
| 188 | + for a in range(1, n + 1): |
| 189 | + d = process.index(i) + 1 |
| 190 | + if d == a: |
| 191 | + li = (a * 10, 10) |
| 192 | + gnt.broken_barh(wi, li, facecolors=(color[c])) |
| 193 | + # print(li) |
| 194 | + # print(wi) |
| 195 | + |
| 196 | + plt.savefig("Highest-Response-Ratio-Next.png") |
| 197 | + |
| 198 | + |
| 199 | +Gantt(n, process) |
| 200 | + |
| 201 | +5 |
| 202 | +3, 6, 4, 5, 2 |
| 203 | +0, 2, 4, 6, 8 |
0 commit comments