简体   繁体   English

OS X上的奇怪/不正确的sem_getvalue信号量行为

[英]Odd/Incorrect sem_getvalue Semaphore Behavior on OS X

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... 我有一些非常基本的信号量代码在Linux上运行良好,但在我的生活中不能让它在OS X上正常运行...它返回最奇怪的结果...

#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: 使用g ++在OS X上编译它会返回以下输出:

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: 而在Ubuntu上,我获得了更明智的结果:

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... 我已经连续3个小时了,并且无法弄清楚为什么OS X会返回如此奇怪的结果......

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. 您正在使用当前未在Mac OS X中实现的功能,并且您要打印的整数包含初始化整数的默认数据,该数据可能是仍在内存中的随机数据。 Had you zero'd it out, by setting it with int value = 0; 如果你把它归零,通过设置int value = 0; you might have caught this mistake sooner. 你可能早就抓住了这个错误。

This is the code I used (thanks to bdonlan ): 这是我使用的代码(感谢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. 好吧,也许sem_open()失败了 - 你没有测试。

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(). 或者,也许OSX不默认支持共享posix sems - 如果未安装/ dev / shm,通常系统将不支持sem_open()。

You may want to use SysV semaphores. 您可能想要使用SysV信号量。

A similar question regarding Slackware was asked here: how-do-i-stop-semopen-failing-with-enosys 关于Slackware的一个类似的问题在这里被问到: 怎么办-se-stop-semopen-faileding 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 但是,进一步的搜索显示OSX名为semaphones建立在Mach信号量之上,你可能需要sem_unlink()它们完成后(不仅仅是sem_close(),或者可能代替),你应该小心权限 - 我建议从0777或者0700开始,而不是0。请参阅达尔文的posiz信号量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM