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:
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 tried my test-program also on 6.0-STABLE FreeBSD (where manpage says, that "A non-zero value for pshared specifies a shared semaphore that can be used by multiple processes, which this implementation is not capable of."). I've successfuly compiled it, but it's core-dumping when executed.
printf("sem_init: %d\n", sem_init(&mysem, 1, 10));
printf("It's implemented: %ld\n", sysconf(_POSIX_THREAD_PROCESS_SHARED));
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'.
/* Thread process-shared synchronization is not supported. */
#define _POSIX_THREAD_PROCESS_SHARED -1
In directory /usr/include/nptl are also some pthread.h, semaphore.h and thread_db.h header files.
/* Thread process-shared synchronization is supported. */
#define _POSIX_THREAD_PROCESS_SHARED 200112L
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?