3

现在我正在做我的项目,我有一个关于信号量初始化的问题。实际上我在 Mac OS X 上编程,但我试图在 Linux 上编译我的项目,但它没有编译。在 OS X 上,它可以编译,但每次都在初始化时崩溃。

sem_t *mutex_1, *mutex_2, *mutex_3, *reader, *writer;

int initialization_semaphores (void) 
{
    int ERROR = EOK;
    if ((mutex_1 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
        ERROR = ESEM;
    if ((mutex_2 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
        ERROR = ESEM;
    if ((mutex_3 = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
        ERROR = ESEM;
    if ((reader = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
        ERROR = ESEM;
    if ((writer = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, 0, 0)) == MAP_FAILED)
        ERROR = ESEM;

    if (ERROR == EOK) {
        if (sem_init(mutex_1, 1, 1) == -1)
            ERROR = ESEM;
        if (sem_init(mutex_2, 1, 1) == -1)
            ERROR = ESEM;
        if (sem_init(mutex_3, 1, 1) == -1)
            ERROR = ESEM;
        if (sem_init(reader, 1, 1) == -1)
            ERROR = ESEM;
        if (sem_init(writer, 1, 1) == -1)
            ERROR = ESEM;
    }
}

当我尝试在 linux 上编译它时,我看到了这个:

/tmp/ccmkN9G7.o: In function `initialization_semaphores':
readerWriter.c:(.text+0x1a2): undefined reference to `sem_init'
readerWriter.c:(.text+0x1cb): undefined reference to `sem_init'
readerWriter.c:(.text+0x1f4): undefined reference to `sem_init'
readerWriter.c:(.text+0x21d): undefined reference to `sem_init'
readerWriter.c:(.text+0x246): undefined reference to `sem_init'
readerWriter.c:(.text+0x275): undefined reference to `shm_open'

这样对吗?:

int ERROR = EOK;
mutex_1 = sem_open("mutex1", O_CREAT, S_IRUSR | S_IWUSR, 1);   
mutex_2 = sem_open("mutex2", O_CREAT, S_IRUSR | S_IWUSR, 1);  
mutex_3 = sem_open("mutex3", O_CREAT, S_IRUSR | S_IWUSR, 1); 
reader = sem_open("reader", O_CREAT, S_IRUSR | S_IWUSR, 1); 
writer = sem_open("writer", O_CREAT, S_IRUSR | S_IWUSR, 1);   
4

2 回答 2

5

Mac OSX 不符合标准,不支持sem_init. 该功能存在,但它默默地失败或更糟,留下一个不工作的信号量。

我鼓励您向 Apple 提交错误,因为这是一个严重影响应用程序可移植性的长期存在的真实问题。抱怨的人越多,解决问题的希望就越大。

至于解决它,您可以尝试查找/编写所有 POSIX 信号量函数的替代实现并将您的程序链接到该实现,或者您可以切换到 usingsem_open而不是mmapand sem_init

(只要您已经经历了为每个信号量映射整个页面的开销,sem_open实际上并不会花费您更多的成本。当您想将信号量包含在现有的struct.)

于 2012-05-02T02:14:53.807 回答
0

Mac OSX 上有一个解决方法。

#include <dispatch/dispatch.h>

typedef dispatch_semaphore_t sem_t;

void sem_init(sem_t* sem, bool is_pshared, unsigned value)
{
    *sem = dispatch_semaphore_create(value);
}

static void sem_destroy(sem_t* sem)
{
    dispatch_release(*sem);
}

static void sem_post(sem_t* sem)
{
    dispatch_semaphore_signal(*sem);
}

static void sem_wait(sem_t* sem)
{
    dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER);
}

但是,我不知道该怎么做sem_getvalue()-如果有人知道,请告诉我。

于 2014-09-06T03:30:15.813 回答