一、Ashmem驱动程序

~/Android/kernel/goldfish

----include

----linux

----ashmem.h

----mm

----ashmem.c

驱动程序具体解释请看《Android系统源码情景分析》。作者罗升阳。

二、执行时库cutils的匿名共享内存訪问接口

~/Android/system/core

----libcutils

----ashmem-dev.c

具体解释请看《Android系统源码情景分析》,作者罗升阳。

三、MemoryFile

~/Android/frameworks/base/core/java/android/os

----MemoryFile.java

~/Android/frameworks/base/core/jni

----android_os_MemoryFile.cpp

具体解释请看《Android系统源码情景分析》,作者罗升阳。

~/Android/frameworks/base/core/java/android/os

----MemoryFile.java

    public ParcelFileDescriptor getParcelFileDescriptor() throws IOException {
FileDescriptor fd = getFileDescriptor();
return fd != null ? new ParcelFileDescriptor(fd) : null;
} .......
public FileDescriptor getFileDescriptor() throws IOException {
return mFD;
}

四、应用实例

具体解释请看《Android系统源码情景分析》,作者罗升阳。

在本节中。我们将创建一个Android应用程序Ashmem。它由一个Service组件Server和一个Activity组件Client组成。Server组件执行在一个独立的进程中,它内部有一个内存訪问服务MemoryService,后者通过MemoryFile类创建一块匿名共享内存。

Client组件执行在还有一个进程中,它会将内存訪问服务MemoryService创建的那块匿名共享内存映射到本进程的地址空间,以便能够訪问它的内容,从而能够和Server组件共享一块匿名共享内存。

~/Android/packages/experimental/Ashmem

----AndroidManifest.java

----Android.mk

----src

----shy/luo/ashmem

----IMemoryService.java

----MemoryService.java

----Server.java

----Client.java

----res

----layout

----main.xml

----values

----strings.xml

----drawable

----icon.png

IMemoryService.java

package shy.luo.ashmem;

import android.util.Log;
import android.os.IInterface;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException; public interface IMemoryService extends IInterface {
public static abstract class Stub extends Binder implements IMemoryService {
private static final String DESCRIPTOR = "shy.luo.ashmem.IMemoryService"; public Stub() {
attachInterface(this, DESCRIPTOR);
} public static IMemoryService asInterface(IBinder obj) {
if (obj == null) {
return null;
} IInterface iin = (IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (iin != null && iin instanceof IMemoryService) {
return (IMemoryService)iin;
} return new IMemoryService.Stub.Proxy(obj);
} public IBinder asBinder() {
return this;
} @Override
public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws android.os.RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_getFileDescriptor: {
data.enforceInterface(DESCRIPTOR); ParcelFileDescriptor result = this.getFileDescriptor(); reply.writeNoException(); if (result != null) {
reply.writeInt(1);
result.writeToParcel(reply, 0);
} else {
reply.writeInt(0);
} return true;
}
case TRANSACTION_setValue: {
data.enforceInterface(DESCRIPTOR); int val = data.readInt();
setValue(val); reply.writeNoException(); return true;
}
} return super.onTransact(code, data, reply, flags);
} private static class Proxy implements IMemoryService {
private IBinder mRemote; Proxy(IBinder remote) {
mRemote = remote;
} public IBinder asBinder() {
return mRemote;
} public String getInterfaceDescriptor() {
return DESCRIPTOR;
} public ParcelFileDescriptor getFileDescriptor() throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain(); ParcelFileDescriptor result; try {
data.writeInterfaceToken(DESCRIPTOR); mRemote.transact(Stub.TRANSACTION_getFileDescriptor, data, reply, 0); reply.readException();
if (0 != reply.readInt()) {
result = ParcelFileDescriptor.CREATOR.createFromParcel(reply);
} else {
result = null;
}
} finally {
reply.recycle();
data.recycle();
} return result;
} public void setValue(int val) throws RemoteException {
Parcel data = Parcel.obtain();
Parcel reply = Parcel.obtain(); try {
data.writeInterfaceToken(DESCRIPTOR);
data.writeInt(val); mRemote.transact(Stub.TRANSACTION_setValue, data, reply, 0); reply.readException();
} finally {
reply.recycle();
data.recycle();
}
}
} static final int TRANSACTION_getFileDescriptor = IBinder.FIRST_CALL_TRANSACTION + 0;
static final int TRANSACTION_setValue = IBinder.FIRST_CALL_TRANSACTION + 1; } public ParcelFileDescriptor getFileDescriptor() throws RemoteException;
public void setValue(int val) throws RemoteException;
}

MemoryService.java

package shy.luo.ashmem;

import java.io.FileDescriptor;
import java.io.IOException; import android.os.Parcel;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import android.util.Log; public class MemoryService extends IMemoryService.Stub {
private final static String LOG_TAG = "shy.luo.ashmem.MemoryService";
private MemoryFile file = null; public MemoryService() {
try {
file = new MemoryFile("Ashmem", 4);
setValue(0);
}
catch(IOException ex) {
Log.i(LOG_TAG, "Failed to create memory file.");
ex.printStackTrace();
}
} public ParcelFileDescriptor getFileDescriptor() {
Log.i(LOG_TAG, "Get File Descriptor."); ParcelFileDescriptor pfd = null; try {
pfd = file.getParcelFileDescriptor();
} catch(IOException ex) {
Log.i(LOG_TAG, "Failed to get file descriptor.");
ex.printStackTrace();
} return pfd;
} public void setValue(int val) {
if(file == null) {
return;
} byte[] buffer = new byte[4];
buffer[0] = (byte)((val >>> 24) & 0xFF);
buffer[1] = (byte)((val >>> 16) & 0xFF);
buffer[2] = (byte)((val >>> 8) & 0xFF);
buffer[3] = (byte)(val & 0xFF); try {
file.writeBytes(buffer, 0, 0, 4);
Log.i(LOG_TAG, "Set value " + val + " to memory file. ");
}
catch(IOException ex) {
Log.i(LOG_TAG, "Failed to write bytes to memory file.");
ex.printStackTrace();
}
}
}

Server.java

package shy.luo.ashmem;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.os.ServiceManager; public class Server extends Service {
private final static String LOG_TAG = "shy.luo.ashmem.Server"; private MemoryService memoryService = null; @Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public void onCreate() {
Log.i(LOG_TAG, "Create Memory Service..."); memoryService = new MemoryService(); try {
ServiceManager.addService("AnonymousSharedMemory", memoryService);
Log.i(LOG_TAG, "Succeed to add memory service.");
} catch (RuntimeException ex) {
Log.i(LOG_TAG, "Failed to add Memory Service.");
ex.printStackTrace();
} } @Override
public void onStart(Intent intent, int startId) {
Log.i(LOG_TAG, "Start Memory Service.");
} @Override
public void onDestroy() {
Log.i(LOG_TAG, "Destroy Memory Service.");
}
}

Client.java

package shy.luo.ashmem;

import java.io.FileDescriptor;
import java.io.IOException; import shy.luo.ashmem.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.MemoryFile;
import android.os.ParcelFileDescriptor;
import android.os.ServiceManager;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class Client extends Activity implements OnClickListener {
private final static String LOG_TAG = "shy.luo.ashmem.Client"; IMemoryService memoryService = null;
MemoryFile memoryFile = null; private EditText valueText = null;
private Button readButton = null;
private Button writeButton = null;
private Button clearButton = null; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); IMemoryService ms = getMemoryService();
if(ms == null) {
startService(new Intent("shy.luo.ashmem.server"));
} else {
Log.i(LOG_TAG, "Memory Service has started.");
} valueText = (EditText)findViewById(R.id.edit_value);
readButton = (Button)findViewById(R.id.button_read);
writeButton = (Button)findViewById(R.id.button_write);
clearButton = (Button)findViewById(R.id.button_clear); readButton.setOnClickListener(this);
writeButton.setOnClickListener(this);
clearButton.setOnClickListener(this); Log.i(LOG_TAG, "Client Activity Created.");
} @Override
public void onResume() {
super.onResume(); Log.i(LOG_TAG, "Client Activity Resumed.");
} @Override
public void onPause() {
super.onPause(); Log.i(LOG_TAG, "Client Activity Paused.");
} @Override
public void onClick(View v) {
if(v.equals(readButton)) {
int val = 0; MemoryFile mf = getMemoryFile();
if(mf != null) {
try {
byte[] buffer = new byte[4];
mf.readBytes(buffer, 0, 0, 4); val = (buffer[0] << 24) | ((buffer[1] & 0xFF) << 16) | ((buffer[2] & 0xFF) << 8) | (buffer[3] & 0xFF);
} catch(IOException ex) {
Log.i(LOG_TAG, "Failed to read bytes from memory file.");
ex.printStackTrace();
}
} String text = String.valueOf(val);
valueText.setText(text);
} else if(v.equals(writeButton)) {
String text = valueText.getText().toString();
int val = Integer.parseInt(text); IMemoryService ms = getMemoryService();
if(ms != null) {
try {
ms.setValue(val);
} catch(RemoteException ex) {
Log.i(LOG_TAG, "Failed to set value to memory service.");
ex.printStackTrace();
}
}
} else if(v.equals(clearButton)) {
String text = "";
valueText.setText(text);
}
} private IMemoryService getMemoryService() {
if(memoryService != null) {
return memoryService;
} memoryService = IMemoryService.Stub.asInterface(
ServiceManager.getService("AnonymousSharedMemory")); Log.i(LOG_TAG, memoryService != null ? "Succeed to get memeory service." : "Failed to get memory service."); return memoryService;
} private MemoryFile getMemoryFile() {
if(memoryFile != null) {
return memoryFile;
} IMemoryService ms = getMemoryService();
if(ms != null) {
try {
ParcelFileDescriptor pfd = ms.getFileDescriptor();
if(pfd == null) {
Log.i(LOG_TAG, "Failed to get memory file descriptor.");
return null;
} try {
FileDescriptor fd = pfd.getFileDescriptor();
if(fd == null) {
Log.i(LOG_TAG, "Failed to get memeory file descriptor.");
return null;
} memoryFile = new MemoryFile(fd, 4, "r");
} catch(IOException ex) {
Log.i(LOG_TAG, "Failed to create memory file.");
ex.printStackTrace();
}
} catch(RemoteException ex) {
Log.i(LOG_TAG, "Failed to get file descriptor from memory service.");
ex.printStackTrace();
}
} return memoryFile;
}
}

首先開始在Activity,onCreate时开启了Service。然后点击读按钮,执行相应代码。

进程间通信具体步骤,临时省略,仅仅看表明的步骤,见下图:

接下来的分析,请看Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析http://blog.csdn.net/luoshengyang/article/details/6666491

注意上文中的这句话,  这里, 我们须要关注的便是虚线框部分了,它在Binder驱动程序中实现了在两个进程中共享同一个打开文件的方法。我们知道。在Linux系统中,文件描写叙述符事实上就是一个整数。每个进程在内核空间都有一个打开文件的数组,这个文件描写叙述符的整数值就是用来索引这个数组的。并且,这个文件描写叙述符仅仅是在本进程内有效,也就是说。在不同的进程中,同样的文件描写叙述符的值。代表的可能是不同的打开文件。因此。在进程间传输文件描写叙述符时,不能简要地把一个文件描写叙述符从一个进程传给另外一个进程。中间必须做一过转换。使得这个文件描写叙述在目标进程中是有效的,并且它和源进程的文件描写叙述符所相应的打开文件是一致的。这样才干保证共享。

也就是依据fd。得到的file结构,在两个进程是一样的。即使两个进程的fd不一样。

Android系统匿名共享内存(Anonymous Shared Memory)Java调用接口分析的更多相关文章

  1. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...

  2. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)驱动程序源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6664554 在上一文章Android系统匿名共 ...

  3. Android系统匿名共享内存(Anonymous Shared Memory)C++调用接口分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6939890 在Android系统中,针对移动设 ...

  4. 【并行计算-CUDA开发】关于共享内存(shared memory)和存储体(bank)的事实和疑惑

    关于共享内存(shared memory)和存储体(bank)的事实和疑惑 主要是在研究访问共享内存会产生bank conflict时,自己产生的疑惑.对于这点疑惑,网上都没有相关描述, 不管是国内还 ...

  5. CUDA学习(五)之使用共享内存(shared memory)进行归约求和(一个包含N个线程的线程块)

    共享内存(shared memory)是位于SM上的on-chip(片上)一块内存,每个SM都有,就是内存比较小,早期的GPU只有16K(16384),现在生产的GPU一般都是48K(49152). ...

  6. 共享内存(shared memory)

    共享内存指在多处理器的计算机系统中,可以被不同中央处理器(CPU)访问的大容量内存.由于多个CPU需要快速访问存储器,这样就要对存储器进行缓存(Cache). 任何一个缓存的数据被更新后,由于其他处理 ...

  7. CUDA学习(六)之使用共享内存(shared memory)进行归约求和(M个包含N个线程的线程块)

    在https://www.cnblogs.com/xiaoxiaoyibu/p/11402607.html中介绍了使用一个包含N个线程的线程块和共享内存进行数组归约求和, 基本思路: 定义M个包含N个 ...

  8. IPC最快的方式----共享内存(shared memory)

    在linux进程间通信的方式中,共享内存是一种最快的IPC方式.因此,共享内存用于实现进程间大量的数据传输,共享内存的话,会在内存中单独开辟一段内存空间,这段内存空间有自己特有的数据结构,包括访问权限 ...

  9. Fresco内存机制(Ashmem匿名共享内存)

    Fresco的内存机制 Fresco是Facebook出品的高性能图片加载库,采用了Ashmem匿名共享内存机制, 来解决图片加载中的OOM问题.这里不对Fresco做深入分析,只关注Fresco在A ...

随机推荐

  1. Python入门学习(二)

    1 字典 1.1 字典的创建和访问 字典不同于前述的序列类型,它是一种映射类型.它的引入是为了简化定义索引值和元素值存在特定关系的定义和访问问题. 字典的定义形式为:字典变量名 = {key1:val ...

  2. 使用springboot完成密码的加密解密

    现今对于大多数公司来说,信息安全工作尤为重要,就像京东,阿里巴巴这样的大公司来说,信息安全是最为重要的一个话题,举个简单的例子: 就像这样的密码公开化,很容易造成一定的信息的泄露.所以今天我们要讲的就 ...

  3. Win10命令大全通用(Win8,Win7)

    Windows 10/Win10命令大全通用(Win8,Win7 Windows 10/Win10命令大全通用(Win8,Win7) 1.calc:启动计算器       2.appwiz.cpl:程 ...

  4. jQuery+ajax实现局部刷新

    在项目中,经常会用到ajax,比如实现局部刷新,比如需要前后端交互等,这里呢分享局部刷新的两种方法,主要用的是ajax里面的.load(),其他高级方法的使用以后再做详细笔记. 第一种: 当某几个页面 ...

  5. 《天书夜读:从汇编语言到windows内核编程》十一 用C++编写内核程序

    ---恢复内容开始--- 1) C++的"高级"特性,是它的优点也是它的缺点,微软对于使用C++写内核程序即不推崇也不排斥,使用C++写驱动需注意: a)New等操作符不能直接使用 ...

  6. EAS(学生管理系统)初建

    一.确定开发使用的技术             本次开发EAS示例网站,使用Servlet+JSP+MySQL技术,其中包括使用bootstrap工具完成简易前端页面设计.所有数据实体与数据关系皆用数 ...

  7. python进阶---Python中的socket编程

    初识socket编程 一.前言 socket基于C\S架构(客户端\服务端)的编程模型,在Python中是以socket模块存在的. Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是 ...

  8. 最小化安装linux CentOS_7操作系统

    实验环境为VMware虚拟机安装操作系统. 1.打开VMware Workstation 虚拟机,选择创建新的虚拟机: 2.选择linux-CentOS 64位操作系统: 3.为虚拟机命名,并选择安装 ...

  9. Python之Django环境搭建(MAC+pycharm+Django++postgreSQL)

    Python之Django环境搭建(MAC+pycharm+Django++postgreSQL) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7828614. ...

  10. install plugin group_replication ERROR 1126 (HY000)

    在给MySQL安装插件的时候,你可能会遇到如题所示的报错. 更详细的错误输出如下: mysql> INSTALL PLUGIN group_replication SONAME 'group_r ...