clone a JVM with POSIX fork through JNI, but child JVM will not exit

℡╲_俬逩灬. 提交于 2019-12-13 18:15:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!