views:

593

answers:

3

Hi,

I have some very basic semaphore code that works great on Linux, but cannot for the life of me get it to run properly on OS X... It returns the oddest of results...

#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>

int main()
{
    sem_t* test;
    test = sem_open("test", O_CREAT, 0, 1);

    int value;
    sem_getvalue(test, &value);
    printf("Semaphore initialized to %d\n", value);
}

Compiling this on OS X with g++ returns the following output:

iQudsi:Desktop mqudsi$ g++ test.cpp
iQudsi:Desktop mqudsi$ ./a.out 
Semaphore initialized to -1881139893

Whereas on Ubuntu, I get the decidedly more-sane result:

iQudsi: Desktop mqudsi$ g++ test.cpp -lrt
iQudsi:Desktop mqudsi$ ./a.out 
Semaphore initialized to 1

I've been at this for 3 hours straight, and cannot figure out why OS X is returning such bizarre results...

I've tried using file paths as the semaphore name, it didn't make a difference.

I'd appreciate any help I could get.

+6  A: 

Are you testing for errors? Try:

#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>

int main()
{
    sem_t* test;
    test = sem_open("test", O_CREAT, 0, 1);
    if (test == SEM_FAILED) {
        perror("sem_open");
        return 1;
    }

    int value;
    if (sem_getvalue(test, &value)) {
        perror("sem_getvalue");
        return 1;
    }
    printf("Semaphore initialized to %d\n", value);
}
bdonlan
The first check should be "if (test == SEM_FAILED)" rather than !test, since that's the documented failure value (and is actually -1, not 0).
smorgan
I posted a followup that contains the reason why his code was failing: http://stackoverflow.com/questions/871435/odd-incorrect-semaphore-behavior-on-os-x/871794#871794
X-Istence
+1  A: 

Well, perhaps sem_open() is failing - you didn't test.

Or, perhaps OSX doesn't default to supporting shared posix sems - if /dev/shm isn't mounted, typically the system won't support sem_open().

You may want to use SysV semaphores.

A similar question regarding Slackware was asked here: how-do-i-stop-semopen-failing-with-enosys

However, further searching shows OSX named semaphones are built on top of Mach semaphores, and you probably need to sem_unlink() them when you're done (not just sem_close(), or maybe instead), and you should be careful about permissions - I suggest starting with 0777 or maybe 0700, instead of 0. See posiz semaphores in Darwin

jesup
+1  A: 
$ g++ sem-testing.cc -Wall
$ ./a.out 
sem_getvalue: Function not implemented
$ man sem_getvalue
No manual entry for sem_getvalue

You are using a function that is not currently implemented in Mac OS X, and the integer you are printing out contains the default data that the integer was initialised with which was probably random data that was still in memory. Had you zero'd it out, by setting it with int value = 0; you might have caught this mistake sooner.

This is the code I used (thanks to bdonlan):

#include <iostream>
#include <fcntl.h>
#include <stdio.h>
#include <semaphore.h>

int main()
{
    sem_t* test;
    test = sem_open("test", O_CREAT, 0, 1);
    if (test == SEM_FAILED) {
        perror("sem_open");
        return 1;
    }

    int value;
    if (sem_getvalue(test, &value)) {
        perror("sem_getvalue");
        return 1;
    }
    printf("Semaphore initialized to %d\n", value);
}
X-Istence