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! :)
kill(pid, SIGTERM)
. Why do you think you need to know? – Enteric