码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-21 16:16:42      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:执行   bdc   分配   结束   优先   dea   none   minimum   __name__   

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)算法

标签:执行   bdc   分配   结束   优先   dea   none   minimum   __name__   

原文地址:https://www.cnblogs.com/gebicungaha/p/9994214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!