NVIDIA Interview Question
Software Engineer / DevelopersI use semaphore. Pseudo code:
int mutex = 1;
function process() {
mutex --; // acquire lock
if ( mutex <= 0 )
{
mutex ++; // wrong, release lock
wait();
}
// process stuffs
//release lock
signal();
}
it is not deadlock free :(
consider this sequences..
1>p1 comes set mutex to 0
2>p2 comes set mutex to -1;
3> both will enter into if condition
4> they would increase mutex to 1 and would hang into wait command
peterson's algorithm
- Anonymous February 28, 2011