Java - Process.destroy() source code for Linux

半腔热情 提交于 2019-11-28 13:20:18
Daniel Kamil Kozar

Process management and all the like operations are done by the OS. Therefore, the JVM has to call the appropriate system call in order to destroy a process. This will, obviously, vary between operating systems.

On Linux, we have the kill syscall to do that - or exit if we want to terminate the currently running process. The native methods in the JDK sources are, of course, separated according to the operating system the JVM is going to run on. As noted previously, Process has a public void destroy() method. In the case of Linux, this method is implemented by UNIXProcess. The destroy() method is implemented pretty much like this:

private static native void destroyProcess(int pid);
public void destroy() {
    destroyProcess(pid);
}

The native method destroyProcess(), in turn, is defined in UNIXProcess_md.c and looks like this:

JNIEXPORT void JNICALL
Java_java_lang_UNIXProcess_destroyProcess(JNIEnv *env, jobject junk, jint pid)
{
    kill(pid, SIGTERM);
}

Where kill is the Linux syscall, whose source is available in the Linux kernel, more precisely in the file kernel/signal.c. It is declared as SYSCALL_DEFINE2(kill, pid_t, pid, int, sig).

Happy reading! :)

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