A question on semaphore value if a process terminates abnormally after decrementing the semaphore value
if a process goes down after decrementing the value o开发者_StackOverflow中文版f semaphore from 1 to 0, as per my understanding the value of semaphore remains as 0.
If the same process comes up again and tries to re-acquire the same semaphore, it will block for ever.
Is there a way to reset the semaphore value to 1 after decrementing it if my process goes down abnormally?
I assume you are using the standard semctl()/semop() API for the semaphores.
There is a flag available called SEM_UNDO that will tell the kernel to reset the semaphore's value if your process terminates. You need to use this in your semop( ) calls.
More information here : http://beej.us/guide/bgipc/output/html/multipage/semaphores.html
精彩评论