Mysterious, common and changeable binder

Time:2021-7-1

Today, let’s talk about the mysterious, frequently used and changeable binder~

  • What is binder
  • Binder communication process and principle
  • Application in Android
  • Binder advantage

What is binder

First of all, let’s borrow a passage from the Divine Book “Android development art exploration”:

Intuitively, binder is a class that implements the ibinder interface.

From the perspective of IPC (inter process communication), binder is a cross process communication mode in Android.

It can also be understood as a virtual physical device whose device driver is / dev / binder.

From the perspective of Android framework, binder is a bridge for service manager to connect various managers (activity manager, window manager, etc.) and respond to manager service.

From the perspective of Android application layer, binder is the medium for communication between client and server.

It’s a lot of concepts, isn’t it,BinderIs used for inter process communication, is a kind ofIPCThe way. All the following explanations are trueBinderThe practical application involves the content.

Whether it’s getting other system services, or the communication between the server and the client, it all comes from theBinderThe ability of interprocess communication.

Binder communication process and principle

First of all, let’s look at a picture. The original picture is also from God’s book:

First of all, it should be clear that the client process cannot directly operate the classes and methods in the server, because different processes do not share resources directly. So the client side only operates a proxy object of the server side process, that is, a class reference of the server side, that isBinderquote.

populationCommunication processnamely:

  • The client sends the request to the server through the proxy object.
  • Proxy object passedBinderThe driver is sent to the server process
  • The server process processes the request and passes theBinderThe driver returns the processing result to the proxy object
  • The proxy object returns the result to the client.

Let’s take a look at the common ones in our applicationWorking model, above:

This is our common working model at the application levelServiceManagerTo obtain various system process services. The communication process here is as follows (please refer to the link at the end of the article for the detailed process)

  • The server should inherit all classes across processesBinderClass, so it is the correspondingBinderEntity. This class is not really remoteBinderObject, but aBinderThe reference (that is, the class reference on the server side) is displayed in theBinderThere is also a mapping in the driver.
  • When the client wants to call the remote object function, it only needs to call theBinderThe method of reference is generallytransactFunction.
  • The binder reference then puts the data into the shared memory of the client,BinderThe driver reads data from the shared memory of the client and finds the shared memory of the corresponding remote process according to the data.
  • Then the data is copied to the shared memory of the remote process, and the remote process is informed to execute the ontransact() function, which also belongs to theBinderClass.
  • Remote processBinderAfter the object is executed, it writes the data to its own shared memory. Binder driver copies the shared memory data of the remote process to the shared memory of the client, and wakes up the client thread.

So in the process of communication, the most important thing is the function of the serverBinder referenceThrough it to find the server and complete communication with it.

Some people may be puzzled when they see this, as shown in the pictureThread poolWhy didn’t you use it?

  • As you can see from the first picture,Binder thread poolLocated on the server side, its main function is to forward the binder requests of each business module to the remote servie for execution, so as to avoid the repeated process of creating a service. That is to say, there is only one server, but it can handle multiple different clientsBinderRequest.

Application in Android

Binder’s application in AndroidServiceManagerWhat else do you think of?

  • System services are usedgetSystemServiceAccess to services, that is, through the internalServiceManager。 For example, the startup and scheduling of the four components are passed to the activitymanagerservice through binder mechanism, and then fed back to theZygote。 And we usually get services through our own applicationsgetSystemService(getApplication().WINDOW_SERVICE)Code acquisition.
  • AIDL(Android Interface definition language)。 For example, if we define an iserver.aidl file, the Aidl tool will automatically generate a java interface class of iserver.java (including stub, proxy and other internal classes).
  • The foreground process passesbindServiceWhen binding the background service process, onserviceconnected (componentname, ibinder service) returns the ibinder object, and can obtain the internal proxy class object of iserver through iserver.stub.asinterface (service), which implements the iserver interface.

Binder advantage

In Linux, binder is definitely not the only way of process communication, but also the following ways:

Pipe
Signal
Message queuing
Shared memory
Socket
Binder

andBinderAfter that, it has the following advantages:

  • High performance, high efficiency: traditional IPC (socket, pipe, message queue) needs to copy memory twice, binder only needs to copy memory once, shared memory does not need to copy memory.
  • Good security: the receiver can obtain the process ID and user ID of the sender from the packet, which is convenient for verifying the identity of the sender. Other IPC want to experiment and can only save it actively, but it may be modified in the process of sending.

be familiar withZygoteMy friends may know that in the fork () process, that is, when the message of creating a process is sent to the zygote process, the inter process communication method used is not binder, but socket. This is mainly because fork does not allow multithreading,BinderCommunication is multi thread.

So the specific situation or to choose the appropriate IPC way.

reference resources

https://www.cnblogs.com/hustcser/p/10228843.html

bye-bye

There are small partners who can study together and pay attention to them ❤️ My official account, the building blocks on the code, dissect a knowledge point every day, and we accumulate knowledge together.