-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFast.py
More file actions
115 lines (89 loc) · 3.11 KB
/
Copy pathFast.py
File metadata and controls
115 lines (89 loc) · 3.11 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#---------------------------------Lamport's Fast mutual exclusion algorithm------------------------------------------#
import threading
import time
import logging
import random
# global variables
x = 0
y = -1
totalRequests = 0
Threads = []
runningThreads = []
B = [0]*100
# using the logging module from python's library which basically formats the output to be displayed on the console
logging.basicConfig(level=logging.DEBUG, format='[%(threadName)s, %(asctime)s, %(message)s]', datefmt = '%Y-%m-%d %H:%M:%S')
class Fast(threading.Thread):
# the constructor which assigns the requests, threadname, index to the respective thread
def __init__(self, string, i, z):
self.threadName = string+str(i)
self.index = i
self.requests = z
threading.Thread.__init__(self, name=self.threadName)
#invoked after the thread.start()
def run(self):
logging.debug('is started ... ')
# calling a global function which handles the number of requests of each thread, decrements its value by 1 after entering and exiting a CS once.
requestManager(self)
def requestManager(self):
# total requests as entered by the user i.e. sys.argv[2]
global totalRequests
# keep doing until all the requests of all the threads have been satisfied
while totalRequests > 0:
#if all the requests are satisfied - do nothing
if self.requests == 0:
pass
# else do this
else:
contend(self)
totalRequests-=1
# a global function which contains the logic of the lamport's fast mutual exclusion algorithm
def contend(self):
global y
global x
logging.debug('requesting CS')
B[self.index] = 1
x = self.index
logging.debug('...')
if y != -1:
logging.debug('...')
B[self.index] = 0
while(not( y == -1)):
logging.debug("...")
contend(self)
y = self.index
logging.debug('...')
if x != self.index:
logging.debug('...')
B[self.index] = 0
for each in Threads:
while(not( B[each.index] == 0)):
logging.debug("...")
if y != self.index:
logging.debug('...')
while(not( y == -1)):
logging.debug("...")
contend(self)
cs()
logging.debug('Exiting CS')
y = -1
B[self.index] = 0
#make the thread sleep for a good 3 seconds! lucky one gets to sleep more!
time.sleep(3)
#global cs function
def cs():
logging.debug('Entering CS')
# method which calls the constructor of the Fast class and invokes the threads
def spawnThreads(assign):
i = 0
global totalRequests
for z in assign:
totalRequests+=z
Threads.append(Fast("THREAD", i, z))
i+=1
for each in Threads:
if each.requests > 0:
runningThreads.append(each)
each.start()
# wait until all the threads are done with their requests, then exit
for each in runningThreads:
each.join()