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

python实现的最低松弛度优先(LLF)算法

时间:2018-12-28 10:47:20      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:cut   real   ssi   set   values   fine   please   tde   ESS   

 

技术分享图片
  1 #!/user/bin/env python
  2 # -*- coding:utf-8 -*-
  3 
  4 import sys
  5 
  6 
  7 class TaskControlBlock:
  8     CURRENT_TIME = 0
  9 
 10     def __init__(self, dictionary,
 11                  name_list,
 12                  period_time,
 13                  central_processing_unit_time,
 14                  remain_time,
 15                  current_period,
 16                  laxity_time):
 17         for key in dictionary.keys():
 18             name_list.append(key)
 19             period_time.append(dictionary.get(key)[1])
 20             central_processing_unit_time.append(dictionary.get(key)[0])
 21             remain_time.append(dictionary.get(key)[0])
 22             current_period.append(1)
 23             laxity_time.append(dictionary.get(key)[1] - dictionary.get(key)[0] - self.CURRENT_TIME)
 24 
 25     @staticmethod
 26     def get_index_of_min(least_laxity_task_list, minimum):
 27         return least_laxity_task_list.index(minimum)
 28 
 29     @staticmethod
 30     def get_another_index_of_min(least_laxity_task_list, minimum):
 31         least_laxity_task_list[least_laxity_task_list.index(minimum)] = sys.maxsize
 32         return least_laxity_task_list.index(min(least_laxity_task_list))
 33 
 34     @staticmethod
 35     def is_exit(temp_list):
 36         for element in temp_list:
 37             if element != sys.maxsize:
 38                 return False
 39         return True
 40 
 41     def scheduling(self, name_list,
 42                    period_time,
 43                    central_processing_unit_time,
 44                    remain_time,
 45                    current_period,
 46                    laxity_time):
 47         least_laxity_task = laxity_time.index(min(laxity_time))
 48         temp_list = []
 49         for i in laxity_time:
 50             temp_list.append(i)
 51         if self.CURRENT_TIME < period_time[least_laxity_task] * (current_period[least_laxity_task] - 1):
 52             while self.CURRENT_TIME < period_time[least_laxity_task] *  53                     (current_period[least_laxity_task] - 1):
 54                 least_laxity_task = self.get_another_index_of_min(temp_list, min(temp_list))
 55                 if self.is_exit(temp_list=temp_list):
 56                     exit(0)
 57             another_task = self.get_another_index_of_min(temp_list, min(temp_list))
 58             if remain_time[least_laxity_task]  59                     <= laxity_time[another_task]:
 60                 running_time = remain_time[least_laxity_task]
 61             else:
 62                 running_time = laxity_time[another_task]
 63             remain_time[least_laxity_task] -= running_time
 64             print(name_list[least_laxity_task], self.CURRENT_TIME, running_time)
 65             self.CURRENT_TIME += running_time
 66             if remain_time[least_laxity_task] == 0:
 67                 current_period[least_laxity_task] += 1
 68                 remain_time[least_laxity_task] = central_processing_unit_time[least_laxity_task]
 69             i = 0
 70             while i < laxity_time.__len__():
 71                 laxity_time[i] = current_period[i] * period_time[i] -  72                                  remain_time[i] - self.CURRENT_TIME
 73                 i += 1
 74             print(laxity_time)
 75         else:
 76             another_task = self.get_another_index_of_min(temp_list, min(temp_list))
 77             if remain_time[least_laxity_task]  78                     <= temp_list[another_task]:
 79                 running_time = remain_time[least_laxity_task]
 80             else:
 81                 running_time = laxity_time[another_task]
 82             remain_time[least_laxity_task] -= running_time
 83             print(name_list[least_laxity_task], self.CURRENT_TIME, running_time)
 84             self.CURRENT_TIME += running_time
 85             if remain_time[least_laxity_task] == 0:
 86                 current_period[least_laxity_task] += 1
 87                 remain_time[least_laxity_task] = central_processing_unit_time[least_laxity_task]
 88             i = 0
 89             while i < laxity_time.__len__():
 90                 laxity_time[i] = current_period[i] * period_time[i] -  91                                  remain_time[i] - self.CURRENT_TIME
 92                 i += 1
 93             print(laxity_time)
 94 
 95 
 96 if __name__ == "__main__":
 97     task_dictionary = {"A": [10, 30],
 98                        "B": [25, 75],
 99                        "C": [30, 90],}
100     current_time = 0
101     name_list = []
102     period_time = []
103     central_processing_unit_time = []
104     remain_time = []
105     current_period = []
106     laxity_time = []
107     tcb = TaskControlBlock(task_dictionary,
108                            name_list,
109                            period_time,
110                            central_processing_unit_time,
111                            remain_time,
112                            current_period,
113                            laxity_time)
LLF Module
技术分享图片
 1 #!/user/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import sys
 5 import EarliestDeadlineFirst
 6 import LeastLaxityFirst
 7 
 8 
 9 class GetCloseTime:
10     """ design the close time by itself """
11     def __init__(self, dictionary):
12         self.dictionary = dictionary
13 
14     def greatest_common_divisor(self, _left, _right):
15         return _left if _right == 0 else self.greatest_common_divisor(_right, _left % _right)
16 
17     def lowest_common_multiple(self):
18         temp_result = 1
19         for value in self.dictionary.values():
20             temp_result = value[1] * temp_result / self.greatest_common_divisor(value[1], temp_result)
21         return temp_result
22 
23 
24 class TimeError(Exception):
25     """
26     Self-defined Exception :
27         Judging whether the processing time and cycle of real-time tasks satisfy the conditions
28     """
29     def __init__(self, message):
30         self.message = message
31 
32 def is_execute(dictionary):
33     sum = 0
34     for value in dictionary.values():
35         sum += value[0] / value[1]
36     return sum
37 
38 if __name__ == "__main__":
39     task_dictionary = {"A": [10, 30],
40                        "B": [20, 60],
41                        "C": [30, 90]}
42     if is_execute(task_dictionary) > 1:
43         raise TimeError("error, scheduling finish!")
44     user_choose = input("Please enter your choice"
45                         "(1 : EarlistDeadlineFirist) or (2 : LeastLaxityFirst) : ")
46     close_time_object = GetCloseTime(task_dictionary)
47     close_time = close_time_object.lowest_common_multiple()
48     if int(user_choose) == 1:
49         current_time = 0
50         name_list = []
51         period_time = []
52         central_processing_unit_time = []
53         remain_time = []
54         current_period = []
55         tcb = EarliestDeadlineFirst.TaskControlBlock(task_dictionary,
56                                name_list,
57                                period_time,
58                                central_processing_unit_time,
59                                remain_time,
60                                current_period)
61 
62 
63         while tcb.CURRENT_TIME < close_time:
64             tcb.scheduling(name_list,
65                            period_time,
66                            central_processing_unit_time,
67                            remain_time,
68                            current_period)
69     else:
70         current_time = 0
71         name_list = []
72         period_time = []
73         central_processing_unit_time = []
74         remain_time = []
75         current_period = []
76         laxity_time = []
77         tcb = LeastLaxityFirst.TaskControlBlock(task_dictionary,
78                                name_list,
79                                period_time,
80                                central_processing_unit_time,
81                                remain_time,
82                                current_period,
83                                laxity_time)
84         while tcb.CURRENT_TIME < close_time:
85             tcb.scheduling(name_list,
86                            period_time,
87                            central_processing_unit_time,
88                            remain_time,
89                            current_period,
90                            laxity_time)
bin

 

python实现的最低松弛度优先(LLF)算法

标签:cut   real   ssi   set   values   fine   please   tde   ESS   

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

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