6

I am writing a Java application that talks to a C++ application using named pipes. When the C++ application dies, the Java gets SIGPIPE and the Java application dies.

On C/C++ I know how to catch that signal and ignore it. Is it possible to do something similar on Android/Java?

elcuco
  • 8,948
  • 9
  • 47
  • 69
  • 1
    Maybe, it's good not to fight with the rules of the game: if your java app is tied to C++ one and there's no more connection, just tear down your java app and restart it. This link may be useful: http://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java - how to set a shutdown hook. – Dmytro Sirenko Oct 06 '12 at 17:22
  • This **is possible** see for example http://stackoverflow.com/questions/1083154/how-can-i-catch-sigsegv-segmentation-fault-and-get-a-stack-trace-under-jni-on – Chris Stratton Mar 17 '14 at 16:34
  • @ChrisStratton seems interesting. Thanks! – elcuco Mar 18 '14 at 09:15

1 Answers1

1

It seems this is not really possible. The best solution available would be to add a shut down hook and "gracefully" restart the application, as described here:

EDIT:

I needed this because (back then) I had JNI code on which I opened a FD and r/w to it. The FD was opened to a named socket (a Unix pipe), and when the remote side of the pipe (in my case a daemon, or Android C/service) closes the connection (or dies), your side will get a signal. This is not always nice. IMHO the best to handle this should be using a signal handler from good old plain-C code, and just swallow it (otherwise - you should notify the Java side of your app).

http://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
elcuco
  • 8,948
  • 9
  • 47
  • 69
  • 1
    This answer cites a jvm-based explanation, which is not necessarily applicable to Android, as while Android uses code compiled and translated form Java, it does not actually run a Java virtual machine, but rather its own Dalvik (or more recently, Android Runtime) machine, which has somewhat different behavior. Further, it is quite likely that - at least with the NDK - one can replace any signal handler in one's own DVM process. – Chris Stratton Mar 17 '14 at 16:25
  • I did not say it would not work, rather that the link is not fully applicable and that there seem to be ways in which it could work. – Chris Stratton Mar 18 '14 at 12:48