-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSleepQueue.py
67 lines (45 loc) · 1.65 KB
/
SleepQueue.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
import random
class SleepQueue:
'''
INSTANCE VARIABLES:\n
sleepQueue : a dictionary of the form\n\t\t\t {blockNum: list of PIDs of processes waiting for this block }
revSleepQueue : a dictionary of the form {pid : blockNum} for fast removal
'''
def __init__(self):
'''
Constructor : Initializes the Sleep Queue
'''
self.sleepQueue = {}
self.revSleepQueue = {}
def add(self, blockNum, pid):
'''
Adds the pid of the process waiting for blockNum to get free in the Sleep Queue.\n
Give blockNum -1 to add pid of a process waiting for any Block Number to get free in the Sleep Queue.
'''
if blockNum in self.sleepQueue:
self.sleepQueue[blockNum].append(pid)
else:
self.sleepQueue[blockNum] = [pid]
self.revSleepQueue[pid] = blockNum
print("Process",pid,": Added to the sleep queue")
def printSQ(self):
'''
Prints the contents of the Sleep Queue.
'''
print("-----SLEEP QUEUE-----")
for blockNo in self.sleepQueue:
st = "["
for pid in self.sleepQueue[blockNo]:
st += str(pid) + ", "
st += "]"
print(blockNo, ":", st)
def getPidsWaitingForBuffer(self,buffer):
'''
Returns pids waiting for a specific buffer
'''
return self.sleepQueue.pop(buffer,-2)
def getPidsWaitingForAnyBuffer(self):
'''
Returns pids waiting for any buffer
'''
return self.sleepQueue.pop(-1,-2)