gpt4 book ai didi

java - 如果从 Java 启动,子进程将忽略 SIGQUIT

转载 作者:可可西里 更新时间:2023-11-01 11:52:01 25 4
gpt4 key购买 nike

举个简单的例子:

public class Main
{
public static void main(String[] args) throws Exception
{
Runtime.getRuntime().exec("sleep 1000");

// This is just so that the JVM does not exit
Thread.sleep(1000 * 1000);
}
}

我在 Linux 上使用 openjdk6 运行它。如果我尝试向“ sleep ”进程发送 SIGQUIT 信号,它会忽略它。其他信号(SIGINTSIGTERM 等)都可以正常工作,但 SIGQUIT 会被忽略。如果我只是在不使用 Java 的情况下从 shell 运行 sleep 1000,那么 SIGQUIT 会得到正确处理。

为什么行为不同?

最佳答案

-Xrs 选项传递给 java。我已经在有和没有选项的情况下测试了你的程序。如果没有该选项,SIGQUIT 将被阻塞;有了它,信号不会被阻塞,当我向它发送 SIGQUIT 信号时, sleep 过程会终止。

关于正在发生的事情,我能找到的最接近的解释是在 OpenJDK 6 java(1) 中。手册页:

Sun's JVM catches signals to implement shutdown hooks for abnormal JVM termination. The JVM uses SIGHUP, SIGINT, and SIGTERM to initiate the running of shutdown hooks.

The JVM uses a similar mechanism to implement the pre-1.2 feature of dumping thread stacks for debugging purposes. Sun's JVM uses SIGQUIT to perform thread dumps.

Applications embedding the JVM frequently need to trap signals like SIGINT or SIGTERM, which can lead to interference with the JVM's own signal handlers. The -Xrs command-line option is available to address this issue. When -Xrs is used on Sun's JVM, the signal masks for SIGINT, SIGTERM, SIGHUP, and SIGQUIT are not changed by the JVM, and signal handlers for these signals are not installed.

关于java - 如果从 Java 启动,子进程将忽略 SIGQUIT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42980744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com