宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取

  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.appendkey)
 19             period_time.appenddictionary.getkey)[1])
 20             central_processing_unit_time.appenddictionary.getkey)[0])
 21             remain_time.appenddictionary.getkey)[0])
 22             current_period.append1)
 23             laxity_time.appenddictionary.getkey)[1] - dictionary.getkey)[0] - self.CURRENT_TIME)
 24 
 25     @staticmethod
 26     def get_index_of_minleast_laxity_task_list, minimum):
 27         return least_laxity_task_list.indexminimum)
 28 
 29     @staticmethod
 30     def get_another_index_of_minleast_laxity_task_list, minimum):
 31         least_laxity_task_list[least_laxity_task_list.indexminimum)] = sys.maxsize
 32         return least_laxity_task_list.indexminleast_laxity_task_list))
 33 
 34     @staticmethod
 35     def is_exittemp_list):
 36         for element in temp_list:
 37             if element != sys.maxsize:
 38                 return False
 39         return True
 40 
 41     def schedulingself, 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.indexminlaxity_time))
 48         temp_list = []
 49         for i in laxity_time:
 50             temp_list.appendi)
 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_mintemp_list, mintemp_list))
 55                 if self.is_exittemp_list=temp_list):
 56                     exit0)
 57             another_task = self.get_another_index_of_mintemp_list, mintemp_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             printname_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             printlaxity_time)
 75         else:
 76             another_task = self.get_another_index_of_mintemp_list, mintemp_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             printname_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             printlaxity_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 = TaskControlBlocktask_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_divisorself, _left, _right):
15         return _left if _right == 0 else self.greatest_common_divisor_right, _left % _right)
16 
17     def lowest_common_multipleself):
18         temp_result = 1
19         for value in self.dictionary.values):
20             temp_result = value[1] * temp_result / self.greatest_common_divisorvalue[1], temp_result)
21         return temp_result
22 
23 
24 class TimeErrorException):
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_executedictionary):
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_executetask_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 = GetCloseTimetask_dictionary)
47     close_time = close_time_object.lowest_common_multiple)
48     if intuser_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.TaskControlBlocktask_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.schedulingname_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.TaskControlBlocktask_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.schedulingname_list,
86                            period_time,
87                            central_processing_unit_time,
88                            remain_time,
89                            current_period,
90                            laxity_time)

bin