Reputation: 268
I'm not exactly sure the best way to go out about what I'm trying to do, so I thought I'd turn here for ideas.
I have N number of programs that all must communicate with one process, ProcA through a memory mapped file.
When I open any program in group N, it checks if ProcA has been opened, if not it launches it. Here is where the question of this comes in...
When ProcA is ready, I need to be able to communicate to the process that spawned it, that the mapped memory has been created and communication can begin.
I was thinking about using a Mutex to accomplish this. Have the spawning processing attempt to acquire and release a mutex in a loop, checking if it was the creator or not, until it gets a return that the mutex was created in other place. Even this though seems potentially problematic since as I said, N number of programs will be doing this at once and if multiple are spinning, acquiring and releasing like this, they'll see each other locking and think it's ProcA instead.
So, what's the best way for N number of processeses blocking until ProcA signals that it's open for business?
Thanks!
Edit
For further clarification, I've tried having the process that spawns ProcA create the memory map which ProcA can then take over. But I found I have the same problem, because the spawning process needs to know when to release the shared memory, if it does it before ProcA grabs it then the memory map is released.
Edit 2
I need to pass pointer data around between the two processes, thus memory mapped files are my only options, pipes, sockets, etc won't work for this.
Upvotes: 0
Views: 194
Reputation: 3960
I think what you should really look into IPC with WCF (it's an old article, but should give you the basics), this would be far better than your current approach and signalling each other with the Mutex.
But if you insist on using this approach, you can create a system wide (Global
), or session wide (named) Mutex, and simply do WaitOne/ReleaseMutex. No need for looping, and you should try to stay away from this (looping) approach as you'll be unnecessarily wasting CPU cycles, but don't think there's much to it unless I'm missing something.
Upvotes: 1