Android Binder Cross-process communication principle analysis

Source: Internet
Author: User

prepare bullets before you leave .

We know that the process, the virtual address is different, is not directly communication, this is a protection mechanism. Open Task Manager and look at the N-ary process, just imagine what happens if these processes communicate directly?

And the user space can communicate with the kernel space through system calls, if there is a module in kernel space, can complete the data forwarding, then is not two processes can communicate? Such as:

The above mentioned some user space, the concept of kernel space, user space can probably guess what is something, and kernel space, it is very low-level things to know. And the module, you can simply understand the implementation of a function of the program or a hardware circuit, such as playing a single-chip computer, there will be infrared module, Bluetooth module, WiFi module. These concepts of things to search the encyclopedia know just fine.

Binder Drive

The binder driver runs in kernel space, which is the kernel module. Binder driver is important, and it undertakes data forwarding of inter-process communication. A mention of driving, is also more familiar with, you plug a U disk, you need to drive it. The binder driver is similar, although the name is very good, the function is very powerful. But it's not a magical thing.

Binder Cross-process communication model

Binder's communication model has 4 roles: Binder Client, Binder Server, Binder Driver (Binder drive), ServiceManager.

Imagine a scene: I travel to Beijing, to send a postcard to high school students, postcards must be written on the address, or how to send to each other? So how do I get this address, very simple, turn over the graduation album is good. And this is a record of the students ' correspondence address of the graduation album, is quite with a directory. The role of ServiceManager is played in the binder's communication model. Well, now that we have a mailing address, it's good to find the post office. After a few days, the students happily received the postcard. So this post office in Binder communication model plays the role of binder-driven, and as the sender I was Binder Client, the recipient is Binder Server.

Let's take a picture to describe the scene above:

As you can see, ServiceManager, Binder Client, binder server are in different processes, three of them are in user space, and binder is driven in kernel space. (I purposely made the binder drive larger because the binder drive is the most important)

Let's start by outlining this communication model:

First, there is a servicemanager, just start this address book is blank, and then the server process to ServiceManager register a mapping relationship table, such as Xu classmate to his address in Guangdong province Guangzhou xx District written in the address book, then formed a table:

Guangzhou xx District, Guangdong Province, Xu classmate

After the client process wants to communicate with the server process, first queries the address to ServiceManager, ServiceManager receives the query request, returns the query result to the client.

Notice here whether the server process registration, or client query, are binder-driven, this is really the role of Binder drive, first not urgent, the following principles will be analyzed.

That's when I took the address and started sending postcards. When I put the postcard into the mailbox, then the job is done by the Post office, that is, binder driver to complete the forwarding of communications.

Binder Communication Principle

From the example of sending a postcard, the postman takes out the postcard from the mailbox and then delivers the postcard across the mountains. From this we can also think, in fact, the binder drive to complete the work is very important.

Let's restore a binder process for cross-process communication.
Case: The client process calls the server process's computer object's Add method.

The next content you may need to know the proxy mode to better understand, but did not learn the proxy mode is OK, you can read it first, and then go to fill the proxy mode, and then come back to see this article. The idea will be much clearer.

1. The server process registers with ServiceManager, tells ServiceManager who I am, what I have, what I can do. Like Xu classmate (server process) has a notebook (computer object), this notebook has an Add method. The Mapping relationship table is then generated.
2. The client process queries the ServiceManager, and I'm going to invoke the Add method of the computer object of the server process, and I can see that this process has been binder driven, when the binder driver begins to play his part. When the query to ServiceManager is complete, is it return a computer object to the client process? In fact, the binder driver transforms the computer object into a Computerproxy object and forwards it to the client process, so the client process gets not the real computer object, but a proxy object. That is, the Computerproxy object. It is easy to understand that the Computerproxy object also has the Add method, (if not even the Add method, is not deceived the client?) ), but this Add method simply wraps the parameters in some way.
3. When the client process calls the Add method, the message is sent to the binder driver, and the driver discovers that the original is Computerproxy, then the client process should call the Add method of the computer object, and the driver notifies the server process, Call the Add method of your computer object and give me the result. The server process then sends the calculation results to the driver, the driver and then to the client process, the client process is also covered in the drum, he thought he called the real computer object of the Add method, in fact, he just called the proxy. However, the client finally got the result of the calculation.

Well, a communication process is complete. We found that the binder drive is actually a relay.


Again, to summarize: when the client process queries the server process to ServiceManager (I want to invoke a method of one of your objects), this process is also a process of cross-process communication, but also through the binder driver, then Binder Drive play its role, A civet cat for the Prince, the real object in the server process into a proxy object, return this proxy object to the client process.
The client process gets the proxy object, and then calls the proxy object's method, and the binder driver continues to perform his mission, which notifies the server process to do the calculation and returns the results of the actual objects in the server process to the client process. In this way the client process has got what it wants. Cross-process communication complete!

This article is written mainly I learn binder when the pain point, I read a lot of articles about binder, found that many are not the binder drive, I am very confused to force why so important things on the skip, but still have great God write very good, such as: Android Binder Design and implementation

Freshman from the field of computer pale to all day holding Guo Shen "The first line of code" gnawing, from which I not only learned the technology, the most important thing is that I learned the pursuit of technology. I am grateful to all the people who accompany me to develop technology. I will continue to write a blog, record the growth of their own while also can help some of the foot in the pit comrades.

I am participating in CSDN blog star selection, if you like my article, please vote for me, every day can vote, thank you for your support.
Vote Link: Http://

Android Binder Cross-process communication principle analysis

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.