无法在使用者线程中使用JNI的生产者-使用者程序中捕获SIGINT信号 [英] Cannot catch SIGINT signal in a producer-consumer program where JNI is used in consumer thread

查看:96
本文介绍了无法在使用者线程中使用JNI的生产者-使用者程序中捕获SIGINT信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个解决生产者-消费者问题的程序.

I am writing a program for a producer-consumer problem.

生产者产生数据并将数据推送到boost::spsc_queue中,然后消费者处理它.

Producer produces data and pushes data into boost::spsc_queue and the consumer processes it.

在使用者线程中,我正在使用JNI从用c ++编写的代码中调用某些Java函数.

In consumer thread, I am using JNI to call some java functions from my code written in c++.

我是使用者线程调用的函数本身中的initializing and creating JVM,然后事件循环开始,在该事件循环中,它从boost::spsc_queue弹出数据并进行处理.

I am initializing and creating JVM in the function itself called by the consumer thread and then the event loop starts in which it pops data from boost::spsc_queue and process it.

现在,我想捕获SIGINT信号,因此我编写了signal_handler并将其注册到我的main()函数中.但这不起作用.

Now, I want to catch SIGINT signal, so I have written a signal_handler and register it in my main() function. But it is not working.

如果我注释掉所有JNI内容,然后在使用者线程调用的函数中在那里开始循环while(1){},则它正在捕获SIGINT并按预期方式工作.

If I comment out all JNI stuff and just start a loop while(1){} there in the function called by consumer thread, then it is catching SIGINT and working as it is supposed to work.

我还有更多需要照顾JVM或JNI的东西吗? 在主线程中初始化并创建JVM之后,我是否应该尝试相同的方法?有道理吗?

Is something more which I need to take care for JVM or JNI stuff? Shall I try the same thing after initializing and creating JVM in the main thread? Does it make sense?

推荐答案

似乎,您需要

It seems, you need the -Xrs option

-Xrs

减少JVM对操作系统信号的使用.

-Xrs

Reduces the use of operating system signals by the JVM.

...

嵌入JVM的应用程序经常需要捕获诸如SIGINTSIGTERM之类的信号,这可能导致对JVM信号处理程序的干扰. -Xrs选项可用于解决此问题.使用-Xrs时,JVM不会更改SIGINTSIGTERMSIGHUPSIGQUIT的信号掩码,并且不会安装这些信号的信号处理程序.

Applications embedding the JVM frequently need to trap signals such as SIGINT or SIGTERM, 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 for SIGINT, SIGTERM, SIGHUP, and SIGQUIT are not changed by the JVM, and signal handlers for these signals are not installed.

指定-Xrs有两个结果:

  • SIGQUIT线程转储不可用.

  • SIGQUIT thread dumps are not available.

用户代码负责导致关闭挂机运行,例如,在终止JVM时调用System.exit().

User code is responsible for causing shutdown hooks to run, for example, by calling System.exit() when the JVM is to be terminated.

您可以在JavaVMInitArgs中指定此类选项,并将其传递给 JNI_CreateJavaVM(…) .

You can specify such options in the JavaVMInitArgs which you pass to JNI_CreateJavaVM(…).

这篇关于无法在使用者线程中使用JNI的生产者-使用者程序中捕获SIGINT信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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