-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clock.py
46 lines (39 loc) · 1.78 KB
/
Clock.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
class Clock:
def __init__(self):
self.time = -1 # to handle the case when a process arrives at time 0
self.scheduler = None
self.process_manager = None
self.process_notification = -1
self.scheduler_notification = -1
def __repr__(self):
return repr((self.time, self.process_notification, self.scheduler_notification))
def attach_scheduler(self, scheduler):
self.scheduler = scheduler
def attach_process_manager(self, process_manager):
self.process_manager = process_manager
def notify(self):
if self.scheduler_notification == self.process_notification:
if self.scheduler_notification > self.time:
self.time = self.scheduler_notification
self.scheduler.notify()
self.process_manager.notify()
elif self.scheduler_notification < self.process_notification:
if self.scheduler_notification > self.time:
self.time = self.scheduler_notification
self.scheduler.notify()
elif self.process_notification > self.time:
self.time = self.process_notification
self.process_manager.notify()
else:
if self.process_notification > self.time:
self.time = self.process_notification
self.process_manager.notify()
elif self.scheduler_notification > self.time:
self.time = self.scheduler_notification
self.scheduler.notify()
# notify scheduler with time relative to current one
def notify_scheduler(self, time):
self.scheduler_notification = time
# notify process manager with absolute time
def notify_process_manager(self, time):
self.process_notification = time