-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlottery.py
110 lines (96 loc) · 4.51 KB
/
lottery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from process import *
from queue_ import Queue
from stack import Stack
from utils import *
from priority_queue import *
from random import randint
class Lottery:
def __init__(self, process_stack, quantum, pre_emptive):
self.process_stack = process_stack
self.queue = Queue()
self.quantum = quantum
self.pre_emptive = pre_emptive
self.waiting_processes = []
self.time_step = 0
self.details = {"state": [], "level": []}
self.pick_next = None
# print(self.pre_emptive)
def step(self):
if not (self.process_stack.isEmpty() and self.queue.isEmpty() and not self.waiting_processes):
# Get arrived process
arrived_processes = getArrivedProcesses(self.process_stack, self.time_step)
for process in arrived_processes:
process.quantum = self.quantum
self.queue.push(process)
for process in self.waiting_processes:
process.quantum = self.quantum
self.queue.push(process)
self.waiting_processes = []
current_max_tickets = sum(process.tickets for process in self.queue.items)
if current_max_tickets > 0:
# determine winning ticket
random_ticket = randint(0, current_max_tickets - 1)
chosen_process = None
ticket_sum = 0
for process in self.queue.items:
ticket_sum += process.tickets
if ticket_sum > random_ticket:
chosen_process = process
break
if self.pre_emptive:
# Preemptive behavior
# print('pre')
if chosen_process:
chosen_process = self.pick_next if self.pick_next != None else chosen_process
chosen_process.decrementDuration()
self.details["state"].append(chosen_process.name)
self.details["level"].append(0)
if chosen_process.duration:
if chosen_process.quantum:
self.pick_next = chosen_process
# self.queue.changePeakProcess(chosen_process)
pass
else:
self.pick_next = None
self.queue.remove(chosen_process)
self.waiting_processes.append(chosen_process)
else:
self.pick_next = None
self.queue.remove(chosen_process)
else:
self.details["state"].append("idle")
self.details["level"].append(0)
else:
# Non-preemptive behavior
# print('non-pre')
if chosen_process:
current_process = chosen_process
if current_process:
current_process = self.pick_next if self.pick_next != None else chosen_process
current_process.decrementDuration()
self.details["state"].append(current_process.name)
self.details["level"].append(0)
if current_process.duration:
self.pick_next = current_process
# self.queue.changePeakProcess(current_process)
# process.quantum = 2 # workaround for crashing, non-preemptive behavior ignores quantum value
pass
else:
self.pick_next = None
self.queue.remove(current_process)
else:
self.details["state"].append("idle")
self.details["level"].append(0)
else:
self.details["state"].append("idle")
self.details["level"].append(0)
else:
self.details["state"].append("idle")
self.details["level"].append(0)
self.time_step += 1
return True
return False
def run(self):
while self.step():
continue
return self.details