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

Posted gebicungaha

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实现的EDF(earliest deadline first)算法相关的知识,希望对你有一定的参考价值。

1. 最早截止时间优先EDF(Earliest DeadlineFirst)算法是非常著名的实时调度算法之一。在每一个新的就绪状态,调度器都是从那些已就绪但还没有完全处理完毕的任务中选择最早截止时间的任务,并将执行该任务所需的资源分配给它。在有新任务到来时,调度器必须立即计算EDF,排出新的定序,即正在运行的任务被剥夺,并且按照新任务的截止时间决定是否调度该新任务。如果新任务的最后期限早于被中断的当前任务,就立即处理新任务。按照EDF算法,被中断任务的处理将在稍后继续进行。

2. 该算法的思想是从两个任务中选择截至时间最早的任务,把它暂作为当前处理任务,再判断该任务是否在当前周期内,若不在当前周期内,就让另一任务暂作当前处理任务,若该任务也不在当前周期内,就让CPU空跑到最靠近的下一个截至时间的开始,若有任务在该周期内,就判断该任务的剩余时间是否小于当前截至时间与当前时间的差,若小于,则让该任务运行到结束.否则,就让该任务运行到该周期的截止时间,就立即抢回处理器,再判断紧接着的最早截至时间,并把处理器给它,做法同上,如此反复执行.

技术分享图片
  1 #!/user/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 
  5 class GetCloseTime:
  6     def __init__(self, dictionary):
  7         self.dictionary = dictionary
  8 
  9     def greatest_common_divisor(self, _left, _right):
 10         return _left if _right == 0 else self.greatest_common_divisor(_right, _left % _right)
 11 
 12     def lowest_common_multiple(self):
 13         temp_result = 1
 14         for value in self.dictionary.values():
 15             temp_result = value[1] * temp_result / self.greatest_common_divisor(value[1], temp_result)
 16         return temp_result
 17 
 18 
 19 class TaskControlBlock:
 20     CURRENT_TIME = 0
 21 
 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 in dictionary.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     def get_index_of_min(self, earliest_deadline_task_list, minimum):
 36         return earliest_deadline_task_list.index(minimum)
 37 
 38     def get_another_index_of_min(self, earliest_deadline_task_list, minimum):
 39         earliest_deadline_task_list[earliest_deadline_task_list.index(minimum)] = 100000
 40         return earliest_deadline_task_list.index(min(earliest_deadline_task_list))
 41 
 42     def is_execute(self, central_processing_unit_time, period_time):
 43         temp_list = [a / b for a, b in zip(central_processing_unit_time, period_time)]
 44         return sum(temp_list)
 45 
 46     def scheduling(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 in zip(period_time, current_period)],
 56             min(a * b for a, b in zip(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 in zip(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_TIME
 69 #            current_period_p = period_time[earliest_deadline_task] * current_period[earliest_deadline_task]
 70             remain_time[earliest_deadline_task] -= running_time
 71             print(name_list[earliest_deadline_task], self.CURRENT_TIME, running_time)
 72             self.CURRENT_TIME += running_time
 73             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_TIME
 82             remain_time[earliest_deadline_task] -= running_time
 83             print(name_list[earliest_deadline_task], self.CURRENT_TIME, running_time)
 84             self.CURRENT_TIME += running_time
 85             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 = 0
 98     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 < close_time:
111         tcb.scheduling(name_list,
112                        period_time,
113                        central_processing_unit_time,
114                        remain_time,
115                        current_period)
EDF算法

 



以上是关于python实现的EDF(earliest deadline first)算法的主要内容,如果未能解决你的问题,请参考以下文章

基于python的DEA绘图

数学建模 数据包络分析(DEA) Lingo实现

kafka auto.offset.reset latest earliest 详解

kafka auto.offset.reset / latest / earliest 详解

web3.eth.getBlock() 中的“latest”和“earliest”这两个参数有啥区别

History 1 : Earliest Human migration