using ashmem

7,522 views
Skip to first unread message

sienna

unread,
Feb 24, 2009, 9:34:23 PM2/24/09
to android-platform
Hello,

I'm trying to create a shared memory region which is shared between
processes. Since shmget() and shm_open() are not implemented, ashmem
and mmap looks good for this purpose, like below :

fd = ashmem_create_region("my_shm_region", size);
if(fd < 0)
return -1;

data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
0);
if(data == MAP_FAILED)
goto out;

What I'm wondering is how other processes find out the named memory
block. I have looked at the ashmem_create_region() but it always
returns fd of /dev/ashmem, regardless of the name of segment
(my_shm_region). What if more than two processes have shared segments
at the same time. Plus, there's only region creation function, but not
segment opening function, which is unlike shm_open. How can other
processes access same segment with the same name? Is there a user
lever function to open the existed segment?

Even if the name is 'anonymous' but it is described as it can be
shared between processes. I'd like to know how to.. Thank you.

Sienna

Dave Sparks

unread,
Feb 24, 2009, 10:14:17 PM2/24/09
to android-platform
For security reasons, you cannot access shared memory by name. You
pass a file descriptor through the binder interface. See the IMemory
implementations for examples of how ashmem is used.

Chen Yang

unread,
Mar 3, 2009, 10:58:58 AM3/3/09
to android-...@googlegroups.com
Would some one like to tell the rationale behind the
design and implementation of the ashmem in the kernel?
Compared with traidtional shared memory or mmaped memory,
what kind of advantage can ashmem provide? Thanks.
--
Chen

sienna

unread,
Mar 4, 2009, 4:45:52 PM3/4/09
to android-platform
Dave,

Thanks, I wrote a program(running as a server) which allocates ashmem
by MemoryHeapBase, from your advice. There are also clients as well to
get shared memory segments from the server through binder. In my
program, MemoryHeapBase objects were created several times and I just
found their file descriptor were same. Does ashmem_create_region()
apply just once per one thread? I thoguht fd is unique just inside
same context. Is ashmem fd system-wide?


On Feb 24, 7:14 pm, Dave Sparks <davidspa...@android.com> wrote:
> For security reasons, you cannot access shared memory by name. You
> pass a file descriptor through the binder interface. See the IMemory
> implementations for examples of howashmemis used.
>
> On Feb 24, 6:34 pm, sienna <copil...@gmail.com> wrote:
>
>
>
> > Hello,
>
> > I'm trying to create a shared memory region which is shared between
> > processes. Since shmget() and shm_open() are not implemented,ashmem
> > and mmap looks good for this purpose, like below :
>
> >     fd = ashmem_create_region("my_shm_region", size);
> >     if(fd < 0)
> >         return -1;
>
> >     data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
> > 0);
> >     if(data == MAP_FAILED)
> >         goto out;
>
> > What I'm wondering is how other processes find out the named memory
> > block. I have looked at the ashmem_create_region() but it always
> > returns fd of /dev/ashmem, regardless of the name of segment
> > (my_shm_region). What if more than two processes have shared segments
> > at the same time. Plus, there's only region creation function, but not
> > segment opening function, which is unlike shm_open. How can other
> > processes access same segment with the same name? Is there a user
> > lever function to open the existed segment?
>
> > Even if the name is 'anonymous' but it is described as it can be
> > shared between processes. I'd like to know how to.. Thank you.
>
> > Sienna- Hide quoted text -
>
> - Show quoted text -

lit...@gmail.com

unread,
Jan 4, 2018, 12:17:33 PM1/4/18
to android-platform
Hello Sienna,

I also face to this, in android, I have two c++ ndk processes, I want to find out the named memory  
block by ashmem_create_region in another c++ process, and I can't use java process.

Do you have any solution or good idea?

Thanks
TiegangLiu

在 2009年2月25日星期三 UTC+8上午10:34:23,sienna写道:

lit...@gmail.com

unread,
Jan 4, 2018, 12:17:33 PM1/4/18
to android-platform
Hello Dave,

Do you have any suggestion about native(not java ) code to pass filedescriptor that generated by ashmem_create_region to another process?

I wrote an email to slenna:

>Hello sienna:
>I also face to this, in android, I have two c++ ndk processes, I want to find out the named memory  

>block by ashmem_create_region in another c++ process, and I can't use java process.

>Do you have any solution or good idea?

>Thanks
>TiegangLiu

在 2009年2月25日星期三 UTC+8上午11:14:17,Dave Sparks写道:

Laxman V. N.

unread,
Aug 22, 2019, 11:50:32 AM8/22/19
to android-platform
Hi,

Kindly share the native code example to pass filedescriptor that generated by ashmem_create_region to another process?

Clark Kent

unread,
Aug 25, 2019, 11:24:50 AM8/25/19
to android-platform
I use this

int shmFD = 0;
void * data = nullptr;
assert(SHM_create(shmFD, &data, 512));
if (CompositorMain.server.socket_accept()) {
serializer
in;
serializer out;
CompositorMain.server.socket_get_serial(in);
int command = -1;
in.get<int>(&command);
else if (command == GLIS_SERVER_COMMANDS.shm) {
out.add<int>(shmFD);
if (SERVER_LOG_TRANSFER_INFO)
LOG_INFO_SERVER("%ssending id %zu", CompositorMain.server.TAG, shmFD);
CompositorMain.server.socket_put_serial(out);
}
assert(CompositorMain.server.socket_unaccept());

Reply all
Reply to author
Forward
0 new messages