4

is there any way to obtain a POSIX named semaphore's name given its ID (sem_t) within C++?

Thanks to all and best regards.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
JoeSlav
  • 4,479
  • 4
  • 31
  • 50

1 Answers1

6

Unfortunately, no. There is no sem_name (or whatever you'd call it) function in the POSIX semaphore spec. There is also no Linux-specific workaround, since it provides no sem_name either and it does not store the name in the sem_t, which is defined in <bits/semaphore.h> as

typedef union
{
  char __size[__SIZEOF_SEM_T];
  long int __align;
} sem_t;

The files /proc/sys/kernel/sem and /proc/sysvipc/sem don't seem to contain this information either.

So, your best option is to store the name yourself when doing sem_open, preferably in a wrapper class. See this answer for an example wrapper class.

Community
  • 1
  • 1
Fred Foo
  • 355,277
  • 75
  • 744
  • 836