Bloomberg LP Interview Question
Financial Software DevelopersCountry: United States
Interview Type: Phone Interview
Well I can answer this in terms of Software Lock.
1> Use some map or even 2 dimensional array / vector will do.
2> First index use to determine whether we can do the operation on the second index value.
3> Second index use the memory location where the object in context resides.
4> Once the process gets the access change the value of first index to "NOT OPERATABLE" State and release back once it stops its execution.
Disadvantage :-
The Scheduler itself is not thread safe.
This is not safe.
What if you get swapped out after testing that you can access the shared resource, but before changing the "first index" to "Not Operatable". Then the swapped in process does the same and sees the "first index" is not set to "Not Operatable" so it sets it and starts to modify the shared resource. During this time there may then be a swap back to the first process which then sets the "first index" to "Not Operatable" and also starts modifying the shared resource.
To do this properly you need something similar to an atomic "test and set (TAS)" operation. This tests and sets a flag in an uninterruptable single instruction. Most operating systems will provide some form of mutex for this.
use mutex
- Rocky March 04, 2013