Android进程间通讯之初见(IPC,RPC,LPC,BINDER,AIDL..)

来源:互联网 发布:超星尔雅网络课程机刷 编辑:程序博客网 时间:2024/06/08 13:12

Inter-process communication (IPC) is a set of methods for the exchange of data among multiplethreads in one or moreprocesses. Processes may be running on one or more computers connected by anetwork

也就是说,理论上所有跨线程的交互都可又称为IPC。它大致会分成两种,即LPC和RPC
The Local Procedure Call (LPC, often also referred to as Lightweight Procedure Call or Local Inter-Process Communication) is an internal, undocumentedinter-process communication facility provided by theMicrosoft Windows NT kernel for lightweight IPC between processes on theSAME computer.

In computer science, a remote procedure call (RPC) is aninter-process communication that allows acomputer program to cause asubroutine or procedure to execute in anotheraddress space (commonly on another computer on ashared network)


对于Android系统来说,除了linux所提供的IPC机制外,它还有个binder机制(实际上用哪一种IPC并不是目的,关键是要看具体的应用环境)。对于binder机制,网上有铺天盖地的资料,这里我只想谈谈自己学习中的几点看法。
学习binder最好的还是官网上的资料,http://developer.android.com/guide/index.html 
先仔细把dev guide上有关的知识点研究一下,然后再针对自己的疑问搜索解答。
因为android很像一个杂合体,涉及太多概念,而且命名组织方面个人感觉还是很乱(可能是高手们并不会太介意这类东西,他们更看重思想的过程),所以在学习过程中,时常会被很多杂揉在一起的东西搞混。这里理一下一些基本的东西

binder提供了IPC的基础,它的base interface是Ibinder, 不过我们只会从binder继承。而对于AIDL,它更像一种工具,目的就是让binder的接口更亲切可人。也就是说,AIDL照其字面意思,AIDL (Android Interface Definition Language) is similar to other IDLs you might haveworked with. It allows you to define the programming interface that both the client and service agree upon in order to communicate with each other usinginterprocess communication (IPC)。所以千万不要以为AIDL是另一种IPC,它只是让BINDER更好看的makeup。

binder的大概流程是(比如A调用B),A和B之间有共同的ibinder的reference,A利用这个reference通过底层binder驱动调用到B中的onTransact(此时A会block住),当B返回结果时,A继承执行。
对于它们如何得到共用的ibinder,对于应用场景为service的情况,就牵涉到service manager了。B先向service manager注册,这样后续A只要向service manager查询,就可以得到B的ibinder了。

原创粉丝点击