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

转载于:https://www.cnblogs.com/gebicungaha/p/9994214.html

python实现的EDF(earliest deadline first)算法相关推荐

  1. 使用Python,OpenCV的Meanshift 和 Camshift 算法来查找和跟踪视频中的对象

    使用Python,OpenCV的Meanshift 和 Camshift 算法来查找和跟踪视频中的对象 1. 效果图 2. 源码 2.1 MeanShift 2.2 Camshift(Continuo ...

  2. 八十五、Python | Leetcode数据结构之图和动态规划算法系列

    @Author:Runsen @Date:2020/7/7 人生最重要的不是所站的位置,而是内心所朝的方向.只要我在每篇博文中写得自己体会,修炼身心:在每天的不断重复学习中,耐住寂寞,练就真功,不畏艰 ...

  3. Python+OpenCV:理解k近邻(kNN)算法(k-Nearest Neighbour (kNN) algorithm)

    Python+OpenCV:理解k近邻(kNN)算法(k-Nearest Neighbour (kNN) algorithm) 理论 kNN is one of the simplest classi ...

  4. python:实现balanced parentheses平衡括号表达式算法(附完整源码)

    python:实现balanced parentheses平衡括号表达式算法 from .stack import Stack def balanced_parentheses(parentheses ...

  5. Python:实现gauss easte高斯复活节日期算法(附完整源码)

    Python:实现gauss easte高斯复活节日期算法 import math from datetime import datetime, timedeltadef gauss_easter(y ...

  6. Python数据挖掘入门与实践-OneR分类算法

    Python数据挖掘入门与实践-OneR分类算法 OneR算法 OneR算法是根据已有的数据中,具有相同特征值的个体最可能属于哪个类别进行分类. 在本例中,只需选区Iris是个特征中分类效果最好的一个 ...

  7. Python:实现first come first served先到先得算法(附完整源码)

    Python:实现first come first served先到先得算法 from __future__ import annotationsdef calculate_waiting_times ...

  8. python:实现9×9二维数组数独算法(附完整源码)

    python:实现9×9二维数组数独算法 from __future__ import annotationsMatrix = list[list[int]]# assigning initial v ...

  9. 掌握python机器学习-读书笔记8 (抽样回归算法)

    为什么80%的码农都做不了架构师?>>>    12 抽查回归算法 今天来学习如何使用python scikit learn 来进行一些回归算法的使用. 篇幅限制, 不会对具体算法做 ...

  10. python实现kd树以及最近邻查找算法

    python实现kd树以及最近邻查找算法 一.kd树简介 二.kd树生成 1.确定切分域 2.确定数据域 3.理解递归树 4.python实现递归树代码 三.kd树上的最近邻查找算法 1.生成搜索路径 ...

最新文章

  1. 值得收藏!脑科学、脑机接口领域白皮书、研究报告汇总
  2. angularjs ngTable -Custom filter template-calendar
  3. CString,string,char数组的转换
  4. SQL相关路径查询脚本
  5. soap签名_签名SOAP消息–生成封装的XML签名
  6. Sass笔记(CSS 的预编译语言)
  7. linux php cgi.sock,nginx中unix:/tmp/php-cgi.sock错误解决解决
  8. html5+游戏+广告,给html5 游戏添加admob广告挣钱盈利
  9. HOWTO:将 IOCTL 发送到筛选器驱动程序
  10. android 主要哪些版本,你用过的最早的安卓版本是哪个?带你见识最早的安卓系统!...
  11. JPA @Lob 存储大文本 org.postgresql.util.PSQLException: 大型对象无法被使用在自动确认事物交易模式。问题...
  12. 记忆化搜索=搜索的形式+动态规划的思想(来自百度百科)
  13. Angular 2 ViewChild ViewChildren
  14. 数据分析-kaggle泰坦尼克号生存率分析
  15. 深入探讨PageRank(四):PageRank的危机及搜索引擎的未来
  16. 使用Mixamo_Converter重新定义根骨骼导入UE4
  17. linux下,Telnet连接输入正确的用户名和密码后,却还一直提示输用户名和密码,解决方案。
  18. JSP 返回上一页的几种方法
  19. vue 生命周期及watch、计算属性等等的理解
  20. fgets和gets的区别

热门文章

  1. 聚合器是什么东西?聚合器的可能性
  2. 50条最隐讳的笑话,只有最聪明的人才能理解
  3. python窗口中导入图片_Python3 tkinter基础 Text image 文本框中插入图片
  4. 通俗理解 机器学习中的偏差和方差
  5. Devc++使用及debug超详解
  6. python识别图片中的人_Python实现识别图片中的所有人脸并显示出来
  7. 使用face-api.js人脸识别让照片中的人物自动流汗(自动流汗黄豆/差不多得了)
  8. 操作日志注解aspectj-autoproxy
  9. 43.248.189.18 Steam游戏服务器搭建教程
  10. 信息系统安全实验(一):InterNIC、Nslookup、Sam spade、Nmap、Nessus的使用