JoeSlav
JoeSlav

Reputation: 4815

Obtain POSIX semaphore's name

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.

Upvotes: 4

Views: 1538

Answers (1)

Fred Foo
Fred Foo

Reputation: 363607

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.

Upvotes: 7

Related Questions