Re: Semaphores and threads anomaly and bug?

From: Manfred Spraul
Date: Tue Nov 04 2003 - 13:00:45 EST


John wrote:

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.

I think that in ipc/sem.c line 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.

There is a potential problem here in that the code in ipc/sem.c doesn'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 with SEM_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

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/