简体   繁体   中英

How to send events/signal from C to Java in JNI

Is there any mechanism to send asynchronous events/signals through JNI from C to Java or viceversa? I need to catch a signal/event from C to Java.

It sounds a bit tricky, as JNI is supposed to be one-way only: Java code can invoke a native method, which of course may call back into Java code, but can't itself initiate the process.

But it isn't impossible, I think the most straightforward solution is to have a native call that blocks until the signal is received.

Another option would be to write a JVMTI agent, which can interfere with a running VM actively.

But if we're talking about UNIX signals, you can handle then with the not public API (therefore not guaranteed in the future and Sun VM specific) but existing sun.misc.Signal class.

You can call a Java method from C code. See the JNI documentation. See also http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM