-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path2proc_pmutex.py
More file actions
44 lines (33 loc) · 833 Bytes
/
2proc_pmutex.py
File metadata and controls
44 lines (33 loc) · 833 Bytes
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
# Author Darío Clavijo
import random
import time
import sys
import threading
interested = [False, False]
def enter_region(process):
global interested
k = 0
print("enter_region: %d" % process)
other = 1 - process
interested[process] = True
turn = process
while turn == process and interested[other] == True:
k += 1
# if (k % 10000 == 0):
# print "process wait",process
continue
print("entered_critical: %d ticks %d" % (turn, k))
def leave_region(process):
global interested
print("leave_region: %d" % process)
interested[process] = False
def proc(n):
t = random.randint(0, 1)
enter_region(n)
time.sleep(t)
leave_region(n)
ts = []
for n in range(0, 2):
t = threading.Timer(0.5, proc, args=(n,))
t.start()
ts.append(t)