-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock-variable.py
57 lines (47 loc) · 1.45 KB
/
lock-variable.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
import time
import random
import threading
class Memory():
def __init__(self):
self.lock = False
self.count = 0
def critical_region(self, id):
while self.lock is True:
print("Process {} is waiting to in critical region...".format(id))
time.sleep(random.randint(1,5))
self.lock = True
self.count += 1
print("Process {} is in critical region...".format(id))
if self.count == 2:
print("2 processes in critical region")
raise Exception()
time.sleep(random.randint(1, 5))
self.lock = False
self.count -= 1
print("Process {} left critical region".format(id))
class Process(threading.Thread):
def __init__(self, id, memory):
super().__init__()
self.id = id
self.memory = memory
def run(self):
while True:
try:
self.memory.critical_region(self.id)
time.sleep(random.randint(1, 5))
except e as Exception:
raise Exception()
def main():
memory = Memory()
n = random.randint(2, 5)
processes = []
for i in range(n):
p = Process(i, memory)
print("Created Process {}..." .format(i))
p.daemon = True
p.start()
processes.append(p)
for p in processes:
p.join()
if __name__ == "__main__":
main()