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)