Why Does Killing Jvm Also Terminates Its Child Process If Waitfor Has Been Used

Why does killing JVM also terminates its child process if waitFor has been used?

There's nothing special about waitPid(), other than the fact that you keep the parent process in the foreground.


If you fork and then wait for the child to finish, you have a (simplified) process tree like this:

─┬= 1 init
└─┬= 2 bash --login
└─┬= 3 java code
└─── 4 bash child.sh

java is the foreground process in the terminal and the child is in its process group.

When you hit ^C the entire foreground process group gets terminated.1


If you don't wait, then at first your process tree is the same as the one above. The java process terminates and the child process becomes a child of the process at the root of the process tree.

─┬= 1 init
├──= 2 bash --login
└─── 4 bash child.sh

The child process finishes executing and terminates normally.


1The process group receives a SIGINT, for which the default action is to terminate. However, a different signal handler may be installed.

How can I cause a child process to exit when the parent does?

There is no tie between a child process and its parent. They may know each others process ID, but there's no hard connection between them. What you're talking about a orphan process. And it's an OS level concern. Meaning any solution is probably platform dependent.

About the only thing I can think of is to have the child check its parents status periodically, exiting if the parent's shutdown. I don't think this would be all that reliable though.

Can I have the JVM kill a started process when it exits?

You should call the .waitFor() method on each Process before exiting.

If you can't control that process as well as you'd like, you could use Runtime.addShutdownHook():

final Process process = startNewProcess();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
process.waitFor();
}
}));

I really don't recommend the latter solution if possible, though. If the JVM doesn't have a chance to clean itself up, then the process will still be running if it is forcibly killed.

Can I have the JVM kill a started process when it exits?

You should call the .waitFor() method on each Process before exiting.

If you can't control that process as well as you'd like, you could use Runtime.addShutdownHook():

final Process process = startNewProcess();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
process.waitFor();
}
}));

I really don't recommend the latter solution if possible, though. If the JVM doesn't have a chance to clean itself up, then the process will still be running if it is forcibly killed.

Lifetime of Runtime.exec process

A Java Process is created as a subprocess of the JVM. All operating systems that I know of kill subprocesses when the parent process terminates.

However, if the process you create forks its own processes, that are not its child processes (or are detached, e.g. in the case of Windows GUI applications), those may continue running after the JVM terminates. There is an issue possibly related to that described in this question.



Related Topics



Leave a reply



Submit