RE: Mutex Problem



1. IF you know that one application is the master (and it most work before
the slave can) then yes, you can use CreateMutex in the master and OpenMutex
in the slave. Note, however, that if the slave app starts earlier, then it
would not get the mutex. (Since nothing to process anyway, one option is to
periodically re-try opening the mutex.)

2. Yes, good programming practice requires that each and every application
releases all resources it acquires. And that includes closing ALL handles the
app acquired.

3. On the other hand, Windows should automatically close all handles anyway
when an app terminates. (But are you sure your app fully terminates and the
slave app doesn't sometimes has another - maybe non-functional, but not
temrinated thread running?) You can check wth Task Manager if the slave app
terminated.

4. Note, that Windows may delay closing handles if it thinks it is faster to
keep a resorce open because maybe the same app will use it again. (Although I
don't know how this interferes with the app-termination cleanup part of the
system.)

Laszlo Elteto
SafeNet, Inc.

"עפר" wrote:

Hi All,

I have 2 applications that I need to synchronize their mutual access of some
shared memory, and for this reason each of these applications tries to create
an unowned named-mutex upon its startup:

hMutex = CreateMutex (NULL, false, szMutexName);

From this moment and on, any of them gets hold of the mutex before accessing
the shared memory, and releases it immediately afterwards:

if (WAIT_OBJECT_0 != WaitForSingleObject (hMutex, 1000))
{
return (-1);
}

<access the shared memory>...

ReleaseMutex (hMutex);

NOTE: NONE of the applications call CloseHandle (hMutex)!

To be more accurate, one of them acts as a master (that is, makes both
read/write operations), and the other as a slave (that is, may only read the
shared memory).

Now, everything works fine all along, no matter what's the first application
to run (and create the mutex), and what's the second.

BUT when the slave ends running and closes, the master application only
partially succeeds getting hold of the mutex (that is, sometimes it get hold
of the mutex, sometimes not, in a seemingly repeating way), and therefore
cannot properly funtion.

1) Why is it so, and how can I solve it?

2) Should the slave application close the mutex somehow upon closing?

3) Should I use OpnetMutex() instead of CreateMutex()?

Thanks for your help,
Ofer.


.



Relevant Pages

  • Re: Mutex Problem
    ... the slave can) then yes, you can use CreateMutex in the master and OpenMutex ... Note, however, that if the slave app starts earlier, then it ... would not get the mutex. ... shared memory, and for this reason each of these applications tries to create ...
    (microsoft.public.platformsdk.security)
  • Library for distributed systems
    ... I'm looking for a good Java library for doing a distributed app. ... * A master machine must be able to dispatch asynchronous messages to slave ... the library must notify the app on the master ... If the master gets no response from a slave, ...
    (comp.lang.java.programmer)
  • Re: Library for distributed systems
    ... I think that JMS (Java Messaging Service) is closest to what you desire. ... > I'm looking for a good Java library for doing a distributed app. ... > * A master machine must be able to dispatch asynchronous messages to slave ...
    (comp.lang.java.programmer)
  • Re: timing problem with pthread_mutex_unlock and lock
    ... but in my program the mutex slave is already locked. ... i divide them into T-1 slaves and 1 master. ... lock master, ...
    (comp.programming.threads)
  • Re: Replikations Frage
    ... vom Master auf den Slave kopiert hast und dabei die Permissions auf ... Die Datenbanken auf den Slave Server laufen ohne Probleme. ... # If you want to know which options a program support, ...
    (de.comp.datenbanken.mysql)