Is there a way to call CreateProcess such that killing the parent process automatically kills the child process?
Perhaps using Create Process Flags?
Edit
The solution is to create a job object, place both parent and child in the job object. Whent he parent is killed the child is killed. I got the code from here:
Kill child process when parent process is killed
Take note of @wilx's comment about inherited handles.
Orphan Processes When a parent process dies before a child process, the kernel knows that it's not going to get a wait call, so instead it makes these processes "orphans" and puts them under the care of init (remember mother of all processes).
In chrome on Windows, the child processes are in a job object and so the OS takes care of killing them when the parent process dies.
The SIGKILL signal can be sent with a kill system call. Note though, the SIGTERM handler registered in the following code sample can't catch the delivered SIGKILL , and it immediately kills the given child process.
1 Answer. Show activity on this post. If you are on the same terminal with the process, type ctrl-z to stop the parent, and the use ps -ef to find the PID of the php child. Use the kill lines above to effectively separate the child from the parent.
Using jobs as Neil says is IMHO the best way. You can make the child processes get killed when the job owning process dies by setting JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
on the job object using SetInformationJobObject()
. The job object handle will be closed when your parent process exits/dies. For this to work it is essential that the job handle is not inherited by the child processes. If you want to track also the grand-child processes then you will have to create your child processes suspended, add them to your job object and only then let them run.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With