Mutex in shared memory when one user crashes?

后端 未结 5 2059
甜味超标
甜味超标 2020-12-29 08:05

Suppose that a process is creating a mutex in shared memory and locking it and dumps core while the mutex is locked.

Now in another process how do I detect that mute

5条回答
  •  没有蜡笔的小新
    2020-12-29 08:44

    I left this WRONG post undeleted only if someone will have the same idea and will find this discussion of use!


    You can use this approach. 1) Lock the POSIX shared mutex 2) Save the process-id in the shared memory. 3) Unlock the shared mutex 4) On correct exit clean the process-id

    If the process coredumps the next process will find that in the shared memory there is a process-id saved on step #2. If there is no process with this process-id in the OS then no one owns the shared mutex. So it's just necessary to replace the process-id.

    Update in order to answer the comment:

    Scenario 1: 1. P1 starts 2. P1 creates/opens a named mutex if it doesn't exists 3. P1 timed_locks the named mutex and successfuly does it (waits for 10 secs if necessary); 4. P1 coredumps 5. P2 starts after the coredump 6. P2 creates/opens a named mutex, it exists, it's OK 7. P2 timed_locks the named mutex and fails to lock (waits for 10 secs if necessary); 8. P2 remove the named mutex 9. P2 recreates a named mutex & lock it

提交回复
热议问题