태터데스크 관리자

도움말
닫기
적용하기   첫페이지 만들기

태터데스크 메시지

저장하였습니다.

자바에서 Shared Memory 사용

Posted 2008/09/17 12:41


댓글 하나가 운영자에겐 커다란 힘이 됩니다!

자바에서 shared memory 사용하기.

일반적으로는 JNI(Java Native Interface)를 이용하여 시스템 콜을 하여 사용한다. 하지만 이럴 경우 자바의 플랫폼 독립적이라는 최대의 장점 중 하나를 포기하는거와 같다.

때문에 다른 방법을 찾아보았다. 아래의 글은 Java forum 에서 발취한 내용이다.


New To Java Technology Archive - Shared Memory
http://forums.sun.com/thread.jspa?messageID=2699137

Taglia81
Posts:34
Registered: 3/16/04
Shared Memory  
2004. 9. 3 ?? 6:50

 
Is it possible under java to use Shared Memory like in c? (http://www.cs.cf.ac.uk/Dave/C/node27.html)
 
PeteKirkham
Posts:441
Registered: 2/25/04
Re: Shared Memory  
2004. 9. 3 ?? 7:12 (reply 1 of 14)  (In reply to original post )

 
Not in pure Java, but possible using JNI.
 
YoGee
Posts:4,653
Registered: 6/19/02
Re: Shared Memory  
2004. 9. 3 ?? 7:26 (reply 2 of 14)  (In reply to #1 )

 
I believe class data sharing (CDS) has been introduced in 1.5.
 
Taglia81
Posts:34
Registered: 3/16/04
Re: Shared Memory  
2004. 9. 3 ?? 7:53 (reply 3 of 14)  (In reply to #2 )

 
Class Data Sharing? (http://java.sun.com/j2se/1.5.0/docs/guide/vm/class-data-sharing.html)
It doesn't seem what I intended ...
 
YoGee
Posts:4,653
Registered: 6/19/02
Re: Shared Memory  
2004. 9. 3 ?? 8:02 (reply 4 of 14)  (In reply to #3 )

 
It doesn't seem what I intended ...

No probably not, but you didn't say what you're trying to do. How about java.nio.MappedByteBuffer?
 
PeteKirkham
Posts:441
Registered: 2/25/04
Re: Shared Memory  
2004. 9. 3 ?? 8:13 (reply 5 of 14)  (In reply to #4 )

 
Whether nio mapped buffers reflect other processes' changes or not is OS dependent.

Pete
 
Taglia81
Posts:34
Registered: 3/16/04
Re: Shared Memory  
2004. 9. 3 ?? 8:36 (reply 6 of 14)  (In reply to #5 )

 
I was studying alternative solutions to make a C process communicate with a Java process, to "exchange data between them".

Other alternatives i know are:
- JNI
- Named FIFOs
- Sockets

I wondered if using shared memory to do this was a possibility.
 
PeteKirkham
Posts:441
Registered: 2/25/04
Re: Shared Memory  
2004. 9. 3 ?? 8:43 (reply 7 of 14)  (In reply to #6 )

 
Probably shared memory via JNI would be the best bet, since your C process will be OS specific, and you get code re-use between the two sides of the communication.

Pete
 
Taglia81
Posts:34
Registered: 3/16/04
Re: Shared Memory  
2004. 9. 3 ?? 8:58 (reply 8 of 14)  (In reply to #7 )

 
Currently I've partially implemented an hybrid solution with FIFOs. Because the C program didn't worked as expected if I compiled it as a shared library, so that I could access directly its functions.

So I've made 2 C programs:
- the first (A) is the original program compiled normally, plus a communication module.
- the second (B) is a shared library for JNI calls and is the other peer of the communication.

So Java class make JNI calls to (B), then (B) communicates with (A) trought FIFO's

U suggest a similar solution with shared memory instead of FIFOs can be more efficient?
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Shared Memory  
2004. 9. 3 ?? 9:25 (reply 9 of 14)  (In reply to #6 )

 
This is an article I found about shared memory,

http://www.codeguru.com/Cpp/misc/misc/print.php/c415
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Shared Memory  
2004. 9. 3 ?? 9:27 (reply 10 of 14)  (In reply to #9 )

 
This is about pipes ( I guess they could also be called FIFOs)

http://www.codeproject.com/threads/anonpipe1.asp
 
edsonw
Posts:3,055
Registered: 18/06/98
Re: Shared Memory  
2004. 9. 3 ?? 12:27 (reply 11 of 14)  (In reply to original post )

 
If you can use NIO (i.e., if your JDK version is >= 1.4) you can try using the java.nio.MappedByteBuffer classes.

The idea is that you can define a file on disk that must be mapped in memory and shared between several processes.
So you can open the file, get the FileChannel, call the "map" method using the mode MapMode.READ_WRITE, and obtain a MappedByteBuffer.
 
edsonw
Posts:3,055
Registered: 18/06/98
Re: Shared Memory  
2004. 9. 3 ?? 1:01 (reply 12 of 14)  (In reply to #11 )

 
To demonstrate sharing memory using a memory-mapped file, try these programs.

Compile both, start TestShared1 in a window, and TestShared2 in a second window.
TestShared1 creates the shared memory and writes a random integer in the first 4 bytes of the shared memory. TestShared2 reads the memory that TestShared1 wrote. A file named "myshared" will be created in the current directory and backs the shared memory.

import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
 
class TestShared1 {
public static void main(String[] args) throws Exception {
Random r = new Random();
//-- Opening the file
RandomAccessFile raf = new RandomAccessFile ("myshared", "rw");
FileChannel fc = raf.getChannel();
//-- Getting the memory-mapped byte buffer
MappedByteBuffer mbb = fc.map (FileChannel.MapMode.READ_WRITE, 0, 1024);
//-- Writing a random integer in the first 4 positions of the memory
//-- every second
for (int i = 0; i < 1000; ++i) {
try { Thread.sleep(1000); } catch (InterruptedException ex) {}
int x = r.nextInt();
mbb.putInt(0, x);
System.out.println ("Written to the shared memory: " + x);
}
fc.close();
raf.close();
}
}

and
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.util.*;
 
class TestShared2 {
public static void main(String[] args) throws Exception {
Random r = new Random();
//-- Opening the file
RandomAccessFile raf = new RandomAccessFile ("myshared", "rw");
FileChannel fc = raf.getChannel();
//-- Getting the memory-mapped byte buffer
MappedByteBuffer mbb = fc.map (FileChannel.MapMode.READ_WRITE, 0, 1024);
//-- Printing the integer that is in the first 4 positions of the memory
//-- every second
for (int i = 0; i < 1000; ++i) {
try { Thread.sleep(1000); } catch (InterruptedException ex) {}
int x = mbb.getInt(0);
System.out.println ("Read from the shared memory: " + x);
}
fc.close();
raf.close();
}
}
 
Taglia81
Posts:34
Registered: 3/16/04
Re: Shared Memory  
2004. 9. 4 ?? 6:38 (reply 13 of 14)  (In reply to #12 )

 
Thanks to all, I'll read and try all suggestions here. BTW I'm programming under linux (emulating flashcard in RAM) and the final application will go on a handheld device with an ARM processor
 
Ivar_Svendsen
Posts:179
Registered: 7/30/04
Re: Shared Memory  
2004. 9. 4 ?? 3:54 (reply 14 of 14)  (In reply to #13 )

 
Using JNI in SDK 1.4, you have the functions like GetDirectBufferAddress(env, buf);
This function takes an object created by NewDirectByteBuf() or java.nio.Buffer.ByteBuffer.allocateDirect().
The buffer memory accessed by Java and JNI is supposed to be at the same physical address.


역시 MMF(Memory Mapped File)을 이용하여 shared memory를 구현하는 방법뿐이 없는거 같다. 혹시, 다른 좋은 방법이 있으시면 댓글 꼭 남겨주시기 바랍니다.

아 이젠 MMF를 통해 구현한 shared memory와 시스템 콜을 했을때와의 속도를 체크해봐야겠다.

위의 정보가 도움이 되셨나요? 그렇다면 댓글 하나만 남겨주세요.
댓글 하나가 운영자에겐 커다란 힘이 됩니다!

  1. 낭만고양이

    | 2008/09/25 17:17 | PERMALINK | EDIT | REPLY |

    흥미있는 내용이네요. 정리 감사합니다.

Write your message and submit
« PREV : 1 : ... 186 : 187 : 188 : 189 : 190 : 191 : 192 : 193 : 194 : ... 436 : NEXT »