I was testing some code using Sys V Semaphores for its ability to recover from various events, and for one such test, I deleted the semaphore set (from the terminal) while the process was in its critical section. When it came time to release the lock with another call to semop
, it returned an error code with errno
set to EIDRM
.
According to the manpage for semop, these are the descriptions of what each errno
means:
EIDRM: The semaphore set was removed.
EINVAL: The semaphore set doesn't exist, or semid is less than zero, or nsops has a nonpositive value.
What I want to understand is the difference between a semaphore set that doesn't exist, and one that has been removed. I had thought that the difference was that errno
would be set to EINVAL
if the Semaphore set had been removed prior to the system call, and EIDRM
if the Semaphore set had existed at the start of the System call and been removed before completion (such as the Semaphore set being deleted while the process is blocked on the Semaphore through the system call).
The difference is:
EIDRM
— at one time, the ID value was valid, but it is no longer valid because the semaphore set has been removed.EINVAL
— the ID value is not valid now and was not a previously valid ID that was removed.It's probable that you would get EINVAL
rather than EIDRM
if you have an ID value that was valid before the last reboot but that was not re-created since the last reboot. It's possible that the duration over which removed ID values are remembered is shorter than the last reboot time — that is, if the machine is not rebooted for some months and a semaphore set with a specific ID value was removed some weeks ago, then you might get EIDRM
or EINVAL
when attempting to reuse that old ID value.