Reputation:
With reference to the semaphore implementation of semaphore.h in Linux, does an uninitialized semaphore sem_t default to 0?
I just discovered that I had forgotten to initialize my semaphores to 0. Even so, the program worked fine without sem_init.
(To all the purists, I completely agree that doing so is a bad practice.)
Upvotes: 1
Views: 2220
Reputation: 363807
This depends on the compiler, rather than the operating system/semaphore implementation. Bart's answer covers the C standard in this case.
However, according to the Unix standard, you shouldn't even rely on a sem_t
being 0, or even a sem_t *
being NULL; using sem_init
on an unitialized variable should be safe. The only special value specified for semaphores is the sem_t *
value SEM_FAILED
and its value is implementation-specific.
Upvotes: 1
Reputation: 15768
It will depend on how/where the semaphore is defined.
static
.static
block-scope variable, then it will start out with whatever value has been previously stored in that memory location.Upvotes: 3
Reputation: 12044
I believe it is officially 'undefined'. That is, it could be anything. In your case, for some reason, it happens to be coming up 0, but that's just luck. Tomorrow it may be 5.
Upvotes: 3