It would be better if you could provide a minimal example of your code. So far the most obvious mistake that I can see is that your struct {/* .. */} shared
is not in actual shared memory.
Threads have access to the same private memory, processes have their own private memory, that's why it's called private.
fork()
creates cow copies of all memory that is not explicitly marked as shared. Allocate shared
in shared memory would be the first thing to do.
mmap/MAP_SHARED
is a good start for a program that creates new processes through fork
.
struct shared_s {/* .. */} * shared;
int main() {
shared = mmap(NULL, sizeof(struct shared_s)
, PROT_READ | PROT_WRITE, MAP_SHARED, -1, 0);
// ....
}
Also in case you are working on OS X you need to pass an additional flag MAP_HASSEMAPHORE
in order for your semaphores to work correctly.