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.
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);
}
$ 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);
}
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.