I can't help with your windows specific question, but the following works on OS X. Probably the only thing you'll need to change, if anything, is the names of the header files.
Code:
#include <semaphore.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int
main(int argc, char **argv)
{
sem_t *sem;
const char *name;
name = (argc > 1) ? argv[1] : "foo";
sem = sem_open(name, O_CREAT, 0777, 1);
if (sem == (sem_t *)SEM_FAILED) {
perror(name);
exit(EXIT_FAILURE);
}
fprintf(stderr, "%d waits...", getpid());
if (sem_wait(sem) == -1) {
perror(name);
exit(EXIT_FAILURE);
}
fprintf(stderr, "Got %s\n", name);
sleep(5);
sem_post(sem);
return EXIT_SUCCESS;
}