I want to use process-shared POSIX semaphore under UNIX/LINUX. In this FAQ (E.7), I've found that "Portable applications should test _POSIX_THREAD_PROCESS_SHARED before using this facility.". Manuals also say, that second parameter (pshared) for sem_init function should be zero, unless this feature is implemented, othrewise sem_init will return ENOSYS.
But that is only theory. I've tested it on my Debian Etch (kernel 2.6.8-2-686, gcc 4.0.2, libc 2.3.6) with this test-program:
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
sem_t mysem;
printf("sem_init: %d\n", sem_init(&mysem, 1, 10));
sem_destroy(&mysem);
#ifdef _POSIX_THREAD_PROCESS_SHARED
printf("It's implemented: %ld\n", sysconf(_POSIX_THREAD_PROCESS_SHARED));
#endif
}
Function sem_init returned zero (success, not ENOSYS), but "It's implemented..." text doesn't appear (after removing #ifdef directive, program will not even compile, because " error: '_POSIX_THREAD_PROCESS_SHARED' undeclared (first use in this function)").
I've googled out some page discussing this problem, but I do not understand its resolution: "We should not infer any errors beyond those in POSIX.". Also I've found three header files in my filesystem mentioning this constant:
- /usr/include/unistd.h (only comment)
/* Get values of POSIX options:
If these symbols are defined, the corresponding features are
always available. If not, they may be available sometimes.
The current values can be obtained with `sysconf'.
...
_POSIX_THREAD_PROCESS_SHARED Process-shared synchronization supported.
...
If any of these symbols is defined as -1, the corresponding option is not
true for any file. If any is defined as other than -1, the corresponding
option is true for all files. If a symbol is not defined at all, the value
for a specific file can be obtained from `pathconf' and `fpathconf'.
...
*/
#include <bits/posix_opt.h>
- /usr/include/bits/posix_opt.h
/* Thread process-shared synchronization is not supported. */
#define _POSIX_THREAD_PROCESS_SHARED -1
- /usr/include/nptl/bits/posix_opt.h
/* Thread process-shared synchronization is supported. */
#define _POSIX_THREAD_PROCESS_SHARED 200112L
In directory /usr/include/nptl are also some pthread.h, semaphore.h and thread_db.h header files.
Is my sem_init function incorrect and I allways should check for _POSIX_THREAD_PROCESS_SHARED constant, is it some (known) bug, or my installation is somehow "mischmasched"? Where is the truth?