Given a world map in the form of a Generic M-ary Tree consisting of N nodes and an array of queries[], the task is to implement the functions Lock, Unlock and Upgrade for the given tree. For each query in queries[], the functions return true when the operation is performed successfully, otherwise, it returns false. The functions are defined as: X: Name of the node in the tree and will be unique uid: User Id for the person who accesses node X 1. Lock(X, uid): Lock takes exclusive access to the subtree rooted. - Once Lock(X, uid) succeeds, then lock(A, any user) should fail, where A is a descendant of X. - Lock(B. any user) should fail where X is a descendant of B. - Lock operation cannot be performed on a node that is already locked. 2. Unlock(X, uid): To unlock the locked node. - The unlock reverts what was done by the Lock operation. - It can only be called on same and unlocked by same uid. 3. UpgradeLock(X, uid): The user uid can upgrade their lock to an ancestor node. - It is only possible if any ancestor node is only locked by the same user uid. - The Upgrade should fail if there is any node that is locked by some other uid Y below. Examples: Input: N = 7, M = 2, nodes = [‘World’, ‘Asia’, ‘Africa’, ‘China’, ‘India’, ‘SouthAfrica’, ‘Egypt’], queries = [‘1 China 9’, ‘1 India 9’, ‘3 Asia 9’, ‘2 India 9’, ‘2 Asia 9’] Output: true true true false true Input: N = 3, M = 2, nodes = [‘World’, ‘China’, ‘India’], queries = [‘3 India 1’, ‘1 World 9’] Output: false true The same question with its Java and Python code can be found on the GFG platform at https://www.geeksforgeeks.org/tree-of-space-locking-and-unlocking-n-ary-tree/ Please let me know the solution to the following problem: Let's say you are running the lock/unlock on a multi-core machine. Now you want to let multiple threads run lock() simultaneously. As we know, locking a node has multiple validations inside. Doing lock on two nodes will definitely cause a race condition. How to solve this race condition? In short, how do make the lock() function thread safe? There are a few contraints that need to be followed while solving the problem: - Multiple threads running it simultaneously shouldn't affect the correctness. - Note that the critical sections should be granular. - Don't create any big transaction blocks that will make parallelism suffer. - making the function synchronized is not allowed but we can use upgradelock - we cannot use an inbuilt mutex, semaphores, synchronize. Give me the "detail" approaches (no need for the code) to the above problem with proper reasoning.
Given a world map in the form of a Generic M-ary Tree consisting of N nodes and an array of queries[], the task is to implement the functions Lock, Unlock and Upgrade for the given tree. For each query in queries[], the functions return true when the operation is performed successfully, otherwise, it returns false.
The functions are defined as:
X: Name of the node in the tree and will be unique
uid: User Id for the person who accesses node X
1. Lock(X, uid): Lock takes exclusive access to the subtree rooted.
- Once Lock(X, uid) succeeds, then lock(A, any user) should fail, where A is a descendant of X.
- Lock(B. any user) should fail where X is a descendant of B.
- Lock operation cannot be performed on a node that is already locked.
2. Unlock(X, uid): To unlock the locked node.
- The unlock reverts what was done by the Lock operation.
- It can only be called on same and unlocked by same uid.
3. UpgradeLock(X, uid): The user uid can upgrade their lock to an ancestor node.
- It is only possible if any ancestor node is only locked by the same user uid.
- The Upgrade should fail if there is any node that is locked by some other uid Y below.
Examples:
Input: N = 7, M = 2, nodes = [‘World’, ‘Asia’, ‘Africa’, ‘China’, ‘India’, ‘SouthAfrica’, ‘Egypt’],
queries = [‘1 China 9’, ‘1 India 9’, ‘3 Asia 9’, ‘2 India 9’, ‘2 Asia 9’]
Output: true true true false true
Input: N = 3, M = 2, nodes = [‘World’, ‘China’, ‘India’],
queries = [‘3 India 1’, ‘1 World 9’]
Output: false true
The same question with its Java and Python code can be found on the GFG platform at https://www.geeksforgeeks.org/tree-of-space-locking-and-unlocking-n-ary-tree/
Please let me know the solution to the following problem:
Let's say you are running the lock/unlock on a multi-core machine. Now you want to let multiple threads run lock() simultaneously.
As we know, locking a node has multiple validations inside. Doing lock on two nodes will definitely cause a race condition.
How to solve this race condition?
In short, how do make the lock() function thread safe?
There are a few contraints that need to be followed while solving the problem:
- Multiple threads running it simultaneously shouldn't affect the correctness.
- Note that the critical sections should be granular.
- Don't create any big transaction blocks that will make parallelism suffer.
- making the function synchronized is not allowed but we can use upgradelock
- we cannot use an inbuilt mutex, semaphores, synchronize.
Give me the "detail" approaches (no need for the code) to the above problem with proper reasoning.

Step by step
Solved in 4 steps with 1 images

Not satisfied with your answer, please extend your answer








