Question: print ( Hello World ) import sys from collections import deque class Node: def _ _ init _ _ ( self , v ,

print("Hello World")
import sys
from collections import deque
class Node:
def __init__(self, v, parent=None):
self.v = v
self.children =[]
self.parent = parent
self.ancestor_locked =0
self.descendant_locked =0
self.user_id =0
self.is_locked = False
def add_children(self, child_labels, parent_node):
for child_label in child_labels:
self.children.append(Node(child_label, parent_node))
def build_tree(root,m,nodes):
q= deque([root])
st=1
while q:
r=q.popleft()
if st >= len(nodes):
continue
temp = nodes[st:st+m]
r.add_children(temp,r)
st += m
for link in r.children:
q.append(link)
return root
class Tree:
def __init__(self, tree_root):
self.root = tree_root
self.vton ={}
self.output_log =[]
self.fill_vton(self.root)
def fill_vton(self, current_node):
if not current_node:
return
self.vton[current_node.v]=(current_node, set())
for child in current_node.children:
self.fill_vton(child)
def lock_node(self, v, id):
t = self.vton.get(v,(None,))[0]
if t.is_locked or len(self.vton[v][1])!=0 or t.descendant_locked !=0:
return False
curr = t.parent
while curr:
if curr.is_locked:
return False
curr = curr.parent
while curr:
curr.descendant_locked +=1
self.vton[curr.v][1].add(t)
curr = curr.parent
t.is_locked = True
t.user_id = id
return True
def unlock_node(self, v, id):
t = self.vton.get(v,(None,))[0]
if not t or not t.is_locked or t.user_id != id:
return False
curr = t.parent
while curr:
curr.descendant_locked -=1
if t in self.vton[curr.v][1]:
self.vton[curr.v][1].remove(t)
curr = curr.parent
t.is_locked = False
return True
def upgrade_node(self, v, id):
t = self.vton.get(v,(None,))[0]
if not t or t.is_locked or t.ancestor_locked !=0 or t.descendant_locked ==0:
return False
locked_descendants = list(self.vton[v][1])
for locked_descendant in locked_descendants:
if locked_descendant.user_id != id:
return False
for locked_descendant in locked_descendants:
self.unlock_node(locked_descendant.v, id)
self.lock_node(v, id)
return True
if __name__=="__main__":
import sys
input = sys.stdin.read
data = input().split()
n = int(data[0])
m = int(data[1])
q = int(data[2])
nodes = data[3:3+ n]
queries = data[3+ n:]
root = Node(nodes[0], None)
root = build_tree(root, m, nodes)
tree = Tree(root)
idx =0
results =[]
while idx < len(queries):
op = int(queries[idx])
sv = queries[idx +1]
uid = int(queries[idx +2])
idx +=3
if op ==1:
results.append("true" if tree.lock_node(sv, uid) else "false")
elif op ==2:
results.append("true" if tree.unlock_node(sv, uid) else "false")
elif op ==3:
results.append("true" if tree.upgrade_node(sv, uid) else "false")
for result in results:
print(result)
Make above code
Lets say you are running the lock/unlock in a multi core machine. Now you
want to let multiple threads to run lock() As we saw in part A, locking a
node has multiple validations inside. Will doing lock on two nodes
cause a race condition. If yes, how will you solve it. In short, how do
make the lock() function thread safe? - Multiple threads running it
simultaneously shouldnt not affect the correctness. - Try to make the
critical sections more granular. ie. dont create any big
atomic/synchronised blocks that will make parallelism suffer. Consider
each operation is atomic.
if run in a multi-threadedmachine
without using threading library , by using valiables to make function mutually exclusive

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!