3

OS: Windows Language: C/C++

The design demands to use a mutex variable across process and its sub processes. If I create mutex in one process, I have to open the mutex in another processs to check the critical section's availablity. To open the mutex, I need to know the name of the mutex created in parent process. Suppose, If I keep the mutex as my application name. I could know the name of the mutex, as it is fixed. However, If I load the second instance of my application parallel, there would be a confusion.

Can the following be the better idea? I have an idea to name the mutex in the parent process as the process id. So now I need to fetch the Parent's process ID from the child process/grand child process to open the mutex. I guess there are no direct ways to fetch parent process id from the grand child process. So I have to pass on the process id in every create process api(in lpenvironment parm).

Can anyone suggest a simple method, as mutexes are most commonly used.... I am a newbie.

Muthukumar Palaniappan
  • 1,622
  • 5
  • 25
  • 49
  • Some resources : http://www.cs.cf.ac.uk/Dave/C/node31.html#SECTION003110000000000000000 , http://www.cfanatic.com/topic132/ – fsonmezay Mar 16 '11 at 14:41

3 Answers3

5

The main idea is fine, but you can maybe make some implementation tweaks.

For one, if your application involves multiple processes cooperating, then the main "controller" process which spawns sub-processes can easily pass its PID via a command line argument. If sub-processes spawn their own children as well, they can transfer the PID via the same mechanism.

Taking this idea further, you can also skip the PID entirely and pass the mutex name itself via command line argument to child processes. This approach has the advantage that the parent and child processes do not need to both include code that derives the mutex name from the PID. By passing the mutex name itself you decouple child processes from having to know how it is produced. This approach is used by many mainstream apps, e.g. Google Chrome.

And finally, you can maybe do better by adding a random string (a GUID maybe?) to the mutex name. I don't believe anyone will name their own global synchronization object with the same name, but some extra precautions won't hurt.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • 1
    Command-line argument or environment variable are both reasonable ways to transmit the unique mutex name. – Ben Voigt Mar 16 '11 at 15:04
0

As I understand it, you propose to use a process ID (PID) as the basis for naming a mutex to be used by your application and its subprocesses. This way, they will have their own mutex name that will not clash with the mutex name used by a second instance of your application.

This appears valid, but handles would be reliable than PIDs, because PIDs can get recycled. The method of using handles (passing them to child processes, similar to what you sugggest) is discussed on this StackOverflow thread.

I think passing the information you need to share to child processes is the way to go. Windows has the concepts for progress groups for a console process and its child processes, but this is really designed for being able to signal all the processes as a group -- not for sharing information among the group.

And there are also job objects for managing a group of processes that belong to a common job, but again, this is designed for managing a group of processes, not for information sharing between the processes in the group.

Community
  • 1
  • 1
Andrew Brown
  • 4,086
  • 1
  • 24
  • 21
0

If I interprete the wording "a process and its sub-processes" as well as "child/grandchild", the situation is that you have a single parent process that launches one or several children (or, children launching grandchildren). Or, any combination of these, but either way, every process we talk about using the same mutex that is created by the parent.

If that assumption is correct, why not just use something embarrassingly simple as:

#define MUTEXNAME "MzdhYTYzYzc3Mzk4ZDk1NDQ3MzI2MmUxYTAwNTdjMWU2MzJlZGE3Nw"

In case you wonder where this one came from, I generated it with this one-liner:

php -r "echo substr(base64_encode(sha1('some text')), 0, -2);"

Replace 'some text' with your name, the current date, or whatever random words are at your mind at this very moment. The chances that any other application on your system will ever have the same mutex name is practically zero.

Damon
  • 67,688
  • 20
  • 135
  • 185
  • I think the questioner wants to support the scenario where there are two parent processes. Each one would need to have its own mutex name that is unique to it, and is only known to its "descendant" processes. That's why he/she wants to generate a mutex name for the parent that is known/shared only with its descendants. – Andrew Brown Mar 16 '11 at 15:17
  • In that case, I would still create a random string (at runtime though), use that as name, and put it on the commandline. Just because it's as simple as it can get (simple is good!) and unlikely to ever fail. But sure, using the pid (plus a 'vendor' ID to make it unique in case someone else has the idea to use 4 digit integers) would work as well. – Damon Mar 16 '11 at 16:18