Value of uninitialized semaphore
It will depend on how/where the semaphore is defined.
- If it is defined as (part of) a global variable, then the compiler must initialise it to 0 if you don't specify any value
- This is also true when it is (part of) a block-scope variable declared as
static
. - If it is (part of) a non-
static
block-scope variable, then it will start out with whatever value has been previously stored in that memory location.
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.