0

I'm learning to use semaphore object. But I can't initialize it. A sem_init function always return value -1 rain or shine.

return value -1 indicates first argument is not valid pointer, say my reference. But I can't find miss print in my code. I compiled my code in Xcode on OS X.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void * thread_snd(void *arg);
void * thread_rcv(void* arg);

sem_t bin_sem;
int number = 0;

char thread1[] = "A thread";
char thread2[] = "B thread";
char thread3[] = "C thread";

int main(int argc, char** argv)
{
    pthread_t t1, t2 ,t3;
    void *thread_result;
    int state;

    state       =   sem_init(&bin_sem, 0, 0);
    if(state != 0)
    {
        puts("fail to initialize semaphore");
        exit(1);
    }

    pthread_create(&t1, NULL, thread_snd, &thread1);
    pthread_create(&t2, NULL, thread_rcv, &thread2);
    pthread_create(&t3, NULL, thread_rcv, &thread3);

    pthread_join(t1, &thread_result);
    pthread_join(t2, &thread_result);
    pthread_join(t3, &thread_result);

    printf("final number : %d \n", number);
    sem_destroy(&bin_sem);
    return 0;
}

void * thread_snd(void * arg)
{
    int i;
    for(i = 0 ; i < 4; i++)
    {
        while(number != 0)
            sleep(1);
        number++;
        printf("execution : %s, number : %d \n", (char*) arg, number);
        sem_post(&bin_sem);
    }
}

void * thread_rcv(void* arg)
{
    int i;
    for(i = 0 ; i < 2; i++)
    {
        sem_wait(&bin_sem);
        number--;
        printf("execution : %s number : %d \n", (char*)arg, number);
    }
}
inherithandle
  • 2,614
  • 4
  • 31
  • 53

1 Answers1

0

On Mac OS X (10.6.8) there is no sem_init() and sem_destroy().

Use sem_open() and sem_unlink() instead.

/*

cat semaphore_test.c

source:
"Why semaphore object is not initialized?", 
https://stackoverflow.com/questions/13834367/why-semaphore-object-is-not-initialized

compiled on Mac OS X 10.6.8 with:
gcc -ansi -pedantic -std=gnu99 -Os -Wall -Wextra -Wshadow -Wpointer-arith -Wcast-qual  -Wstrict-prototypes \
    -Wmissing-prototypes -Wformat=2 -Wreturn-type -Wunreachable-code -finline  -l pthread -o semaphore_test semaphore_test.c

./semaphore_test

*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>

void * thread_snd(void *arg);
void * thread_rcv(void* arg);

//sem_t bin_sem;
static sem_t *bin_sem;
static const char *semname = "Semaphore";
static int number = 0;

char thread1[] = "A thread";
char thread2[] = "B thread";
char thread3[] = "C thread";

int main(void)
{
    pthread_t t1, t2 ,t3;
    void *thread_result;
    int state;

/*
    state  =  sem_init(&bin_sem, 0, 0);
    if(state != 0)
    {
        puts("fail to initialize semaphore");
        exit(1);
    }
*/

    bin_sem = sem_open(semname, O_CREAT, 0777, 0);
    if (bin_sem == SEM_FAILED)
    {
        fprintf(stderr, "%s\n", "ERROR creating semaphore semname");
        exit(EXIT_FAILURE);
    }

    pthread_create(&t1, NULL, thread_snd, &thread1);
    pthread_create(&t2, NULL, thread_rcv, &thread2);
    pthread_create(&t3, NULL, thread_rcv, &thread3);

    pthread_join(t1, &thread_result);
    pthread_join(t2, &thread_result);
    pthread_join(t3, &thread_result);

    printf("final number : %d \n", number);
    //sem_destroy(&bin_sem);
    sem_unlink(semname);
    return 0;
}

void * thread_snd(void * arg)
{
    int i;
    for(i = 0 ; i < 4; i++)
    {
        while(number != 0)
            sleep(1);
        number++;
        printf("snd execution : %s, number : %d \n", (char*) arg, number);
        //sem_post(&bin_sem);
        sem_post(bin_sem);
    }
}

void * thread_rcv(void* arg)
{
    int i;
    for(i = 0 ; i < 2; i++)
    {
        //sem_wait(&bin_sem);
        sem_wait(bin_sem);
        number--;
        printf("rcv execution : %s number : %d \n", (char*)arg, number);
    }
}

See also:

Community
  • 1
  • 1
chad
  • 61
  • 4