Binder detailed explanation of Android interview notes

Binder detailed explanation of Android interview notes

Binder eight questions

1. Why do you ask Biner in the interview?

1.1 What is Binder?

Activity, Service等组件都需要和AMS 进行交互,这些跨进程的通信都是 通过 Binder来完成的。

三种角度看待Binder:
	机制:一种跨进程通信的机制

	驱动:虚拟物理设备驱动

	应用层:Binder是一个能发起通信的Java类

1.2 Why use multiple processes?

1.2.1 Because the memory allocated by the virtual machine to each process is limited, it can break through the memory limit

为什么现在的机器最大内存已经有16G了,但是你加载某个大图的时候,还是会OOM,就是因为 虚拟机分配给每个进程的内存是有限制的

1.2.2 An independent process helps to maintain the stability of the long connection (push)

1.2.3 Avoid memory leaks: independent webview process blocks the problems caused by memory leaks

1.2.4 Isolate risks and run separate processes for unstable functions to avoid the main process from crashing

1.3 System service input method, alarm clock, text message, phone... are all multi-process

AMS,WMS,PMS

如果这些服务不是多进程的,那么每一个app 都集成这些系统服务,将会导致 app 变得很大

2. What are the advantages of Binder? Compared with the traditional IPC mechanism

2.1 Traditional IPC mechanism-Linux

传统的通信机制:管道, 共享内存, socket,消息队列

ProcessA ,B通信
	1. ProcessA 通过 系统调用 copy_from_user ,将信息 copy进 内核空间

	2. ProcessB 通过 系统调用 copy_to_user, 将信息 copy 进 ProcessB的用户空间

	3. 通信完成, 两次copy

2.2 Comparison of Binder and traditional IPC

Insert picture description here

性能:Binder只需要copy一次  -- mmap

特点:基于C/S 架构,易用性搞

安全性:为每个APP分配UID(身份识别),为每一个进程打上UID的标签  支持实名和 匿名

实名就是 系统服务 eg:AMS 获取 通过 getSystemService("activity"), 通过名字来获取服务,这种是需要注册到SM中的

匿名就是 自己创建的服务

2.3 Process space division

Memory division diagram:

Insert picture description here

2.2.1 进程分 用户空间 + 内核空间,两个进程的用户空间是进程隔离的,而同一进程的用户空间和内核空间也是隔离的(通过API调用)

2.2.2 两个进程的内核空间是真实指向 同一块物理空间的

2.2.3 所有进程 的 内核空间,都是映射在同一块物理空间的,虚拟空间 和 物理空间 是通过坐标一一对应的 

2.2.4 我们所说的 用户空间 和 内核空间  都是 虚拟空间

3. How does Binder make a copy? mmap

ProcessA ,B通信
	1. ProcessA 通过 系统调用 copy_from_user ,将 信息 copy进 内核空间

	2. 然后,系统将 信息 存入 内核空间

	3. ProcessB 的 部分用户空间 和 部分内核空间 指向同一块 物理空间  (跟传统IPC不同的地方,传统IPC不会对 虚拟空间映射做处理)

	4. 在ProcessB需要取信息的时候,直接到 物理空间中取出来,直接用就好了,不用再次copy

4. Explain the principle of MMAP?

Linux通过将一个虚拟内存区域与磁盘上的一个对象关联起来,以初始化这个 虚拟内存区域的内容  这个过程称为内存映射 (memory mapping)

这个过程是由驱动来完成的   java层 mmap方法 --》 驱动层 binder_mmap

e.g. 
    1.用户是不能直接操作,文件的,因为用户在app里是虚拟空间,而文件在硬盘上,是物理空间。
    2.使用mmap 可以将 虚拟空间的 一部分内存空间 映射到 真实的屋里空间上去
    3.从而达到 用户在虚拟空间中写入数据,就相当于在 真正在操作 磁盘上的文件

4.1 How to map the source code

Insert picture description here

5. How does the Binder mechanism cross-process?

Insert picture description here

通过一次copy

6. Describe the details of the Java classes generated by AIDL?

1. (远程服务)服务端定义好接口,客户端发起方法调用 e.g. addPerson

2. 客户端会自动生成 Proxy.java类,服务端Stub会实现 addPerson方法

3. 首先会生成 两个包 (客户端用来发送数据的包 data 和用来接收服务端数据的包reply)

4. 其次 客户端 会通过 mRemote.transact(int, data, reply, flag),与服务端通信  (此时 客户端的线程会挂起, 一般都是同步通信)

5. 然后就到了服务端的Stub中的onTransact (code, data, reply, flags)方法, 通过code 区分不同的方法

6. 最后调用到 服务端实现的 具体方法,如果有 返回值的,写入到reply包中

6.1 How does the server know which method to call?

如果接口中有多个方法,客户端通过 transact(int, 第一个参数,来指定调用的方法名,而不会使用string的字符串,节省空间

7. The underlying communication mechanism of the four major components

Take binderService as an example to explain what is done in the process, and finally connect into a function to return an IBinder

7.1 How does the client obtain the IBinder object of the server

Insert picture description here

  1. The client communicates with the SM (SM is a fixed Handler-it is 0, easy to find), through the real name to obtain the AMS IBinder
    2. Through the AMS IBinder to communicate with AMS, request BindService
    3. Then the AMS will execute the scheduleBindService to the remote The onBind method of the service, get the IBinder object of the remote service

7.2 How does the server return the IBinder object

Insert picture description here

  1. Like the client, the remote server needs to obtain the AMS IBinder object
  2. Communicate with AMS through AMS's IBinder object, and publish your own (remote service) IBinder object to AMS
  3. The ibinder object of the remote service is returned to the client through the IBinder object of AMS (c.conn.connected(r.name.service) - the onServiceConnected method of the service will be called)

7.3 Overall process

Insert picture description here

7.4 How many cross-processes are there in the whole process of bindService?

客户端与 ServiceManager 也属于 跨进程

总共 跨了 6次 进程,只是为了拿到IBinder对象,拿到之后,客户端与服务端通信只需要通过这个IBinder对象来进行就可

7.4 Are AMS and Binder in the same process?

不在,AMS在系统层,而Binder在驱动层

8. Why can't Intent deliver big data?

https://wanandroid.com/wenda/show/13775
Daily Questions | Those things about Activity and Fragment, "It's okay to use, I'm going to go, you crashed?"

Memory allocated by mmap: (1M-8K) This does not include the data header

8.1 If you pass data larger than this size, it will definitely crash

8.2 Some students crashed after passing 520k, why?

如果 传递数据 是同步的,那么 mmap size是 1M - 8K

如果 传递数据 是异步的,那么 mmap size是 (1M - 8K)/2

8.2 Some students crashed after passing 300 k, why?

可能是已经使用过部分空间,然后申请的空间 > 剩余mmap的大小

Reference

  1. In the Tencent classroom, the source code analysis course provided by Xiangxue

Guess you like

Origin blog.csdn.net/HeartCircle/article/details/108747432