I am on a project for which I have to spawn many different processes, all in different executables, and there will be some shared memory segments and some semaphores to synchronize them. I use an "initializer" process to open some POSIX semaphores and allocate some shared memory segments.

I can not decide which is the best practice; to initialize the semaphores and load them to shared memory so everyone that acceses the memory can access them too or to pass them as command line arguments during exec?

I do not find any option better than the other, except from that the first might be redundant because the semaphores will probably never change (ID's their values will certainly change a lot) and accessing them without needing them (for some processes) is also not useful, but I have seen it happen, probably for a reason.

So I will need some reasoning for each as well.


The best practice is to use the facilities that the platform offers you instead of rolling your own.

For semaphores across processes, you can use named semaphores (see the man page) and a convention for how to call each semaphore. Then you don't need a separate process to initialize the semaphores and somehow communicate them to the other processes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.