binder驱动层简单研究
内存被操作系统划分为用户空间和内核空间,他们都是虚拟内存,用户空间是用户代码运行的地方,内核空间是内核代码运行的地方。为了安全,它们是隔离的,即使用户的程序崩溃了,内核也不受影响。
android可以使用binder进行进程间通讯,他和传统的ipc对比有什么优势呢
1.socket
需要拷贝两次,基于C/S 架构作为一款通用接口,其传输效率低,开销大,依赖上层协议访问接入点是开放的不安全
2.共享内存
无需拷贝,控制复杂,易用性差,依赖上层协议访问接入点是开放的不安全
3.Binder
需要拷贝一次,基于C/S 架构,易用性高,为每个APP分配UID同时支持实名和匿名
简单描述copy一次
数据发送方通过copy_from_user发到到内核地址空间,mmap()通过映射把一块内核地址空间和物理内存空间映射起来,然后数据接收房直接从物理内存中获取,只copy了一次。相当于内核空间和数据接收进程共享了一片区域
binder驱动启动流程


首先执行device_initcall(binder_init)方法binder_deferred_workqueue=create_singlethread_workqueue("binder")创建单利线程init_binder_device初始化binder,
binder_device = kzalloc(sizeof(*binder_device), GFP_KERNEL);分配内存
binder_init简单来说主要做,分配内存,初始化设备,放入链表binder_devices
binder_open

创建binder_proc对象,当前进程信息proc,filp->private_data,添加到binder_procs链表中
binder_mmap

为什么intent传输不能超过1m-8k,这是由binder决定的
1.通过用户空间的虚拟内存大小分配一块内核的虚拟内存
2.分配一块物理内存--4kb
3.把这块物理内存分别映射到用户空间的虚拟内存和内核的虚拟内存
等需要的时候他会进行扩充
binder_ioctl

binder_ioctl()函数负责在两个进程间收发IPC数据和IPC reply数据,Native C\C++ 层传入不同的cmd和数据,根据cmd的值,进行相应的处理并返回,进行读写乱七八糟操作
binder的jni操作
调用app_main.cpp的main方法启动zygote


然后图在androidruntime方法里面注册jni,使app能调用native方法,java和native通讯要通过jni,native和java是要名字映射的,互相调用,binder的jni注册使native和java是要名字映射的,互相调用


自己看,借一张偷来的图
