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 of
IPCThe way. All the following explanations are true
BinderThe 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 the
BinderThe 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 is
- The client sends the request to the server through the proxy object.
- Proxy object passed
BinderThe driver is sent to the server process
- The server process processes the request and passes the
BinderThe 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 application
Working model, above:
This is our common working model at the application level
ServiceManagerTo 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 processes
BinderClass, so it is the corresponding
BinderEntity. This class is not really remote
BinderObject, but a
BinderThe reference (that is, the class reference on the server side) is displayed in the
BinderThere is also a mapping in the driver.
- When the client wants to call the remote object function, it only needs to call the
BinderThe method of reference is generally
- 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 the
- Remote process
BinderAfter 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 server
Binder referenceThrough it to find the server and complete communication with it.
Some people may be puzzled when they see this, as shown in the picture
Thread 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 clients
Application in Android
Binder’s application in Android
ServiceManagerWhat else do you think of?
- System services are used
getSystemServiceAccess to services, that is, through the internal
ServiceManager。 For example, the startup and scheduling of the four components are passed to the activitymanagerservice through binder mechanism, and then fed back to the
Zygote。 And we usually get services through our own applications
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 passes
bindServiceWhen 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.
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
BinderAfter 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 with
ZygoteMy 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.
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.