0

I am trying to understand how inter-process communication works with pthread_mutex I've read a bunch of info on the topic but I still cannot wrap my head around it.

I understand that there seems to be an easier way using semaphores but I am not trying to use that but to learn how mmap and inter-process communication works in C.

Let's say I have the following code in "Process 1"

...
if (!initialized) {
    pthread_mutexattr_init(&attr);
    pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
    pthread_mutex_init(&mtx->mtx, &attr);
    // save to shared memory
} else {
    // get the mutex from shared memory
}
...

Then I start the a copy of the same process and want to check if the mutex is already initialized and in shared memory. How to:

  1. Initialize the mutex only in the first process and not try to initialize it in the next ones
  2. Allocate the memory with mmap.
  • 1
    What's the question? – Allan Wind Aug 10 '22 at 21:33
  • 3
    "Interprocess" - is between processes. "`pthread_*`" is for threads – Eugene Sh. Aug 10 '22 at 21:34
  • @EugeneSh. pthread_mutex_t allows to init it with the PTHREAD_PROCESS_SHARED attribure for inter process communication if you store it in shared memory yourself which is what I do not know how to do. – ProgramMeALife Aug 10 '22 at 21:39
  • @AllanWind 1. How to store the mutex in shared memory (I have no idea how mmap works) 2. How to check that if it is initialized in the second process. – ProgramMeALife Aug 10 '22 at 21:42
  • 3
    Check this out: https://stackoverflow.com/questions/42628949/using-pthread-mutex-shared-between-processes-correctly Using `phtread_mutex` between processes doesn't sound like a good idea. – Eugene Sh. Aug 10 '22 at 21:44
  • What problem are you trying to solve? – tadman Aug 10 '22 at 21:48
  • @tadman re-read the question I just updated it for more clarity. Basically I am trying to safely init a pthread_mutex only in one process and ignore the initialization in another. – ProgramMeALife Aug 10 '22 at 21:55
  • Isn't that something you should know when creating the process? Like only the parent should set up that mutex, the rest should know they're children. – tadman Aug 10 '22 at 22:04
  • @tadman It would be fine if one process started another but the processes are started independently. I've seen that some languages have something called "named mutex" to solve the problem but that is not the case in C. – ProgramMeALife Aug 10 '22 at 22:07
  • Are you trying to establish some kind of 'primary' process that controls the other? You can use atomics if you want to get low-level, but it's probably better to elect the primary through a more formal mechanism. – tadman Aug 10 '22 at 22:13
  • You can allocate the mutex in POSIX shared memory so unrelated processes can access it. – Shawn Aug 10 '22 at 23:48
  • 3
    Or just use a lock on a file instead of a shared mutex. Lot simpler to set up. – Shawn Aug 10 '22 at 23:49
  • In your example code `initialized` can be affected by either process, so would have to be protected by the mutex. See the problem? – stark Aug 11 '22 at 10:40

1 Answers1

0

Ended up using Shawn's solution and just used a lock on a file it is a whole lot easier to setup and works similarly on Windows so I can easily write cross-platform wrapper code if needed.