-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeterson.py
More file actions
49 lines (29 loc) · 1013 Bytes
/
Copy pathpeterson.py
File metadata and controls
49 lines (29 loc) · 1013 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
45
46
47
48
49
import threading
import time
import logging
B = [0]*2
turn = 9
class Peterson(threading.Thread):
counter = 10
logging.basicConfig(level=logging.DEBUG, format='[%(threadName)s, %(message)s]')
def __init__(self, index):
self.index = index
threading.Thread.__init__(self)
def run(self):
logging.debug('is starting ...')
B[self.index] = 1
turn = self.index
#print threading.currentThread().getName() + " " + str(B[not self.index])
#print threading.currentThread().getName()+ " " + str(turn)
while(B[not self.index] is 1 and turn is self.index ):
logging.debug('waiting ... ')
enterCS()
B[self.index] = 0
#print threading.currentThread().getName() + " " +str(B[self.index])
def enterCS():
logging.debug('executing in CS ...')
logging.debug('exiting CS ...')
t0 = Peterson(0)
t1 = Peterson(1)
t0.start()
t1.start()