Detect a stale mutex file for inter-process synch

Detect a stale mutex file for inter-process synch

Post by qlu » Sun, 16 Nov 2003 01:10:23


Hi, there,

In one of my projects, I need to create a mutex in shared memory using
memory-mapped I/O in order to achieve inter-process synchronization
for updating a data file; the mutex info is actually stored in a disk
file (mutex file), which is mapped to the address space of each
process (see Chapter10.15 of W. Richard Stevens' book "Unix Network
Programming", v2, Second Edition).

The problem is, if I killed a process in the middle, it would leave a
stale mutex file in hard disk (without unlocking it?). Then, the next
process will hang there forever when it tries to lock this mutex file
because it thinks another process (the one I just killed) is still
locking it.

Is there any way to detect if a mutex file is already dead or not? Or,
can I set a maximum waiting time for a process to wait for locking a
mutex file?

Thanks in advance for your help.

Best Regards,
Lu
 
 
 

Detect a stale mutex file for inter-process synch

Post by ptjm » Sun, 16 Nov 2003 04:34:41

In article < XXXX@XXXXX.COM >,


% In one of my projects, I need to create a mutex in shared memory using
% memory-mapped I/O in order to achieve inter-process synchronization
% for updating a data file;

[...]

% The problem is, if I killed a process in the middle, it would leave a
% stale mutex file in hard disk (without unlocking it?).

That's one problem. Another is that, if you kill a process in the middle,
the data file will be corrupt.

If you want to handle this situation, you need to have a clean-up
strategy. This will include detecting that the process modifying
the data file is gone, detecting wither the file is in an inconsistent
state, rolling back the incomplete changes, destroying the old mutex
and creating a new one.
--

Patrick TJ McPhee
East York Canada
XXXX@XXXXX.COM

 
 
 

Detect a stale mutex file for inter-process synch

Post by Uday Kiran » Sun, 16 Nov 2003 13:37:36


You may also use NO_OWNER mutexes, so that you can unlock the mutex
if you see some inconsistancy and found that the mutex is in locked state.
 
 
 

Detect a stale mutex file for inter-process synch

Post by ptjm » Tue, 18 Nov 2003 05:17:30

In article < XXXX@XXXXX.COM >,


%
% > In article < XXXX@XXXXX.COM >,

% >
% > % In one of my projects, I need to create a mutex in shared memory using
% > % memory-mapped I/O in order to achieve inter-process synchronization
% > % for updating a data file;
% >
% > [...]
% >
% > % The problem is, if I killed a process in the middle, it would leave a
% > % stale mutex file in hard disk (without unlocking it?).
% >
% > That's one problem. Another is that, if you kill a process in the middle,
% > the data file will be corrupt.
% >
% > If you want to handle this situation, you need to have a clean-up
% > strategy. This will include detecting that the process modifying
% > the data file is gone, detecting wither the file is in an inconsistent
% > state, rolling back the incomplete changes, destroying the old mutex
% > and creating a new one.

% You may also use NO_OWNER mutexes, so that you can unlock the mutex
% if you see some inconsistancy and found that the mutex is in locked state.

OK, rather than using the standard mutexes, you could use some other
kind, say process-shared semaphores. In that case, rather than detecting
that the process modifying the data file is gone, detecting whether the
file is in an inconsistent state, rolling back the incomplete changes,
destroying the old mutex and creating the new one, you can detect that
the process modifying the data file is gone, determine whether the file
is in an inconsistent state, roll back the incomplete changes, and go up
on the the semaphore.

If this seems simpler to you or it makes you happier in some way, go for
it. My experience is that semaphores tend to be slower than mutexes on
systems which allow both to be shared between processes, but do your
locking however you like. It doesn't change the core part of the
problem, which has nothing to do with restoring the mutex. Restoring the
mutex is trivial.
--

Patrick TJ McPhee
East York Canada
XXXX@XXXXX.COM
 
 
 

Detect a stale mutex file for inter-process synch

Post by crowben » Thu, 27 Nov 2003 04:43:44


What are you storing in the mutex file? Perhaps you could store the
PID of the process that currently has the lock on the mutex. Then if
a process-2 attempts to lock the mutex and finds it locked it can
check to see if process-1 (the locking process) is still running. If
the process that owns the lock is not running the other process can
assume the mutex is stale and take control.

Greg