def stats(self) : j=1 for i in self.threads: self.turn+=(i.burst+i.waiting) j+=1 j=1 for i in self.threads: self.wait+=i.waiting #printer(i.waiting) j+=1 self.wait=self.wait/len(self.threads) self.turn=self.turn/len(self.threads) printer("avgWT\tavgTAT\tCSs",flag=self.verbose) printer(str(self.wait)+"\t"+str(self.turn)+"\t"+str(self.CS),flag=self.verbose) return [self.wait,self.turn,self.CS]
def apply_cpu_time(self,quantum,que_copy): temp_rem_bt = [] i =0 j= len(self.rem_bt) - 1 for k in range(len(self.rem_bt)) : if k%2 == 0 : temp_rem_bt.append(self.rem_bt[i]) i+=1 if i >= j : continue else : temp_rem_bt.append(self.rem_bt[j]) j-=1 printer(list(map(lambda a : a.ID, temp_rem_bt)),flag=self.verbose) self.CS += len(temp_rem_bt) done = True for i in range(len(temp_rem_bt)): if(temp_rem_bt[i].rburst > 0): done = False #printer("**") if (temp_rem_bt[i].rburst > quantum): self.t += quantum temp_rem_bt[i].rburst -= quantum else: self.t = self.t + temp_rem_bt[i].rburst temp_rem_bt[i].waiting = self.t - temp_rem_bt[i].burst - temp_rem_bt[i].start temp_rem_bt[i].rburst = 0 que_copy.remove(temp_rem_bt[i])
def process_generator(n=100, behaviour='inc', arrival_times=False, verbose=True): err_list = [] dqrr_list = [] err_avg_WTs = [] err_avg_TATs = [] err_CSs = [] dqrr_avg_WTs = [] dqrr_avg_TATs = [] dqrr_CSs = [] Pnames = [] pperc = 0 processes = [] printer("Generating...") if behaviour == 'inc': g_burst = 1 elif behaviour == 'dec': g_burst = n * 4 + 1 g_arrival = 0 for i in range(n): if behaviour == 'inc': g_burst += random.randint(1, 4) elif behaviour == 'rand': g_burst = random.randint(1, 100) elif behaviour == 'dec': g_burst -= random.randint(1, 4) else: print("Invalid argument provided.") exit() if arrival_times != False: g_arrival += random.randint(1, 4) Pname = 'P' + str(i + 1) Pnames.append(Pname) processes.append(i + 1) err_list.append(Process(Pname, g_burst, g_arrival)) dqrr_list.append(MyThread(id=Pname, burst=g_burst, start=g_arrival)) err_perf = ERR(err_list, verbose=False, performance_mode=True) obj = DQRR(deepcopy(dqrr_list)) #obj.insertQue_thread.daemon = True #obj.insertQue_thread.start() obj.iodstrr() dqrr_perf = obj.stats() perc = math.ceil(((i + 1) / n) * 100) if pperc != perc: pperc = perc printer("█", end='') err_avg_WTs.append(err_perf[0]) err_avg_TATs.append(err_perf[1]) err_CSs.append(err_perf[2]) dqrr_avg_WTs.append(dqrr_perf[0]) dqrr_avg_TATs.append(dqrr_perf[1]) dqrr_CSs.append(dqrr_perf[2]) data = pd.DataFrame({ 'no_of_processes': processes, 'err_avg_wait_time': err_avg_WTs, 'err_avg_turnaround_time': err_avg_TATs, 'err_context_switches': err_CSs, 'dqrr_avg_wait_time': dqrr_avg_WTs, 'dqrr_avg_turnaround_time': dqrr_avg_TATs, 'dqrr_context_switches': dqrr_CSs, }) if arrival_times == False: arrival_state = 'z' else: arrival_state = 'nz' Fname = str(n) + '_' + behaviour + '_' + arrival_state + '.csv' printer("\n%s generated." % Fname, verbose) data.to_csv('CSVs/' + Fname, index=False)
def iodstrr(self): #rem_bt=[] #for i in range(len(threads)): # rem_bt[i]=threads[i].burst #t=0 #readyq=[] self.threads.sort(key=lambda t : t.start) #mark=0 que_copy = self.threads[:] #task_thread.append(thread) cs = 0 #for m in range(10): while True: done=False #printer("iodstrr") #printer(self.rem_bt) #thread.join() '''for i in range(self.mark,len(self.threads)): if(self.threads[i].start <= self.t): printer("process: ", self.threads[i].rburst ) self.rem_bt.append(self.threads[i]) self.mark=i''' #lock.acquire() self.pushQue(que_copy) printer("ready que: ",end='',flag=self.verbose) printer(list(map(lambda x: x.ID, self.rem_bt)),flag=self.verbose) if(len(self.rem_bt)==0): self.t+=1 continue #lock.release() #printer("iodstrr") self.rem_bt=list(filter(lambda a: a.rburst != 0, self.rem_bt)) #rem_bt.sort(key = lambda x : x.rburst, rem_bt) temp=[i.rburst for i in self.rem_bt] if(len(self.rem_bt) != 0): printer("length of readyq: ", end='',flag=self.verbose) printer(len(temp),flag=self.verbose) printer("burst times in ready q : ",end='',flag=self.verbose) printer(temp,flag=self.verbose) quantum=floor(median(temp)) printer("quantum: %d " % quantum,flag=self.verbose) self.rem_bt.sort(key= lambda rem_burst : rem_burst.rburst) #done = self.apply_cpu_time(quantum) self.apply_cpu_time(quantum,que_copy) #cs +=1 if que_copy == [] : done = True if(done == True): break
j+=1 self.wait=self.wait/len(self.threads) self.turn=self.turn/len(self.threads) printer("avgWT\tavgTAT\tCSs",flag=self.verbose) printer(str(self.wait)+"\t"+str(self.turn)+"\t"+str(self.CS),flag=self.verbose) return [self.wait,self.turn,self.CS] '''new=Thread(target=iodstrr, args=(threads,)) new.start() new.join()''' '''printer("\n\n") printer(len(task_thread)) for i in task_thread : printer(i.name)''' '''obj = DQRR() obj.fill_threadList() #obj.insertQue_thread.start() obj.iodstrr() #time.sleep(10) printer(obj.stats())''' if __name__ == '__main__': obj = DQRR(verbose=True)
def process_generator(n=100, behaviour='inc', arrival_times=False, verbose=True): err_list = [] dqrr_list = [] err_avg_WTs = [] err_avg_TATs = [] err_CSs = [] dqrr_avg_WTs = [] dqrr_avg_TATs = [] dqrr_CSs = [] Pnames = [] pperc = 0 processes = [] if behaviour == 'inc': g_burst = 1 elif behaviour == 'dec': g_burst = n * 4 + 1 g_arrival = 0 for i in range(n): if behaviour == 'inc': g_burst += random.randint(1, 4) elif behaviour == 'rand': g_burst = random.randint(1, 100) elif behaviour == 'dec': g_burst -= random.randint(1, 4) else: print("Invalid argument provided.") exit() if arrival_times != False: g_arrival += random.randint(1, 4) Pname = 'P' + str(i + 1) Pnames.append(Pname) processes.append(i + 1) err_list.append(Process(Pname, g_burst, g_arrival)) dqrr_list.append(MyThread(id=Pname, burst=g_burst, start=g_arrival)) err_perf = ERR(err_list, verbose=False, performance_mode=True) obj = DQRR(deepcopy(dqrr_list)) #obj.insertQue_thread.daemon = True #obj.insertQue_thread.start() obj.iodstrr() dqrr_perf = obj.stats() perc = math.ceil(((i + 1) / n) * 100) if pperc != perc: pperc = perc printer("█", end='') err_avg_WTs.append(err_perf[0]) err_avg_TATs.append(err_perf[1]) err_CSs.append(err_perf[2]) dqrr_avg_WTs.append(dqrr_perf[0]) dqrr_avg_TATs.append(dqrr_perf[1]) dqrr_CSs.append(dqrr_perf[2]) data = pd.DataFrame({ 'no_of_processes': processes, 'err_avg_wait_time': err_avg_WTs, 'err_avg_turnaround_time': err_avg_TATs, 'err_context_switches': err_CSs, 'dqrr_avg_wait_time': dqrr_avg_WTs, 'dqrr_avg_turnaround_time': dqrr_avg_TATs, 'dqrr_context_switches': dqrr_CSs, }) if arrival_times == False: arrival_state = 'z' else: arrival_state = 'nz' Fname = str(n) + '_' + behaviour + '_' + arrival_state + '.csv' printer("\n%s generated." % Fname, verbose) data.to_csv('Data/' + Fname, index=False) TATcols = [ 'dqrr_avg_turnaround_time', 'err_avg_turnaround_time', 'no_of_processes' ] WTcols = ['dqrr_avg_wait_time', 'err_avg_wait_time', 'no_of_processes'] CScols = [ 'dqrr_context_switches', 'err_context_switches', 'no_of_processes' ] df = data cols = df.columns df_TAT = pd.DataFrame(df[TATcols]) df_WT = pd.DataFrame(df[WTcols]) df_CS = pd.DataFrame(df[CScols]) printer("████████████████████", end='') df_TAT.rename(columns={ 'dqrr_avg_turnaround_time': 'Dynamic Quantum Re-adjusted Round Robin', 'err_avg_turnaround_time': 'Enhanced Reactive Ratio' }, inplace=True) df_WT.rename(columns={ 'dqrr_avg_wait_time': 'Dynamic Quantum Re-adjusted Round Robin', 'err_avg_wait_time': 'Enhanced Reactive Ratio' }, inplace=True) df_CS.rename(columns={ 'dqrr_context_switches': 'Dynamic Quantum Re-adjusted Round Robin', 'err_context_switches': 'Enhanced Reactive Ratio' }, inplace=True) df_TAT_melted = pd.melt(df_TAT, id_vars='no_of_processes', value_name='time', var_name='Algorithms') df_WT_melted = pd.melt(df_WT, id_vars='no_of_processes', value_name='time', var_name='Algorithms') df_CS_melted = pd.melt(df_CS, id_vars='no_of_processes', value_name='switches', var_name='Algorithms') printer("████████████████████", end='') #TAT sns.set(style='whitegrid', font='Calibri', palette='Reds_r') TATplot = sns.lineplot(x='no_of_processes', y='time', hue='Algorithms', style='Algorithms', data=df_TAT_melted) TATtitle = Fname.split('_') TATtitle.append('| Turn Around Time') TATtitle = ' '.join(TATtitle) TATplot.set_title(TATtitle) TATfig = TATplot.get_figure() TATname = Fname + '_TAT.png' TATfig.savefig('Graphs/' + TATname) plt.clf() printer("████████████████████", end='') #WT sns.set(style='whitegrid', font='Calibri', palette='Greens_r') WTplot = sns.lineplot(x='no_of_processes', y='time', hue='Algorithms', style='Algorithms', data=df_WT_melted) WTtitle = Fname.split('_') WTtitle.append('| Waiting Time') WTtitle = ' '.join(WTtitle) WTplot.set_title(WTtitle) WTfig = WTplot.get_figure() WTname = Fname + '_WT.png' WTfig.savefig('Graphs/' + WTname) plt.clf() printer("████████████████████", end='') #CS sns.set(style='whitegrid', font='Calibri', palette='Blues_r') CSplot = sns.lineplot(x='no_of_processes', y='switches', hue='Algorithms', style='Algorithms', data=df_CS_melted) CStitle = Fname.split('_') CStitle.append('| Context Switches') CStitle = ' '.join(CStitle) CSplot.set_title(CStitle) CSfig = CSplot.get_figure() CSname = Fname + '_CS.png' CSfig.savefig('Graphs/' + CSname) plt.clf() printer("████████████████", end='') print("\n%s, %s, %s graphed.\n" % (TATname, WTname, CSname))
plt.clf() printer("████████████████", end='') print("\n%s, %s, %s graphed.\n" % (TATname, WTname, CSname)) if __name__ == '__main__': #os.remove('Data') #os.remove('Graphs') os.makedirs('Data') os.makedirs('Graphs') n = int(input("Enter number of processes to generate: ")) comb = input("Generate for all behaviours? [Y/n]: ") if comb == 'y' or comb == 'Y': printer("Generating...") behs = ['inc', 'dec', 'rand'] arrives = [True, False] for beh in behs: for arrive in arrives: process_generator(n, behaviour=beh, arrival_times=arrive) else: beh = input("Specify burst generation behaviour [inc,dec,rand]:") arrive = input("Arrival times? [Y/n]: ") printer("Generating...") if arrive == 'y' or arrive == 'Y': arrive = True else: arrive = False process_generator(n, behaviour=beh, arrival_times=arrive)