Let’s have a look at the performance of 😎, 😨 and 🤔️


😎 thinks he knows everything, has reached the application development ceiling, and currently earns 10K a month

Interviewer ️ : Talk about your understanding of Binder

😎 : Binder is used for cross-process communication and can be divided into client, Server, Binder driver and Service Manager.

Interviewer: Do you know the principle of one copy?

😎 : Not quite clear, in fact, for application development, there is no need to know.

Interviewer: Ok, go back and wait for the announcement


😨 plays games, watches TV dramas and stays up late in his spare time. His monthly salary is 15K

Interviewer: Tell me about Binder

😨 : Binder is an IPC mode. Compared with Linux pipes, shared memory, and sockets, binder uses MMAP to copy data once. It is faster than Socket and pipe, safer and controllable than shared memory, and is the main IPC communication mode in Android.

Interviewer: With Binder, intEnts are limited in size.

😨 : um… It should matter

Interviewer: How does Binder limit this size?

😨 : I don’t know. I haven’t read the relevant source code in depth.

Interviewer: Ok, go back and wait for the announcement


🤔️ insist on learning every day and constantly improve myself. My current monthly salary is 30K

Interviewer: Tell me about Binder

🤔️ : Binder is the main cross-process communication method in Android. Binder driver and Service Manager are equivalent to router and DNS in network protocol respectively, and mMAP-based IPC data transmission requires only one copy.

Binder includes BinderProxy, BpBinder and other binder entities, as well as ProcessState and IPCThreadState encapsulation of binder driver operations, plus the internal structure of binder driver, command processing, As a whole, it runs through Java and Native layer, involving user mode and kernel mode. Service, AIDL and mMAP and Binder drive devices are mentioned above, which is a huge and tedious mechanism.

I don’t have enough time in a day to talk about it myself, so ask me specific questions.

Interviewer: How do you implement a copy based on Mmap?

🤔️ : Actually very simple, let me draw a schematic diagram:

The Client and Server are in different processes and have different virtual address rules. Therefore, they cannot communicate with each other. A page box can be mapped to multiple pages, so a physical memory block can be mapped to the virtual memory block of the Client and Server, respectively.

As shown, the Client only needs copy_FROm_user to make a copy of the data, and the Server process can read the data. In addition, the mapped virtual memory block size is close to 1M (1m-8K), so the amount of data transferred by IPC communication is also limited to this value.

Interviewer: What do you know about page frames and pages?

🤔️ : A page frame is a block of actual physical memory, and a page is a block of in-memory data units of a program. The memory data must be stored in the actual physical memory, i.e. the page must correspond to a page frame, and the page data is actually stored on the page frame.

A page frame is the same size as a page, and is the kernel’s unit of memory partition. A page box can be mapped to multiple pages, which means that a piece of physical storage can be mapped to multiple virtual memory Spaces of multiple processes, and this is a fundamental rule on which the MMAP mechanism relies.

Interviewer: Talk a little bit about binder’s overall architecture

🤔️ : Let’s draw a simple diagram of a typical IPC communication flow between two applications:

binder_design.jpg

The Client uses Proxy to encapsulate remote binder entities obtained by ServiceManager or AMS, such as ServiceManagerProxy and Proxy classes generated by AIDL. The encapsulated remote Binder entity is a BinderProxy.

Binder and BinderProxy are actually one and the same thing: remote Binder entities, just a Native layer, a Java layer, with a Binder handle value called Handle inside.

ProcessState is a process singleton that opens Binder drivers and MMAP; IPCThreadState is a thread singleton responsible for communicating specific commands with binder drivers.

A transact() call from the Proxy packages the data into a Parcel, cascading down to BpBinder, where the transact() method of IPCThreadState is called and the handle value is passed in. IPCThreadState executes specific binder commands.

The binder to Server process is as follows: The Server receives a Client request via IPCThreadState and then calls back to Stub onTransact().

Of course, this does not mean that all IPC processes, such as Service Manager as a Server, do not have the upper layer of encapsulation, without the aid of IPCThreadState, Instead, the binder_loop() method is initialized to communicate directly with binder drivers.

Interviewer: Yes, let’s talk about something else.


What do you think of the interview performance of these three students?