# rm_scheduer.py: Rate Monotonic Scheduler # 11/28/2003 # Jiwon Hahn from sys import * def gcd(a,b): if b==0: return a return gcd(b,a%b) def lcm(a,b): return a*b/gcd(a,b) def LCM(list): return reduce(lcm, list) def load_tasks(taskfile): f=open(taskfile) data=f.read() f.close() data = data.split('\n') tasks = {} period_list = [] for i in data: i=i.split() if len(i) > 2: if i[0]=='#': continue tasks[i[0]]={} tasks[i[0]]['wcet']=int(i[1]) tasks[i[0]]['period']=int(i[2]) period_list.append(int(i[2])) lcm_period = LCM(period_list) #insert idle task tasks['idle']={'wcet':lcm_period, 'period':lcm_period+1} return tasks, lcm_period def scheduler(tasks, D): queue = tasks.keys() #initialize task queue schedule = [] curr = '' #current task prev = '' #previous task tmp = {} for task in tasks.keys(): tmp[task]={} #temporary data for each task tmp[task]['deadline']=tasks[task]['period'] tmp[task]['executed']=0 #start scheduling... #proceed by one timestamp to handle preemption for time in range(D): # insert new tasks into the queue for t in tmp.keys(): if time == tmp[t]['deadline']: if tasks[t]['wcet']>tmp[t]['executed']: print 'Scheduling Failed at %d' %time exit(1) else: tmp[t]['deadline']+=tasks[t]['period'] tmp[t]['executed']=0 queue.append(t) # select next task to be scheduled min = D*2 for task in queue: if tmp[task]['deadline']