Java - Linux 的 Process.destroy() 源代码 [英] Java - Process.destroy() source code for Linux

查看:16
本文介绍了Java - Linux 的 Process.destroy() 源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查 Process.destroy() 的代码,看看它是如何kill 成为 Linux 上的子进程的.

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

有谁知道这个方法是做什么的或者有它的来源链接?我检查了 jdk 源码,Process 只是一个抽象类,destroy 方法还没有实现,似乎没有任何链接extendsimplements Process 的子类.任何帮助将不胜感激.

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.

谢谢,

推荐答案

进程管理等所有操作都由操作系统完成.因此,JVM 必须调用适当的系统调用来销毁进程.显然,这会因操作系统而异.

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.

在 Linux 上,我们有 kill 系统调用来执行此操作 - 或者 exit 如果我们想终止当前正在运行的进程.当然,JDK 源代码中的本地方法根据 JVM 将运行的操作系统分开.如前所述,Process 有一个 public void destroy() 方法.在Linux的情况下,该方法由UNIXProcess.destroy() 方法的实现大致如下:

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);
}

本地方法 destroyProcess() 依次定义在 UNIXProcess_md.c 看起来像这样:

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);
}

其中 kill 是 Linux 系统调用,其源代码可在 Linux 内核中找到,更准确地说,在文件 kernel/signal.c 中.声明为SYSCALL_DEFINE2(kill, pid_t, pid, int, sig).

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).

阅读愉快!:)

这篇关于Java - Linux 的 Process.destroy() 源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆