Hi all,
I'm not a Java programmer so please be gentle:
Prereq:
---------------
- The lookup service (see below) implemented by a C module cannot be
re-written in Java ;-)
- The lookup service is to be loaded into the process of the Java
application, i.e. I want to avoid expensive IPC
1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API that I guess I
would best access from a Java application by using JNI, right? Are
there any other choices than JNI?
2)
>From my understanding, Java being a pointerless language, Java client
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Does JNI in any way make this less
true? For example, are there mechanisms in JNI that uses physical
pointers for efficiency?
In C/C++, a rouge pointer can very easily damage the lookup service
and this would not be noticed until several days have passed. This is
not acceptable.
Thank you for your time
/Sune
Mike Baranczak - 09 Sep 2007 20:23 GMT
> Hi all,
>
[quoted text clipped - 12 lines]
> would best access from a Java application by using JNI, right? Are
> there any other choices than JNI?
With those prerequisites, no, I don't think there is.
> 2)
> >From my understanding, Java being a pointerless language, Java client
[quoted text clipped - 6 lines]
> and this would not be noticed until several days have passed. This is
> not acceptable.
An oversimplified view of how JNI works:
1. In some Java source file, you write a method stub with the "native"
modifier.
2. Run the javah tool, which creates a C header file containing a
function signature. If the native Java method has any arguments, those
will be passed to the C function, along with some data structures that
contain information about the Java runtime.
3. Implement this C function, creating a bridge between the JNI function
and whatever C library you want to use.
4. Your Java code can now call the native method - this call will be
forwarded to the C code you just wrote.
So there won't be any rogue pointers unless you introduce them in step 3.
Also keep in mind that a JNI call can be moderately expensive. Don't
just assume that it'll be faster than some IPC scheme.
> Thank you for your time
> /Sune

Signature
http://www.pocketgorilla.com - search engine for freelance programmers
Sune - 10 Sep 2007 02:47 GMT
> In article <1189350241.744901.165...@50g2000hsm.googlegroups.com>,
>
[quoted text clipped - 50 lines]
>
> --http://www.pocketgorilla.com- search engine for freelance programmers
Thanks Mike!
I knew JNI wasn't that efficient, but if you say it may be as bad as
IPC I get the chills...
Can Java in any way, without JNI, access write/read a shared memory
block created by a C module?
By shared memory block I mean what is acquired by shmem, shmat etc in
UNIX, LINUX...
BRs
/Sune
Arne Vajhøj - 10 Sep 2007 03:29 GMT
> Can Java in any way, without JNI, access write/read a shared memory
> block created by a C module?
>
> By shared memory block I mean what is acquired by shmem, shmat etc in
> UNIX, LINUX...
No.
Java is not the right language for something as platform
specific / close to the OS like this.
Arne
Arne Vajhøj - 10 Sep 2007 03:28 GMT
> Prereq:
> ---------------
> - The lookup service (see below) implemented by a C module cannot be
> re-written in Java ;-)
> - The lookup service is to be loaded into the process of the Java
> application, i.e. I want to avoid expensive IPC
You asked the same question in the C# group. Do you need the
solution in Java or C# or both ?
> 1)
> I got a C module that organizes a big block of memory into a fairly
> nice lookup service. This C module exposes a C API that I guess I
> would best access from a Java application by using JNI, right? Are
> there any other choices than JNI?
I believe JNI is the only solution.
> 2)
>>From my understanding, Java being a pointerless language, Java client
> code cannot by any means, by mistake, access the memory block managed
> by the C module. Is this correct? Does JNI in any way make this less
> true? For example, are there mechanisms in JNI that uses physical
> pointers for efficiency?
A Java reference is more or less a pointer.
But Java throws exceptions if you try and go over an arrays
boundaries.
So you can not overwrite memory by accident.
JNI code is C code and it can overwrite just any other C code.
Arne
Roedy Green - 10 Sep 2007 03:37 GMT
>1)
>I got a C module that organizes a big block of memory into a fairly
>nice lookup service. This C module exposes a C API that I guess I
>would best access from a Java application by using JNI, right? Are
>there any other choices than JNI?
The other choices, probably not suited in your case are:
1. execing a program and communicating with stdin/stdout.
2. talking to another program via TCP/IP.
>2)
>>From my understanding, Java being a pointerless language, Java client
>code cannot by any means, by mistake, access the memory block managed
>by the C module. Is this correct? Does JNI in any way make this less
>true? For example, are there mechanisms in JNI that uses physical
>pointers for efficiency?
Java has pointers. It calls them references. You can't do pointer
arithmetic in Java. However in C you can do what you please and glue
it to Java with JNI C calls. I suggest you read up on JNI to find out
what is possible. See http://mindprod.com/jgloss/jni.html
>In C/C++, a rogue pointer can very easily damage the lookup service
>and this would not be noticed until several days have passed. This is
>not acceptable.
Java can't generate rogue pointers. This is perhaps the main reason
for coding in Java over C. Of course you can generate them in your C
JNI glue.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com