Java - Process.destroy() source code for Linux
Asked Answered
A

1

16

I need to check to code of Process.destroy() to see how exactly it kills a subprocess on Linux.

Does anyone know what this method does or have a link to its source? I checked the jdk source and Process is just an abstract class and the destroy method has not been implemented, there seem to be no links to any subclass that extends or implements Process. Any help will be appreciated.

Thanks,

Abrego answered 17/5, 2012 at 5:30 Comment(1)
It almost certainly calls kill(pid, SIGTERM). Why do you think you need to know?Enteric
S
25

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! :)

Sproul answered 17/5, 2012 at 11:21 Comment(1)
Beware, in at least OpenJDK 7, the destroy method will also close stdin, stdout and stderr after calling destroyProcess.Stepfather

© 2022 - 2024 — McMap. All rights reserved.