Some problems using semaphores

Now I am working on my project, and I have a question about initializing semaphores. I actually program on Mac OS X, but I tried to compile the project on Linux and it does not compile. On OS X, it compiles, but each time it just crashes at the time of initialization.

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;
    }
}

When I tried to compile it in linux, I see the following:

/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'

Is it correct?

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);   
+3
source share
2 answers

Mac OSX is non-compliant and not supported sem_init. A function exists, but it silently fails or worsens, leaving you with an inactive semaphore.

Apple, , . , .

, / POSIX , sem_open mmap sem_init.

( , sem_open . , struct.)

+5

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);
}

However, I do not know how to do this sem_getvalue()- if anyone knows, please let me know.

0
source

All Articles