Skip to content
Preptima
hardConceptDesignMidSeniorStaff

Two processes share a memory region and both write to it. What do you need besides the memory, and what happens if one of them dies while holding the lock?

Shared memory gives you bytes, not coordination. Process synchronisation needs a mutex inside the region, marked PTHREAD_PROCESS_SHARED and initialised once. If the holder dies, a plain mutex stays locked forever; a robust mutex returns EOWNERDEAD, but repairing half-written data is your job.

5 min readUpdated 2026-10-09

Answer

Short answer

Shared memory gives two processes the same bytes and nothing else. You also need a synchronisation primitive that works across processes and lives inside the region, typically a pthread mutex initialised with PTHREAD_PROCESS_SHARED, plus a rule for who initialises it. If the holder dies, an ordinary mutex stays locked and every other process blocks forever. A robust mutex instead gives the next locker EOWNERDEAD, meaning it owns the lock and must repair the data and call pthread_mutex_consistent before unlocking.

Was this answer helpful?

Likely follow-ups

  • The process holding the lock is not dead but stopped under a debugger. What does a robust mutex do then, and what would you add?
  • You want to drop the lock and use atomics in the shared region. What must be true of those atomics for this to work across processes?
  • Your shared region holds a linked list. Why do the pointers break, and what do you store instead?
  • The recovering process cannot tell what the dead one was halfway through. How would you lay out the writes so recovery is always possible?

Related questions

Related Topics