2

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).

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29

2 Answers2

1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

Perhaps the second error is for when you open the semaphore with the path, and the first is if it is deleted when in use, referenced through the semaphore that was opened. The code for much of this in many OS is open source! Working comes a just a few flavors, broken comes in infinite flavors!

  • System V semaphores aren't opened by path. You can feed a path into `ftok` to get a key to pass into `semget` to return an id to pass into `semop` to operate on the semaphore. But `semop` itself just takes that id as an argument. – Christian Gibbons Aug 20 '20 at 20:31