Semaphores and threads anomaly and bug?
Quoted 5 lines · John
I know this isn't defined anywhere but the seems to be an ambiguity and discrepancy between versions of Unix and Linux over threads and semaphores.
Do the "SEM_UNDO"s get applied when a thread terminates or when the "whole thing" terminates?
According to the Unix spec: per-process.
Older Linux kernels applied it per-thread. Newer kernels can handle it per-process, and AFAIK it's the default for NPTL.
Quoted 3 lines
I think that in
ipc/sem.cline 1062 the line should be made conditional on "u->semadj[i]" being non-zero.
Fixed in 2.6. But there is another bug in that block: undos can increase the semaphore value above SEMVMX.
Quoted 3 lines
There is a potential problem here in that the code in
ipc/sem.cdoesn't allow the adjustment to yield a negative value but what if it starts at zero, thread A increments it, thread B decrements it back to zero (both withSEM_UNDO) and thread A exits first? Thread A's undo won't work and then thread B's undo will increment it again leaving it in an incorrect state which is different from thread B exiting first.
Correct. undo operations should never try to decrease the semaphore value - an attempt to decrease below 0 is either silently ignored, or the semaphore value is set to 0.
--
Manfred