Java - Process.destroy() source code for Linux

java, kill, linux, process

Solution

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

Problem

I need to check to code of `Process.destroy()` to see how exactly it `kill`s 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,

Original source