I was studying the dining philosopher problem (See problem description) and its solution using semaphores, and I came across this :
**Code for the ith philosopher:**
while(1)
{
take_chopstick[i];
take_chopstick[ (i+1) % 5] ;
EATING THE NOODLE
put_chopstick[i] );
put_chopstick[ (i+1) % 5] ;
THINKING
}
I had two doubts here:
Can the order of putting back the chopsticks be reversed, i.e. the (i+1) th chopstick is put back first, and then the ith chopstick? Why/Why not?
In general, when we have two semaphores, do we release that semaphore first, which was locked later? Or are there any guidelines regarding the same?