tags:

views:

80

answers:

3

I have a program which needs to invoke a process to perform an operation and wait for it to complete the operation. The problem is that the invoked process clones itself and exits, which causes the wait api to return when the process exits. How can I wait for the cloned process to finish execution and return?

I am using the windows JOB object as mentioned in http://www.microsoft.com/msj/0399/jobkernelobj/jobkernelobj.aspx, But I am not sure if this is the best way.

A: 

If you have control over the source of invoked process, one possible solution would be to make it wait for the process it spawns by cloning itself.

pajton
I don't have the source, hence the question.
Naveen
Ok. Do you know the name of the executable the other process is executing? Maybe you could periodically check if a process of given name is still running? Or better, you could check processes with given parent pid - you know the pid of the invoked process, right?
pajton
A: 

umm, I'm pretty sure you can can the spawner process id from any process. I'd iterate through all the processes, find the one's who's parent id matches the one of the process you spawned, and wait for it to die.

alternatively (I mean, thats pretty hack) what is the child child process doing? is there some other way you could detect when it has finished doing what it is meant to do?

a hack way to get a process's parent id

http://www.codeguru.com/cpp/w-p/win32/article.php/c1437

takes a handle, and using the method in the code above, returns the parent id.

http://msdn.microsoft.com/en-us/library/ms684280(VS.85).aspx

OpenProcess takes an id, gets a handle to it (if you're lucky)

http://msdn.microsoft.com/en-us/library/ms684320(VS.85).aspx

GetProcessId takes a handle, gets it's id.

http://msdn.microsoft.com/en-us/library/ms683215(VS.85).aspx

GetExitCodeProcess takes a handle, returns whether the process is done or not.

http://msdn.microsoft.com/en-us/library/ms683189(VS.85).aspx

so appart from using hidden nt calls that it expressly tells you not to, you would basically have to create your process, get it's id, then spam all the process, opening them and checking their parent ids against the id of the process you created, if you didn't find one, then it's done, if you do, spam it with GetExitCodeProcess until its done.

I haven't tested any of this, but it looks like A way to do it. though if it's THE BEST way to do it I might just have to loose all faith in windows...

matt
A: 

+1 for using job objects ;)

Assuming the process that you're running isn't spawning the cloned version of itself in such a way that it breaks out of the job...

You should be able to simply monitor the job events and act on JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO (see JOBOBJECT_ASSOCIATE_COMPLETION_PORT and SetInformationJobObject()). Monitoring the job in this way will also give you notifications of the processId's of new processes created within the job and details of when they exit.

Len Holgate