python读取edf_python实现的EDF(earliest deadline first)算法

1 #!/user/bin/env python

2 #-*- coding:utf-8 -*-

3

4

5 classGetCloseTime:6 def __init__(self, dictionary):7 self.dictionary =dictionary8

9 defgreatest_common_divisor(self, _left, _right):10 return _left if _right == 0 else self.greatest_common_divisor(_right, _left %_right)11

12 deflowest_common_multiple(self):13 temp_result = 1

14 for value inself.dictionary.values():15 temp_result = value[1] * temp_result / self.greatest_common_divisor(value[1], temp_result)16 returntemp_result17

18

19 classTaskControlBlock:20 CURRENT_TIME =021

22 def __init__(self, dictionary,23 name_list,24 period_time,25 central_processing_unit_time,26 remain_time,27 current_period):28 for key indictionary.keys():29 name_list.append(key)30 period_time.append(dictionary.get(key)[1])31 central_processing_unit_time.append(dictionary.get(key)[0])32 remain_time.append(dictionary.get(key)[0])33 current_period.append(1)34

35 defget_index_of_min(self, earliest_deadline_task_list, minimum):36 returnearliest_deadline_task_list.index(minimum)37

38 defget_another_index_of_min(self, earliest_deadline_task_list, minimum):39 earliest_deadline_task_list[earliest_deadline_task_list.index(minimum)] = 100000

40 returnearliest_deadline_task_list.index(min(earliest_deadline_task_list))41

42 defis_execute(self, central_processing_unit_time, period_time):43 temp_list = [a / b for a, b inzip(central_processing_unit_time, period_time)]44 returnsum(temp_list)45

46 defscheduling(self, name_list,47 period_time,48 central_processing_unit_time,49 remain_time,50 current_period):51 if self.is_execute(central_processing_unit_time, period_time) > 1:52 print("error, scheduling finish!")53 exit(0)54 earliest_deadline_task =self.get_index_of_min(55 [a * b for a, b inzip(period_time, current_period)],56 min(a * b for a, b inzip(period_time, current_period)))57

58 if self.CURRENT_TIME < period_time[earliest_deadline_task] * (current_period[earliest_deadline_task] - 1):59 current_period_p = period_time[earliest_deadline_task] * (current_period[earliest_deadline_task] - 1)60 temp_list = [a * b for a, b inzip(period_time, current_period)]61 while self.CURRENT_TIME < period_time[earliest_deadline_task] *\62 (current_period[earliest_deadline_task] - 1):63 earliest_deadline_task =self.get_another_index_of_min(temp_list, min(temp_list))64

65 if remain_time[earliest_deadline_task] <= current_period_p -self.CURRENT_TIME:66 running_time =remain_time[earliest_deadline_task]67 else:68 running_time = current_period_p -self.CURRENT_TIME69 #current_period_p = period_time[earliest_deadline_task] * current_period[earliest_deadline_task]

70 remain_time[earliest_deadline_task] -=running_time71 print(name_list[earliest_deadline_task], self.CURRENT_TIME, running_time)72 self.CURRENT_TIME +=running_time73 if remain_time[earliest_deadline_task] ==0:74 current_period[earliest_deadline_task] += 1

75 remain_time[earliest_deadline_task] =central_processing_unit_time[earliest_deadline_task]76 else:77 current_period_p = period_time[earliest_deadline_task] *current_period[earliest_deadline_task]78 if remain_time[earliest_deadline_task] <= current_period_p -self.CURRENT_TIME:79 running_time =remain_time[earliest_deadline_task]80 else:81 running_time = current_period_p -self.CURRENT_TIME82 remain_time[earliest_deadline_task] -=running_time83 print(name_list[earliest_deadline_task], self.CURRENT_TIME, running_time)84 self.CURRENT_TIME +=running_time85 if remain_time[earliest_deadline_task] ==0:86 current_period[earliest_deadline_task] += 1

87 remain_time[earliest_deadline_task] =central_processing_unit_time[earliest_deadline_task]88

89

90 if __name__ == "__main__":91 task_dictionary = {"A": [10, 30],92 "B": [20, 60],93 "C": [30, 90]}94 close_time_object =GetCloseTime(task_dictionary)95 close_time =close_time_object.lowest_common_multiple()96

97 current_time =098 name_list =[]99 period_time =[]100 central_processing_unit_time =[]101 remain_time =[]102 current_period =[]103 tcb =TaskControlBlock(task_dictionary,104 name_list,105 period_time,106 central_processing_unit_time,107 remain_time,108 current_period)109

110 while tcb.CURRENT_TIME <111 tcb.scheduling period_time central_processing_unit_time remain_time current_period>111>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值