Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
610 views
in Technique[技术] by (71.8m points)

c - How to use Shared Memory (IPC) in Android

I've already written a simple Shared Memory C program in Linux.
How can I use Shared Memory (or should I call it "ashmem?") in Android?

I hope you can give me a step-by-step guide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is what worked for me:
1) Open a MemoryFile object: mFile;
2) create a service to map it to ashem using mmap;
3) Return the native file descriptor (fd) to client that binds to your service using ParcelFileDescriptor pfd;
4) Create JNI for the client that takes the fd and map to ashes using mmap;
5) Create InputStream using this fd and now the client can read/write the same memory area using InputStream object.
This link shows how to map a MemoryFile to ashem. This link shows how to send native file descriptor (fd) to client through AIDL and ParcelFileDescriptor to client.

On the server side, you will need create:
1) A service & AIDL that passes native fd to client through ParcelFileDescriptor; 2) A JNI on the service side that does the mapping.

On the client: 1) Methods to Bind to service and then call service to get native fd; 2) A JNI that maps the fd to ashem.

The mapped memory region can then be read/write by the service & client.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...