I have an XMLRPC windows service in c++ . I need to child launch processes from it , if windows service received a request and process already exist I need to end that particular child process and launch it again . I dont need to wait for process to exit after launching but once its started and starts what its supposed to do I need to notify main process . When the child process ends it needs to notify service that launched it so that I can maintain a list of what processes already exist . I know I can do this with CreateProcess but how do I handle aspects of notifying once particular thing is reached n child process .
2 Answers
After calling CreateProcess
, keep track of the process handle. You can use that to see if the process has exited or not, and call TerminateProcess
if necessary.
You can even keep all the handles in an array, and then a single call to WaitForMultipleObjects
will let you know as soon as any one of the child processes has exited.
If the child processes need to notify the main process of something then you can use one of the interprocess communications mechanisms. Of course this assumes that you are writing the code for the child processes. If all you need to know is when a child process starts and ends then, as stated in a previous answer, you can use CreateProcess and one of the wait functions. You can stop a child process with TerminateProcess but this is not a graceful way to stop a process (you might stop it in the middle of something important), a more gentle way to stopping the child process is to notify the child process that you want it to stop (using an interprocess communications mechanism) and then waiting for it to stop itself. Oh, I forgot to explain, you can wait for a process to end by passing the handle to the process to one of the wait functions.