static sem_t* create_semaphore(const char* const name, const int value)
{
#ifdef VGO_darwin
- sem_t* p = sem_open(name, O_CREAT, 0600, value);
+ char name_and_pid[32];
+ snprintf(name_and_pid, sizeof(name_and_pid), "%s-%d", name, getpid());
+ sem_t* p = sem_open(name_and_pid, O_CREAT | O_EXCL, 0600, value);
+ if (p == SEM_FAILED) {
+ perror("sem_open");
+ return NULL;
+ }
return p;
#else
sem_t* p = malloc(sizeof(*p));
static sem_t* create_semaphore(const char* const name)
{
#ifdef VGO_darwin
- sem_t* p = sem_open(name, O_CREAT, 0600, 0);
+ char name_and_pid[32];
+ snprintf(name_and_pid, sizeof(name_and_pid), "%s-%d", name, getpid());
+ sem_t* p = sem_open(name_and_pid, O_CREAT | O_EXCL, 0600, 0);
+ if (p == SEM_FAILED) {
+ perror("sem_open");
+ return NULL;
+ }
return p;
#else
sem_t* p = malloc(sizeof(*p));
* Use the ipcs and ipcrm commands to clean up named semaphores left by
* aborted instances of this process.
*/
- snprintf(semaphore_name, sizeof(semaphore_name), "drd-sem-open-test");
- s_sem = sem_open(semaphore_name, O_CREAT, 0600, 1);
+ snprintf(semaphore_name, sizeof(semaphore_name), "drd-sem-open-test-%d",
+ getpid());
+ s_sem = sem_open(semaphore_name, O_CREAT | O_EXCL, 0600, 1);
if (s_sem == SEM_FAILED)
{
fprintf(stderr, "Failed to create a semaphore with name %s\n",