python实现的最低松弛度优先(LLF)算法
1#!/ur/bin/env python
2# -*- coding:utf-8 -*-
3
4import sys
5
6
7class TaskControlBlock:
8 CURRENT_TIME = 0
9
10def__init__(lf, dictionary,
11 name_list,
12 period_time,
13 central_processing_unit_time,
14 remain_time,
15 current_period,
16 laxity_time):
17for key in dictionary.keys():
18 name_list.append(key)
19 period_time.(key)[1])
20 central_processing_unit_time.(key)[0])
五分钟自我介绍
21 remain_time.(key)[0])
22 current_period.append(1)
23 laxity_time.(key)[1] - (key)[0] - lf.CURRENT_TIME)
24
25 @staticmethod
26def get_index_of_min(least_laxity_task_list, minimum):
27return least_laxity_task_list.index(minimum)
28
29 @staticmethod
30def get_another_index_of_min(least_laxity_task_list, minimum):
怎么画飞机31 least_laxity_task_list[least_laxity_task_list.index(minimum)] = sys.maxsize
32return least_laxity_task_list.index(min(least_laxity_task_list))
33
34 @staticmethod
35def is_exit(temp_list):
36for element in temp_list:
37if element != sys.maxsize:
38return Fal
39return True
40
41def scheduling(lf, 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 = []
49for i in laxity_time:
50 temp_list.append(i)
51if lf.CURRENT_TIME < period_time[least_laxity_task] * (current_period[least_laxity_task] - 1):
52while lf.CURRENT_TIME < period_time[least_laxity_task] * \
53 (current_period[least_laxity_task] - 1):
54 least_laxity_task = lf.get_another_index_of_min(temp_list, min(temp_list))
55if lf.is_exit(temp_list=temp_list):
56 exit(0)
57 another_task = lf.get_another_index_of_min(temp_list, min(temp_list))
58if remain_time[least_laxity_task] \
59 <= laxity_time[another_task]:
60 running_time = remain_time[least_laxity_task]
61el:
62 running_time = laxity_time[another_task]
63 remain_time[least_laxity_task] -= running_time
64print(name_list[least_laxity_task], lf.CURRENT_TIME, running_time)
梦见妈妈生孩子
65 lf.CURRENT_TIME += running_time
66if 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
70while i < laxity_time.__len__():
71 laxity_time[i] = current_period[i] * period_time[i] - \
72 remain_time[i] - lf.CURRENT_TIME
73 i += 1
74print(laxity_time)
75el:
76 another_task = lf.get_another_index_of_min(temp_list, min(temp_list))
77if remain_time[least_laxity_task] \
78 <= temp_list[another_task]:
79 running_time = remain_time[least_laxity_task]
80el:
81 running_time = laxity_time[another_task]
82 remain_time[least_laxity_task] -= running_time
83print(name_list[least_laxity_task], lf.CURRENT_TIME, running_time)
84 lf.CURRENT_TIME += running_time
85if 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
89while i < laxity_time.__len__():
90 laxity_time[i] = current_period[i] * period_time[i] - \
91 remain_time[i] - lf.CURRENT_TIME
92 i += 1
93print(laxity_time)
94
95
96if__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#!/ur/bin/env python
2# -*- coding:utf-8 -*-
3
4import sys
5import EarliestDeadlineFirst
6import LeastLaxityFirst
7
骆驼祥子的思维导图
8
9class GetCloTime:
10""" design the clo time by itlf """
11def__init__(lf, dictionary):
晒晒我们班的牛人作文600字12 lf.dictionary = dictionary
13
教10
14def greatest_common_divisor(lf, _left, _right):
15return _left if _right == 0 atest_common_divisor(_right, _left % _right)
16
17def lowest_common_multiple(lf):
18 temp_result = 1
19for value in lf.dictionary.values():
20 temp_result = value[1] * temp_result / lf.greatest_common_divisor(value[1], temp_result) 21return temp_result
22
23
24class TimeError(Exception):
25"""
26 Self-defined Exception :
27 Judging whether the processing time and cycle of real-time tasks satisfy the conditions 28"""
29def__init__(lf, message):
30 lf.message = message
31
32def is_execute(dictionary):
33 sum = 0
34for value in dictionary.values():
35 sum += value[0] / value[1]
36return sum
37
阜阳特色美食
38if__name__ == "__main__":
39 task_dictionary = {"A": [10, 30],
40"B": [20, 60],
41"C": [30, 90]}
42if is_execute(task_dictionary) > 1:
43rai TimeError("error, scheduling finish!")
44 ur_choo = input("Plea enter your choice"
45"(1 : EarlistDeadlineFirist) or (2 : LeastLaxityFirst) : ")
46 clo_time_object = GetCloTime(task_dictionary)
47 clo_time = clo_time_object.lowest_common_multiple()
48if int(ur_choo) == 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
63while tcb.CURRENT_TIME < clo_time:
64 tcb.scheduling(name_list,
65 period_time,
66 central_processing_unit_time,
67 remain_time,
68 current_period)
69el:
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)
84while tcb.CURRENT_TIME < clo_time:
85 tcb.scheduling(name_list,
86 period_time,
87 central_processing_unit_time,
88 remain_time,
89 current_period,
90 laxity_time)
bin