Elaborate Binder Theory and Mechanism

First on work flow chart of a Binder. (If not clear, you can copy the picture link into your browser or saved to a local view, I often like this Figure, ha)

More Android interview senior collection on github above the (more interview documents, projects download, source code)
https://github.com/xiangjiana/androids
need more items to download, the source code of a small partner can click on the link I get

Elaborate Binder Theory and Mechanism

Beginning to use, more strange things, But, in fact, it is not complicated. Oh, a flow chart is drawn ProcessOn. Great online drawing tools.

Before departure preparation process between what we know, different virtual addresses, are not directly communicate, which is a protective mechanism. Open the Task Manager, look at the N multi-process, imagine what will happen if these processes direct communication will bring?

Kernel space and user space can communicate with, and if there is a module in kernel space, to complete the data forwarded by System calls (callback system), it is not two processes can communicate with it? As shown below:

image

Some of the above-mentioned user space, the concept of kernel space, user space can probably guess what it is, and the kernel space, you know that it is good for a very bottom of things. The module it can be simply understood as the realization of a functional program or a hardware circuit, such as playing the microcontroller when there will be an infrared module, Bluetooth module, wifi module. What these concepts search Wikipedia know like.

Binder driver

Binder driver running in kernel space, it is that the kernel module. Binder driver is very important to undertake inter-process data transfer and other communications. Mention drive, is more familiar, you insert a U disk, you need to drive it. Binder while driving is similar, although the name has made very good, very powerful feature. But not a magical thing.

Binder cross-process communication model

Binder communication model has four characters: Binder Client, Binder Server, Binder Driver (Binder drive), ServiceManager.

想象一个情景:我到北京旅行,要给高中同学寄一张明信片,明信片肯定要写上地址吧,不然怎么寄给对方呢?那么我怎么拿到这个地址呢,很简单,翻一下毕业相册就好了。而这个记录着同学们通信地址的毕业相册,就相当与一个通讯录。在Binder的通信模型中扮演的是ServiceManager的角色。好,现在已经有了通信地址了,那么就找到邮局寄出去就好了。过几天同学就高高兴兴的收到了明信片。那么这个邮局在Binder通信模型中扮演的是Binder驱动的角色,而作为寄信人的我就是Binder Client,收信人同学就是Binder Server。

先上一张图来描述上面的那个情景:

Elaborate Binder Theory and Mechanism

可以看到,ServiceManager、Binder Client、Binder Server处于不同的进程,他们三个都在用户空间,而Binder驱动在内核空间。(我是特意把Binder驱动画的比较大的,因为Binder驱动的作用最大)

那先来简述一下这个通信模型:

首先是有一个ServiceManager,刚开始这个通讯录是空白的,然后Server进程向ServiceManager注册一个映射关系表,比如雷同学把自己的地址湖南省长沙市xx区写进通讯录,那么就形成了一张表:

雷同学 —> 湖南省长沙市xx区

之后Client进程想要和Server进程通信,首先向ServiceManager查询地址,ServiceManager收到查询的请求之后,返回查询结果给Client。

注意到这里不管是Server进程注册,还是Client查询,都是经过Binder驱动的,这也真是Binder驱动的作用所在,先不急,下面的原理会分析到。

这时候我就拿着地址就开始寄明信片咯。当我把明信片放扔进邮筒,之后的工作就是由邮局去完成了,也就是Binder驱动去完成通信的转发。

Binder通信原理

从寄明信片的例子中,邮递员从邮筒取出明信片,然后跨越千山万水将明信片送达。从这点我们也能想到,其实Binder驱动完成的工作是很重要的。

我们来还原一个Binder跨进程通信的过程。 案例:Client进程调用Server进程的computer对象的add方法。

接下来的内容你可能需要知道代理模式才能更好的理解,不过没学习过代理模式也没关系,可以先读下去,然后在去补一下代理模式,再回来看这篇文章。思路会清晰很多。

1. Server进程向ServiceManager注册,告诉ServiceManager我是谁,我有什么,我能做什么。就好比徐同学(Server进程)有一台笔记本(computer对象),这台笔记本有个add方法。这时映射关系表就生成了。

2. Client进程向ServiceManager查询,我要调用Server进程的computer对象的add方法,可以看到这个过程经过Binder驱动,这时候Binder驱动就开始发挥他的作用了。当向ServiceManager查询完毕,是返回一个computer对象给Client进程吗?其实不然,Binder驱动将computer对象转换成了computerProxy对象,并转发给了Client进程,因此,Client进程拿到的并不是真实的computer对象,而是一个代理对象,即computerProxy对象。很容易理解这个computerProxy对象也是有add方法,(如果连add方法都没有,岂不是欺骗了Client?),但是这个add方法只是对参数进行一些包装而已。

3. 当Client进程调用add方法,这个消息发送给Binder驱动,这时驱动发现,原来是computerProxy,那么Client进程应该是需要调用computer对象的add方法的,这时驱动通知Server进程,调用你的computer对象的add方法,将结果给我。然后Server进程就将计算结果发送给驱动,驱动再转发给Client进程,这时Client进程还蒙在了鼓里,他以为自己调用的是真实的computer对象的add方法,其实他只是调用了代理而已。不过Client最终还是拿到了计算结果。

Well, a communication process is complete. We found that, in fact, Binder is a transit driver.

to sum up

Again comb summarize: When the process Client Server processes the query process to ServiceManager (I want to call a method of an object you up), this process is an inter-process communication, it has also been driven Binder, Binder then drive to play its role, came a 狸猫换太子, Server process to convert into a real object proxy object, the proxy object to the Client returns process. Client process to get this proxy object, and then call the proxy object methods, Binder drives continue to play his mission, it will notify the Server process executes calculations, the results Server process real object execution is returned to the Client process, so Client process or do so to get what you want. Cross-process communication is completed

about me

More Android interview senior collection on github above the

Very much like to share with everyone and common progress

Currently a programmer, Android developers not only to share knowledge, but also to share technical people grew up, including personal summary, experience in the workplace, interview experience, you want to make less go a little detour. **

Guess you like

Origin blog.51cto.com/14541311/2443658