The main function is in the C layer, and the virtual machine is started by calling the JNICreateJavaVM
function through C . The C code layer also has a function to capture the signal.
The original intention is that the C layer captures the signal of ctrl c, but I didn't expect it to be captured by the Java layer. I checked the data and it is the Java layer's own signal capture function. I would like to ask how to avoid letting the java layer capture the signal (let the C layer capture the signal), causing the entire process to exit?
CodePudding user response:
You need to specify the -Xrs
option on startup.
-Xrs
Reduces the use of operating system signals by the JVM. Shutdown hooks enable the orderly shutdown of a Java application by running user cleanup code (such as closing database connections) at shutdown, even if the JVM terminates abruptly.
Linux and macOS:
The JVM catches signals to implement shutdown hooks for unexpected termination. The JVM uses
SIGHUP
,SIGINT
, andSIGTERM
to initiate the running of shutdown hooks.Applications embedding the JVM frequently need to trap signals such as
SIGINT
orSIGTERM
, which can lead to interference with the JVM signal handlers. The-Xrs
option is available to address this issue. When-Xrs
is used, the signal masks forSIGINT
,SIGTERM
,SIGHUP
, andSIGQUIT
aren't changed by the JVM, and signal handlers for these signals aren't installed.Windows:
The JVM watches for console control events to implement shutdown hooks for unexpected termination. Specifically, the JVM registers a console control handler that begins shutdown-hook processing and returns
TRUE
forCTRL_C_EVENT
,CTRL_CLOSE_EVENT
,CTRL_LOGOFF_EVENT
, andCTRL_SHUTDOWN_EVENT
.The JVM uses a similar mechanism to implement the feature of dumping thread stacks for debugging purposes. The JVM uses
CTRL_BREAK_EVENT
to perform thread dumps.If the JVM is run as a service (for example, as a servlet engine for a web server), then it can receive
CTRL_LOGOFF_EVENT
but shouldn't initiate shutdown because the operating system doesn't actually terminate the process. To avoid possible interference such as this, the-Xrs
option can be used. When the-Xrs
option is used, the JVM doesn't install a console control handler, implying that it doesn't watch for or processCTRL_C_EVENT
,CTRL_CLOSE_EVENT
,CTRL_LOGOFF_EVENT
, orCTRL_SHUTDOWN_EVENT
.There are two consequences of specifying
-Xrs
:
Linux and macOS:
SIGQUIT
thread dumps aren't available.Windows: Ctrl Break thread dumps aren't available.
User code is responsible for causing shutdown hooks to run, for example, by calling the
System.exit()
when the JVM is to be terminated.
The CTRL C is handled by the SIGINT
signal (or obviously CTRL_C_EVENT
on Windows). Keep in mind that you’re also responsible for handling all the other specified signals once you use this option.
CodePudding user response:
Short of modifying the JVM itself, there's no simple way of doing this. One approach would be to install a Java shutdown hook which prevents the JVM from exiting. It can call into the C layer via JNI to indicate that it ctrl-c was detected, but there's no way to fully consume the signal. This is fine if the expectation is that the C layer needs to do some work before shutting down.