Solved

How to determine which process created a named mutex?

Posted on 2002-04-16
9
540 Views
Last Modified: 2013-12-03
Is it possible to determine which process created a named mutex?

For example, let's say you have 5 applications (or perhaps 5 instances of the same app). Each one attempts to create a named mutex. Obviously only one can succeed. Is it possible to tell from another application which one of the 5 created the mutex? Is it possible to tell which (if any) has the mutex locked?

The number of apps/instances doesn't matter (just that it's more than one).
0
Comment
Question by:Melange
9 Comments
 
LVL 5

Accepted Solution

by:
robpitt earned 100 total points
ID: 6946226
If an application calls CreateMutex for a named mutex and that mutex already exists then a handle to the existing mutex is returned. This scenario can be detected by testing whether GetLastError()==ERROR_ALREADY_EXISTS

See the CreateMutex documentation for more info.

Does that help?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6946452
There is no way to do that. I checked the NT Native API, and even NT does not keep track of that (IOW: 'ZwQueryObject()' does not provide any info about the creator of an object).
0
 
LVL 10

Expert Comment

by:makerp
ID: 6947274
Easy, as robpit said if the mutes already exists then it returns ERROR_ALREADY_EXISTS. So if you don’t get this back then you are the first process to call CreateMutex with that name. So in your code if this is the case set a bool to true indicating you created it. if you do get back ERROR_ALREADY_EXISTS set the bool to false. now using some sort of process communication you could ask each process if they created the mutex. alternativley using sockets broadcast a 'Who created the mutex' message, the client who did could send back a message with their process-id in (they would not have to send their IP as you can get that from the Recv call in the sockaddr_in struct). if all prcesses are on the same machine then have the mutex creator to write a process-id file to a known location on disk...

there is one serious limintaion of my suggestion, that limitation being you must have the source code for the applications so you can code this in.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 5

Expert Comment

by:robpitt
ID: 6947458
Yeah I assumed that he realy didn't need to know exactly which other process created the mutex... if you need that then as JKR says you need an additional interprocess mechanism (e.g. shared memory to which you could write a process id - see CreateFileMapping for info)
0
 

Expert Comment

by:dpodvaln
ID: 6980785
makerp : you not necessarily need a source code to do it. You may inject your dll into all processes and override CreateMutex function with something as :

shared_processId = GetProcessId()
OriginalCreateMutex();
this technique described in the Richman's book.
0
 
LVL 3

Author Comment

by:Melange
ID: 6980951
Yeah, I was specifically looking for which process created a mutex as opposed to if it existed. That's easy with OpenMutex.

Unfortunately in the situation I was looking for neither interprocess communication or dll injection would work. I was looking at this as a diagnostic tool to see why some apps were hanging or causing problems (that naturally seem to occur only at customer sites).

Dll injection wouldn't work because the mutex would already exist by the time I got to take a look at it. Interprocess communication wouldn't work because the app might not be responsive.

At least I may have indirectly figured out what the hangups were. Seems it wasn't even related to the mutex after all. But, it's one of those situations where I won't know if I succeeded in solving it or not unless it just doesn't happen again.

I'm still interested to see if there is a way to get this kind of diagnostic information. But, if the OS doesn't save the creating process id anywhere, then I guess it's not possible.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question