-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeterson1.py
More file actions
44 lines (31 loc) · 948 Bytes
/
Copy pathpeterson1.py
File metadata and controls
44 lines (31 loc) · 948 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
import threading
import time
import logging
class Peterson(threading.Thread):
counter = 10
logging.basicConfig(level=logging.DEBUG, format='[%(threadName)s, %(message)s]')
B = [0]*2
turn = 9
def __init__(self, threadName, index):
self.threadName = threadName
self.index = index
threading.Thread.__init__(self)
def run(self):
logging.debug('is starting ...')
time.sleep(2)
while Peterson.counter:
B[self.index] = 1
x = !self.index
turn = x
while(B[x]==1 and turn == x):
logging.debug('waiting ... ')
enterCS()
B[self.index] = 0
Peterson.counter -= 1
def enterCS():
logging.debug('executing in CS ...')
logging.debug('exiting CS ...')
t0 = Peterson('process-0', 0)
t1 = Peterson('process-1', 1)
t0.start()
t1.start()