<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>javajiggle.com &#187; JNI</title>
	<atom:link href="http://javajiggle.com/category/jni/feed/" rel="self" type="application/rss+xml" />
	<link>http://javajiggle.com</link>
	<description>Java Jiggle. See what's shaking.</description>
	<lastBuildDate>Sat, 14 Mar 2009 17:40:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>If JNI based application is crashing, check signal handling!</title>
		<link>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/</link>
		<comments>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/#comments</comments>
		<pubDate>Sun, 06 Jan 2008 05:03:48 +0000</pubDate>
		<dc:creator>javajiggle</dc:creator>
				<category><![CDATA[JNI]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/</guid>
		<description><![CDATA[
When designing an application that is made up of Java and Native code it is important to handle signals correctly.   What I&#8217;m trying to say is that JVM uses signals internally and if your application registers itself as a signal handler it will intercept signals intended for JVM.   As a result [...]]]></description>
			<content:encoded><![CDATA[<p><!-- !  		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } 	  	 --></p>
<p style="margin-bottom: 0in"><img src="https://duke.dev.java.net/images/web/Duke_Thread.gif" alt="" width="241" height="191" align="left" />When designing an application that is made up of Java and Native code it is important to handle signals correctly.   What I&#8217;m trying to say is that JVM uses signals internally and if your application registers itself as a signal handler it will intercept signals intended for JVM.   As a result JVM&#8217;s signal handler will not be invoked.   Eventually this will lead to incorrect behavior and most likely a JVM crash.</p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in">Don&#8217;t worry Sun provides an excellent <strong>signal chaining</strong> library that makes solving this problem very easy. Let&#8217;s elaborate on this a bit.</p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>First thing first, what are signals?</strong></p>
<p style="margin-bottom: 0in">First of all, lets make sure we are on the same page about signals.    As stated in wikipedia “A signal is a limited form of inter-process communication used in Unix, Unix-like, and other POSIX-compliant operating systems.”  See <a href="http://en.wikipedia.org/wiki/Signal_%28computing%29">http://en.wikipedia.org/wiki/Signal_%28computing%29</a></p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>Problem Description</strong></p>
<p style="margin-bottom: 0in">Solaris and Linux JVMs use signals internally.   This means that the JVM will originate signals and it will need to handle them.   The problem arises when native code registers itself as a signal handler following JVMs initialization.   Since there can be only one signal handler for a particular signal, the native code signal handler will overwrite JVMs signal handler.   When this happens, every time JVM originates a signal, the signal will be intercepted by the native code signal handler.   As a result JVM signal handler will neverget invoked.   This can lead to the unexplained behavior that is very difficult to debug.</p>
<p style="margin-bottom: 0in"><img src="http://javajiggle.com/SCfiles/NormalJVM.png" alt="" /></p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>Example</strong></p>
<p style="margin-bottom: 0in">Check the attached sample code for example.   In this example, the C++ file creates a JVM and registers a signal handler immediately after.   This will override JVMs signal handler.   If you let this program run you will witness a JVM crash.   JVMs will originate signals, its signal handler will never get invoked and the problem will never get addressed.   Tested on Ubuntu linux with Java 1.6.</p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><a title="native c++ code" href="http://javajiggle.com/SCfiles/testjni.cpp.txt">C++ code</a></p>
<p style="margin-bottom: 0in"><a title="java code" href="http://javajiggle.com/SCfiles/jniProgressBar.java.txt">coresponding java code</a></p>
<p style="margin-bottom: 0in"><strong>To compile Java:</strong></p>
<p style="margin-bottom: 0in">javac *.java</p>
<p style="margin-bottom: 0in">jar cvf testJNI.jar *.class</p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>To compile and link native code:</strong></p>
<p style="margin-bottom: 0in; font-style: normal">g++ -c -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/ -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/linux -o ../bin/jnitest.o testjni.cpp</p>
<p style="margin-bottom: 0in; font-style: normal">g++ ../bin/jnitest.o -L /usr/lib/jvm/java-6-sun-1.6.0.03/jre/lib/i386/client</p>
<p style="margin-bottom: 0in; font-style: normal">-ljvm -o ../bin/jnitest.x</p>
<p style="margin-bottom: 0in">
<p><!--  		@page { size: 8.5in 11in; margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in"><strong>Solutions</strong></p>
<p style="margin-bottom: 0in">It turns out that the solution is incredibly simple.   In fact this problem can be solved three different ways:</p>
<ol>
<li>
<p style="margin-bottom: 0in">remove signal handlers from your 	native code.</p>
</li>
<li>
<p style="margin-bottom: 0in">recompile with signal chaining 	library.</p>
</li>
<li>
<p style="margin-bottom: 0in">set LD_PRELOAD env. variable prior 	to launching the program.</p>
</li>
</ol>
<p><img src="http://javajiggle.com/SCfiles/withljsig.png" alt="" width="500" height="375" /></p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>Signal Chaining</strong></p>
<p style="margin-bottom: 0in">Using signal chaining requires no code modification and is explained here:</p>
<p style="margin-bottom: 0in"><a href="http://java.sun.com/j2se/1.4.2/docs/guide/vm/signal-chaining.html">http://java.sun.com/j2se/1.4.2/docs/guide/vm/signal-chaining.html</a></p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in"><strong>How to fix Example above.</strong></p>
<p style="margin-bottom: 0in">Fortunately there is a fine signal chaining library provided for free by Sun<sup>TM</sup>.</p>
<p style="margin-bottom: 0in">Here is how I linked the attached sample code and enabled signal chaining.   The part in red is needed to enable the signal chaining, which solves the problem.     Java code does not need to be recompiled.</p>
<p style="margin-bottom: 0in">
<p style="margin-bottom: 0in; font-style: normal">g++ -c -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/ -I /usr/lib/jvm/java-6-sun-1.6.0.03/include/linux -o ../bin/jnitest.o testjni.cpp</p>
<p style="margin-bottom: 0in; font-style: normal"><span style="font-style: normal">g++ ../bin/jnitest.o -L /usr/lib/jvm/java-6-sun-1.6.0.03/jre/lib/i386/client</span><span style="color: #800000;"><strong>-ljsig</strong></span> -ljvm -o ../bin/jnitest.x</p>
<p style="margin-bottom: 0in; font-style: normal">
<p style="margin-bottom: 0in; font-style: normal">Any questions, let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://javajiggle.com/2008/01/06/if-jni-based-application-is-crashing-check-signal-handling/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
