Skip to content

Instantly share code, notes, and snippets.

@syed
Created September 7, 2012 04:36
Show Gist options
  • Save syed/3663166 to your computer and use it in GitHub Desktop.
Save syed/3663166 to your computer and use it in GitHub Desktop.
from collections import dqueue
class P(DistProcess):
def setup(ps, token_val):
ts = 0
process_set = ps
#requests: array of ts values, where requests[k] is ts of Pk's last request for token,
requests = { p : 0 for p in process_set}
requests[self] = 0
# token: array of ts values, where token[k] is ts of the token's last assignment to Pk, initialized to 0.
token = { p : 0 for p in process_set}
token[self] = 0
#token_present: boolean for whether token is present to Pi
token_present = token_val
#token_held: boolean for whether token is held by Pi, initialized to false.
token_held = False
def cs(task):
--start
#output("!!!!!!!!In start!!!!!!!->pid is %d token_present =%d token_len=%d requestlen=%d" % (self.pid,token_present, len(token), len(requests)))
if not token_present:
#output("!!!!!!!!In start sending request (%d) ts=%d!!!!!!!" % (self.pid, ts))
send(Request(logical_clock()), s)
--reply
await(token_present)
token_held = True
# critical section
--cs
task()
#time.sleep(2)
# to exit
--release
output("!!!!!!!!In release!!!!!->pid is %d ts=%d\n" % (self.pid,ts))
#time.sleep(2)
#print("||||2.token in release->||||||", token)
release_fun()
--end
def release_fun():
token[self] = ts
token_held = False
#output("!!!!In release_fun !!!! ->pid is %d len_s=%d" % (self.pid,len(s)))
#print("\n\n==> Pid =", self.pid)
#print("token->", token)
#print("request->",requests)
index = key_list.index(self)
key_list = dqueue(sorted(token))
key_list.rotate(-(index+1))
for key in key_list :
#for x, y in ((a,b) for a in [1,2,3] for b in [5,6,7]):
#for j in range(index+1, lenght),i 0, index-1):
#output("!!!!In release_fun !!!! requests[j] %d token[j]=%d" % (requests[j], token[j]))
if requests[j] > token[j] and token_present:
token_present = False
#output("!!!!In release_fun sending reply token!!!! ->pid is %d" % self.pid)
#time.sleep(2)
send(Reply(token), j) #send token to pj
break;
def main():
def anounce():
output("!!!!!!!!In cs!!!!!!!!! pid=%d" % self.pid)
#time.sleep(0.5)
while True:
cs(anounce)
# when receiving requests from others
def OnRequest(reqc):
requests[_source] = max(requests[_source], reqc)
#print("===>requests in OnRequest->, pid= ",requests, self.pid)
#output("!!!!!!!!In OnRequest!!!!!->pid is %d requests[_source]=%d reqc=%d ts=%d _timestamp=%d" % (self.pid, requests[_source], reqc, ts, _timestamp))
if token_present and not token_held :
release_fun()
def OnReply(r_token):
#output("!!!!!!!!In OnReply!!!!!->pid is %d " % self.pid)
#print("OnReply token-> local_token->", token, r_token)
token = r_token
token_present = True
def main():
#configuration: number of processes
#create n process
ps = createprocs(P, int(sys.argv[1]))
#setup the processes
print("!!!!!!Is mian starting.!!!!!!!!!")
token_given = False
for p in ps:
if not token_given
setupprocs([p], [ps-{p}, True])
token_given = True
else:
setupprocs([p], [ps-{p}, False])
startprocs(ps)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment