问题
I'm trying to clone a running JVM with POSIX fork. The way I get access to fork is through JNI (i.e. https://github.com/kohsuke/akuma/blob/master/src/main/java/com/sun/akuma/CLibrary.java). After the fork, I'd like both the parent and the child to do some computation and then exit.
The following is the test code. After the fork, I can see two lines of "After the fork.", meaning that both the parent and the child reach this point. But then, the parent process exits normally while the child process doesn't.
Furthermore, I cannot kill the child process with signal 15 in the terminal. I have to use kill -9 to kill the child JVM process.
Any idea of what might be wrong?
import static com.sun.akuma.CLibrary.LIBC;
public class ForkTest {
public static void main(String[] args) {
int pid = LIBC.fork();
if (pid == 0) {
System.out.println("This is the child.");
} else {
System.out.println("This is the parent. child pid=" + pid);
}
System.out.println("After the fork.");
}
}
回答1:
@Awaken According to this article http://www.linuxprogrammingblog.com/threads-and-fork-think-twice-before-using-them, when you fork a multithreaded application only the thread that called fork() stays alive while other threads die. I don't think you can accomplish the snapshot in the child if you lose all threads.
来源:https://stackoverflow.com/questions/19735660/clone-a-jvm-with-posix-fork-through-jni-but-child-jvm-will-not-exit