參考自:https://blog.csdn.net/carson_ho/article/details/73560642
一,Linux內核的基礎知識
1,進程隔離/虛擬地址空間
爲了保證 安全性 & 獨立性,一個進程不能直接操作或者訪問另一個進程,即Android的進程是相互獨立、隔離的
2,系統調用(用戶空間和內核空間)
進程間,用戶空間的數據不可共享,所以用戶空間 = 不可共享空間
進程間,內核空間的數據可共享,所以內核空間 = 可共享空間
所有進程共用1個內核空間
進程內 用戶空間 & 內核空間 進行交互 需通過 系統調用,主要通過函數:
copy_from_user():將用戶空間的數據拷貝到內核空間
copy_to_user():將內核空間的數據拷貝到用戶空間
3,binder驅動連接進程
通過binder創建的接收緩存區
,分別映射
到內核緩存區和接收進程的用戶空間,從而建立進程間連接
二,Binder機制 在Android中的具體實現原理
Client進程、Server進程 & Service Manager 進程(用戶空間)之間的交互 都必須通過Binder(內核空間)驅動(使用 open 和 ioctl文件操作函數),而非直接交互
1,註冊服務
註冊服務後,Binder驅動持有 Server進程創建的Binder實體
Binder binder = new Stub();
// 步驟1:創建Binder對象 ->>分析1
// 步驟2:創建 IInterface 接口類 的匿名類
// 創建前,需要預先定義 繼承了IInterface 接口的接口 -->分析3
IInterface plus = new IPlus(){
// 確定Client進程需要調用的方法
public int add(int a,int b) {
return a+b;
}
// 實現IInterface接口中唯一的方法
public IBinder asBinder(){
return null ;
}
};
// 步驟3
binder.attachInterface(plus,"add two int");
// 1. 將(add two int,plus)作爲(key,value)對存入到Binder對象中的一個Map<String,IInterface>對象中
// 2. 之後,Binder對象 可根據add two int通過queryLocalIInterface()獲得對應IInterface對象(即plus)的引用,可依靠該引用完成對請求方法的調用
// 分析完畢,跳出
<-- 分析1:Stub類 -->
public class Stub extends Binder {
// 繼承自Binder類 ->>分析2
// 複寫onTransact()
@Override
boolean onTransact(int code, Parcel data, Parcel reply, int flags){
// 具體邏輯等到步驟3再具體講解,此處先跳過
switch (code) {
case Stub.add: {
data.enforceInterface("add two int");
int arg0 = data.readInt();
int arg1 = data.readInt();
int result = this.queryLocalIInterface("add two int") .add( arg0, arg1);
reply.writeInt(result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
// 回到上面的步驟1,繼續看步驟2
<-- 分析2:Binder 類 -->
public class Binder implement IBinder{
// Binder機制在Android中的實現主要依靠的是Binder類,其實現了IBinder接口
// IBinder接口:定義了遠程操作對象的基本接口,代表了一種跨進程傳輸的能力
// 系統會爲每個實現了IBinder接口的對象提供跨進程傳輸能力
// 即Binder類對象具備了跨進程傳輸的能力
void attachInterface(IInterface plus, String descriptor);
// 作用:
// 1. 將(descriptor,plus)作爲(key,value)對存入到Binder對象中的一個Map<String,IInterface>對象中
// 2. 之後,Binder對象 可根據descriptor通過queryLocalIInterface()獲得對應IInterface對象(即plus)的引用,可依靠該引用完成對請求方法的調用
IInterface queryLocalInterface(Stringdescriptor) ;
// 作用:根據 參數 descriptor 查找相應的IInterface對象(即plus引用)
boolean onTransact(int code, Parcel data, Parcel reply, int flags);
// 定義:繼承自IBinder接口的
// 作用:執行Client進程所請求的目標方法(子類需要複寫)
// 參數說明:
// code:Client進程請求方法標識符。即Server進程根據該標識確定所請求的目標方法
// data:目標方法的參數。(Client進程傳進來的,此處就是整數a和b)
// reply:目標方法執行後的結果(返回給Client進程)
// 注:運行在Server進程的Binder線程池中;當Client進程發起遠程請求時,遠程請求會要求系統底層執行回調該方法
final class BinderProxy implements IBinder {
// 即Server進程創建的Binder對象的代理對象類
// 該類屬於Binder的內部類
}
// 回到分析1原處
}
<-- 分析3:IInterface接口實現類 -->
public interface IPlus extends IInterface {
// 繼承自IInterface接口->>分析4
// 定義需要實現的接口方法,即Client進程需要調用的方法
public int add(int a,int b);
// 返回步驟2
}
<-- 分析4:IInterface接口類 -->
// 進程間通信定義的通用接口
// 通過定義接口,然後再服務端實現接口、客戶端調用接口,就可實現跨進程通信。
public interface IInterface
{
// 只有一個方法:返回當前接口關聯的 Binder 對象。
public IBinder asBinder();
}
// 回到分析3原處
2,獲取服務
3,使用服務
步驟1: Client進程 將參數(整數a和b)發送到Server進程
// 1. Client進程 將需要傳送的數據寫入到Parcel對象中
// data = 數據 = 目標方法的參數(Client進程傳進來的,此處就是整數a和b) + IInterface接口對象的標識符descriptor
android.os.Parcel data = android.os.Parcel.obtain();
data.writeInt(a);
data.writeInt(b);
data.writeInterfaceToken("add two int");;
// 方法對象標識符讓Server進程在Binder對象中根據"add two int"通過queryLocalIInterface()查找相應的IInterface對象(即Server創建的plus),Client進程需要調用的相加方法就在該對象中
android.os.Parcel reply = android.os.Parcel.obtain();
// reply:目標方法執行後的結果(此處是相加後的結果)
// 2. 通過 調用代理對象的transact() 將 上述數據發送到Binder驅動
binderproxy.transact(Stub.add, data, reply, 0)
// 參數說明:
// 1. Stub.add:目標方法的標識符(Client進程 和 Server進程 自身約定,可爲任意)
// 2. data :上述的Parcel對象
// 3. reply:返回結果
// 0:可不管
// 注:在發送數據後,Client進程的該線程會暫時被掛起
// 所以,若Server進程執行的耗時操作,請不要使用主線程,以防止ANR
// 3. Binder驅動根據 代理對象 找到對應的真身Binder對象所在的Server 進程(系統自動執行)
// 4. Binder驅動把 數據 發送到Server 進程中,並通知Server 進程執行解包(系統自動執行)
步驟2:Server進程根據Client進要求 調用 目標方法(即加法函數)
// 1. 收到Binder驅動通知後,Server 進程通過回調Binder對象onTransact()進行數據解包 & 調用目標方法
public class Stub extends Binder {
// 複寫onTransact()
@Override
boolean onTransact(int code, Parcel data, Parcel reply, int flags){
// code即在transact()中約定的目標方法的標識符
switch (code) {
case Stub.add: {
// a. 解包Parcel中的數據
data.enforceInterface("add two int");
// a1. 解析目標方法對象的標識符
int arg0 = data.readInt();
int arg1 = data.readInt();
// a2. 獲得目標方法的參數
// b. 根據"add two int"通過queryLocalIInterface()獲取相應的IInterface對象(即Server創建的plus)的引用,通過該對象引用調用方法
int result = this.queryLocalIInterface("add two int") .add( arg0, arg1);
// c. 將計算結果寫入到reply
reply.writeInt(result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
// 2. 將結算結果返回 到Binder驅動
步驟3:Server進程 將目標方法的結果(即加法後的結果)返回給Client進程
// 1. Binder驅動根據 代理對象 沿原路 將結果返回 並通知Client進程獲取返回結果
// 2. 通過代理對象 接收結果(之前被掛起的線程被喚醒)
binderproxy.transact(Stub.ADD, data, reply, 0);
reply.readException();;
result = reply.readInt();
}
}
三. 優點
對比 Linux (Android基於Linux)上的其他進程通信方式(管道、消息隊列、共享內存、 信號量、Socket),Binder 機制的優點有:
拓展閱讀
https://blog.csdn.net/freekiteyu/article/details/70082302