I have to synchronize N client processes with one server. These processes are forked by a main function in which I declared 3 semaphores. I decided to use POSIX semaphores but I don’t know how to share them between these processes. I thought that shared memory should work correctly, but I have some questions:
- How can I allocate the right space of memory in my segment?
- Can I use
sizeof(sem_t)insize_tfield ofshmgetin order to allocate exactly the space I need? - Does anyone have some examples similar to this situation?
It’s easy to share named
POSIXsemaphoresChoose a name for your semaphore
Use
sem_openwithO_CREATin the process that creates themOpen semaphores in the other processes
If you insist on using shared memory, it’s certainly possible.
I haven’t tested the above so it could be completely bonkers.