This program works (I tested it), even though the semaphore is not in shared memory. Note how I create the variable once - before the fork().
On the other hand, a semaphore created with sem_init()
needs to be in shared memory to work. But it's still a sem_t
structure, so why doesn't it require shared memory?
Are the contents of the sem_t
structure somehow different?
sem_t *s = sem_open("mysemaphore1", O_CREAT, 0600, 0);
if (fork()) {
sleep(3);
sem_post(s);
} else {
sem_wait(s);
printf("Woke\n");
}