简体   繁体   中英

How to wait for a cloned child process of an invoked process to exit?

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.

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/wp/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...

如果您可以控制调用进程的来源,一种可能的解决方案是让它等待它通过克隆自己产生的进程。

+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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM