-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRicart-Agrawala.py
More file actions
43 lines (34 loc) · 1.24 KB
/
Ricart-Agrawala.py
File metadata and controls
43 lines (34 loc) · 1.24 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
n = int(input("Enter the number of sites: "))
request_set = {}
for i in range(1, n + 1):
lst = []
for j in range(1, n + 1):
if i != j:
lst.append(j)
request_set[i] = lst
# Input of Sites Wanting to Enter CS
required_client = int(input("Enter the number of sites who wants to enter the CS: "))
required_sites = []
for i in range(required_client):
t, id = map(int, input("Enter t and id for site: ").split())
required_sites.append((t, id))
required_sites.sort()
# Request Phase Simulation
print(" -----------Request Phase---------------")
for i, id in required_sites:
for j in request_set[id]:
print(f"Request sent from {id} to {j}")
# Reply Phase + Critical Section Entry
pending_task = [id for t, id in required_sites]
for t, id in required_sites:
for i in request_set[id]:
if i not in pending_task:
print(f"Reply from {i} to {id}")
else:
for peer_time, peer_id in required_sites:
if peer_id == id and peer_time > t:
print(f"Site {i} sends reply to site {id}")
print(f"Site {id} enters the critical section")
x = input("Enter anything to exit CS....")
print(f"Site {id} exits the Critical Section\n")
pending_task.remove(id)