从 JVM 发送 POSIX 信号 [英] Sending a POSIX signal from the JVM

查看:34
本文介绍了从 JVM 发送 POSIX 信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 JVM 内部发送 POSIX 信号?(在 Java 或 Clojure 中)

How do I send a POSIX signal from within the JVM? (in Java or Clojure)

我从没想过这会成为一个问题,直到我尝试使用谷歌搜索它——有很多关于处理信号的信息,但没有关于发送它们的信息.

I never thought this would be an issue until I tried googling it — there is lots of information about handling signals, but nothing about sending them.

除了使用 JNI 或调用 shell 执行kill"之外,还有其他方法可以向 PID 发送信号吗?

Short of using the JNI or calling the shell to execute "kill", is there any other way to send a signal to a PID?

推荐答案

好的.回答我自己:我查看了建议的库,但我担心在大量代码上引入新的依赖项,尤其是如果我只使用其中的一小部分.

Ok. Answering myself: I looked at the suggested libraries, but I am wary of introducing new dependencies on lots of code, especially if I'll only use a small part of it.

事实证明,最简单的方法是使用 JNA 并与系统 (libc) API 接口.在 clojure 中,这几乎相当于:

It turns out the easiest way is to use JNA and interface with the system (libc) API. In clojure this pretty much amounts to:

(jna-invoke Integer kill pid signo)

(jna-invoke Integer kill pid signo)

当然是在做了一个 (:use net.n01se.clojure-jna) 之后.

after doing a (:use net.n01se.clojure-jna) of course.

由于该软件不打算在 Windows 上运行,所以我对这个解决方案很满意,它应该适用于所有 POSIX 系统(我对 MacOS 和 Linux 感兴趣).

Since this software is not intended to ever run on Windows, I'm fine with this solution which should work on all POSIX systems (MacOS and Linux are what I'm interested in).

这篇关于从 JVM 发送 POSIX 信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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