Clopen
Clopen

Reputation: 37

Semaphore not initializing

I'm trying to initialize my semaphore to 1 using sem_open but I can't seem to get it to work.

 sem_t *Montague, *Capulet; // the two semaphores
 char sem_Montague[]= "jud_Montague"; // their names
 char sem_Capulet[]= "jud_Capulet"; // their names
 int semvalue;  // stores value returned by sem_getvalue()

 Montague = sem_open(sem_Montague,  O_CREAT, 0600, 1);
 // We specify that the semaphore should be created it it did
 // not exist already, prevent other users from accessing it
 // (0600) and set its initial value to one 

 if (Montague == SEM_FAILED) {
      perror("unable to create Montague semaphore");
      sem_unlink(sem_Montague);
      exit(1);
 } // if
 // Just to show that we can test the value of a semaphore

 sem_getvalue(Montague, &semvalue);
 printf("The initial value of sem_Montague is %d\n", semvalue);

When I run the code, the output is:

  The initial value of sem_Montague is 0

It should be 1 and I'm not sure why it's not initializing properly.

Upvotes: 0

Views: 373

Answers (1)

bodangly
bodangly

Reputation: 2624

I don't believe sem_getvalue is implemented for OS X. I think its just a stub.

Upvotes: 1

Related Questions