Given an n-ary tree of resources arranged hierarchically such that height of tree is O(Log N) where N is total number of nodes (or resources). A process needs to lock a resource node in order to use it. But a node cannot be locked if any of its descendant or ancestor is locked. Following operations are required for a given resource node: Lock()- locks the given node if possible and updates lock information. Lock is possible only if ancestors and descendants of current node are not locked.
Given an n-ary tree of resources arranged hierarchically such that height of tree is O(Log N) where N is total number of nodes (or resources). A process needs to lock a resource node in order to use it. But a node cannot be locked if any of its descendant or ancestor is locked. Following operations are required for a given resource node:
Lock()- locks the given node if possible and updates lock information. Lock is possible only if ancestors and descendants of current node are not locked.
unLock()- unlocks the node and updates information.
How design resource nodes and implement above operations such that following time complexities are achieved.
Lock() O(log N)
unLock() O(log N)
Part b : -
Let's say you are running the lock/unlock in a multi core machine. Now you want to let multiple threads to run lock() simultaneously.
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 shouldn't not affect the correctness.
Note that the critical sections should be granular.
Don't create any big transaction blocks that will make parallelism suffer.
condition :- making the function synchronized is not allowed but we can use upgradelock
we cannot use inbuilt mutex , semaphores,synchornize
I Want solution for part b
Trending now
This is a popular solution!
Step by step
Solved in 2 steps
Implement thread safe concurrent lock method without using any atomic method, mutex, semaphores, synchronized keywork without using
Please modify below code such that it become thread safe without using Operating System concepts.
I want solution for lock function to make it thread safe
I also need code for the same