I’ve done a bit of digging, and the closest I’ve come to direct answer to my question is here.
I’ve created a mutex then spin into a loop where I listen for activity on a TCP/IP port, fork, respond, then exit from the child. Part of the ‘respond’ requires that the child process access hardware autonomously, that is protected by a mutex.
If I initialize a mutex before creating child processes, will all the potentially concurrently running children honor the mutex?
Not by default.
You have to store the mutex in a shared memory block. You can get one with SysV IPC or with the
MAP_SHAREDflag tommap().You also have to set the attributes on the mutex properly. You need to make a mutex attribute with
pthread_mutexattr_init()and callpthread_mutexattr_setpshared(), then pass that attribute topthread_mutex_init().If you do not use shared memory, the child processes will not be able to see the mutex state because each will be using a private copy. If you do not set the pshared attribute the mutex will probably crash the program because it will not expect other processes to be changing the data.