Madrugada
Madrugada

Reputation: 1289

Unexpected behavior of sem_open

I have the following code:

// DIE is macro i defined for catching errors

semc = sem_open(sem_name, O_CREAT, 0644, 1);    
DIE(semc == SEM_FAILED, "sem_open");

unsigned int val;
rc = sem_getvalue(semc, &val);
DIE(rc == -1, "sem_getvalue");
printf("sem is %d\n" , val);

Although I initialized the semaphore with 1, the value printed is 0 ... How can this be explained ?

Upvotes: 2

Views: 1836

Answers (1)

Ben Jackson
Ben Jackson

Reputation: 93720

Specifying O_CREAT doesn't force it to create, it only creates the semaphore if it does not already exist. Since you find that it doesn't take on your initialization value I would guess that sem_name already exists with value 0 at the time you call sem_open.

Upvotes: 2

Related Questions