《老罗的Android之旅》阅读笔记——IPC机制Binder



Android系统Binder机制中的四个组件Client、Server、Service Manager和Binder驱动程序的关系:


  • Service Manager成为Binder守护进程的过程

源代码路径:frameworks/base/cmds/servicemanager

成为Android进程间通信(IPC)机制Binder守护进程的过程:

        1. 打开/dev/binder文件:open("/dev/binder", O_RDWR);

        2. 建立128K内存映射:mmap(NULL, mapsize, PROT_READ, MAP_PRIVATE, bs->fd, 0);

        3. 通知Binder驱动程序它是守护进程:binder_become_context_manager(bs);

        4. 进入循环等待请求的到来:binder_loop(bs, svcmgr_handler);

Binder驱动程序中建立了三个结构体:

一个struct binder_proc结构、

一个struct  binder_thread结构、

一个struct binder_node结构

  • Server和Client获得Service Manager的过程

获取Service Manager远程接口的函数是defaultServiceManager

    gDefaultServiceManager = new BpServiceManager(new BpBinder(0));  

Server和Client拿到这个Service Manager远程接口之后:

 对Server来说,就是调用IServiceManager::addService这个接口来和Binder驱动程序交互了,即调用BpServiceManager::addService

 对Client来说,就是调用IServiceManager::getService这个接口来和Binder驱动程序交互了

  • 分析源代码了解Server的启动过程

IServiceManager::addService过程中MediaPlayerService、ServiceManager和BinderDriver之间的交互作:

  • Client获得了Server的远程接口过程

获取MediaPlayerService这个远程接口的本质:从Service Manager中获得MediaPlayerService的一个句柄

    intr = new BpMediaPlayerService(new BpBinder(handle));  


  • Android系统应用程序框架层的用Java语言来实现的Binder接口

通过JNI来调用基于C/C++语言的Binder运行库来为Java应用程序提供进程间通信服务

1. 获取Service Manager的Java远程接口的过程;

2. HelloService接口的定义;

3. HelloService的启动过程;

4. Client获取HelloService的Java远程接口的过程;

5.  Client通过HelloService的Java远程接口来使用HelloService提供的服务的过程。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值