The interviewer asks you a question: Tell me what you know about Binder drivers. Although this question has some “interview rocket” helpless, but the difficulty is the bright spot, the value lies, is the effective means of screening the interviewer. How much would you say if you had to answer? Let’s take a look at the answers of 😎, 😨 and πŸ€”οΈ


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

Interviewer ️ : Tell me what you know about Binder drivers

😎 : Binder drivers are very low-level things, the cornerstone of the Binder mechanism in the system kernel.

Interviewer: Is that all? Tell me what you know

😎 : let me say that understanding is bad to answer directly, still ask me a question

Interviewer: Ok, so you mentioned the system kernel. Let’s talk about user space and kernel space

😎 : don’t know, this thing understood also don’t matter use! I know a lot about business development API, such as RecycleView layout, I wrote a thief ~

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 what you know about Binder drivers

😨 : Binder mechanism is divided into four parts: Binder driver, Service Manager, client and server. Service Manager is DNS, binder driver is router, it runs in kernel space, different processes can communicate through binder driver.

Interviewer: Why do Binder drivers run in kernel space? Can I move it to user space?

😨 : No, the process space of the two processes has different virtual address mapping rules. The memory is not shared and cannot communicate with each other directly. Linux divides the process space into user space and kernel space, running user programs and system kernel respectively.

User space and kernel space are also isolated, but you can copy data from user space to kernel space using copy_from_user, and from kernel space to user space using copy_to_user.

Binder drivers need to be in kernel space to communicate between processes. Binder mmap the kernel space and target user space. Just call copy_from_user and copy it once.

Interviewer: How do YOU call the binder drivers of kernel space from user space?

😨 : I don’t know about binder. I haven’t read binder source code, but I just know the general communication mode

Interviewer: What else do you know about Binder drivers

😨 : um… Without the

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 what you know about Binder drivers

πŸ€”οΈ : Draw a simple picture:

As a router for IPC communication, Binder is responsible for data interaction between different processes and is the core of Binder mechanism. For Linux systems, it is a character driver device that runs in kernel space and provides the /dev/binder device nodes and system calls like open, Mmap, and IOCtl to the upper layer.

Interviewer: You mentioned drivers. Let’s talk about Linux drivers first

πŸ€”οΈ : Linux abstracts all hardware access to files for reading and writing, setting, the concrete implementation of this “abstraction” is the driver program. The driver acts as a hub between hardware and software, providing a standardized set of calls and mapping those calls to actual hardware device-specific operations, hiding the details of how the device works from the application.

Linux driver devices fall into three categories: character devices, block devices, and network devices. A character device is a device that can be accessed like a byte stream file. When a character device is read/written, I/O of the actual hardware generally follows. Character device drivers typically implement open, close, read, and write system calls, such as display, keyboard, serial port, LCD, LED, and so on.

Block devices refer to devices that can be accessed by transferring 512 or 1K data blocks, such as hard disks, SD cards, USB flash drives, and Cd-RoMs. Network devices are devices that can exchange data with other hosts, such as network cards and Bluetooth devices.

The character device has a special MISC miscellaneous device, device number 10, that automatically generates device nodes. Ashmem and Binder for Android are misC miscellaneous devices.

Interviewer: Have you seen the implementation of binder driven open, MMAP, and IOCTL methods?

πŸ€” ️ : Binder_open (), binder_mmap(), and binder_ioctl(), respectively, correspond to binder_open(), binder_mmap(), and binder_ioctl(), respectively. Binder_proc is a structure that holds binder related data, one copy per process.

The main job of binder_mmap() is to create a map of the application process’s virtual memory in the kernel, so that the application and the kernel have shared memory space in preparation for a later copy.

Binder_ioctl () is one of the most complex binder_ioctl() drivers. Binder_ioctl () is the most complex binder_ioctl() driver. It does most of the business for Binder drivers.

Interviewer: How does a single method, binder_ioctl(), do most of the work?

πŸ€” ️ : Binder mechanisms subdivide services into commands. Binder_ioctl () calls are passed with specific commands to distinguish services, such as BINDER_WRITE_READ commands that read and write data, and Service Manager commands that are registered as DNS.

The BINDER_WRITE_READ command is the most critical, which subdivides several subcommands, such as BC_TRANSACTION, BC_REPLY, and so on. BC_TRANSACTION is the most commonly used upper-level IPC invocation command, and the TRANSact method of the AIDL interface is this command.

Interviewer: Binder drivers use data structures to hold all of these functions, such as the binder_proc you mentioned in the binder_open() method. What other structures do you know?

πŸ€”οΈ : Know some, such as:

The structure of the body instructions
binder_proc Describes processes that use binder and are created when the binder_open function is called
binder_thread Describes threads using binder that are created when the binder_ioctl function is called
binder_node Describes a Binder entity node that corresponds to a serve, a user-mode BpBinder object
binder_ref Describes a reference to a binder entity node associated with a binder_node
binder_buffer Describes the buffers that store data during communication with the binder
binder_work Describes a binder task
binder_transaction Describes data information related to a binder task
binder_ref_death Description Death information of binder_node, a binder server

The main structure reference relationship is as follows:

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

Although this question has some “interview rocket” helpless, but the difficulty is the bright spot, the value lies, is the effective means of screening the interviewer. If you were asked this question, how much would you answer?

The last

I recently put together an Android Binder Principles Study Note, Linux, C/S architecture, ServiceManager, ProcessState, system service registration process, ServiceManager startup process, system service acquisition process, MediaPlayerService, JNI, Binder class registration, AMS, JavaBBinder “** and other N technical points.

Directory: Binder mechanism in ServiceManager three, system service registration process four, ServiceManager startup process five, system service acquisition process six, Java Binder initialization seven, Java The registration process for system services in Binder

  • Due to the length, in order to avoid affecting your reading experience, only part of the content is shown in screenshots, if you need【 Click on me 】It’s free.

Screenshot of some notes

First, knowledge must be understood before learning Binder

IPC mechanism of Linux and Android 2. IPC communication principle of Linux and Binder 3. 4. Learn About Binder?

Binder mechanism in ServiceManager

C/S architecture based on Binder communication 2. Main function of MediaServer 3. ProcessState unique to each process 4. Binder mechanism in ServiceManager

Registration process of system services

1. Describe how MediaPlayerService is registered from the point of view of call chain 2. How is MediaPlayerService registered from a process perspective

4. Start the ServiceManager

1.ServiceManager entry functions 1.1 Enabling Binder devices 1.2 Registering as a Context manager for Binder mechanisms 1.3 Cyclic waiting and processing requests from clients

  • Due to the length, in order to avoid affecting your reading experience, only part of the content is shown in screenshots, if you need【 Click on me 】It’s free.

5. Process of obtaining system services

1. The MediaPlayerService client requests service 2. The server ServiceManager handles requests

Java Binder initialization

Java Binder JNI Registration 1.1 Binder registration 1.2 BinderInternal registration

Java Binder system services registration process

1. 1.1 BinderInternal AMS registration to ServiceManager. GetContextObject. () 1.2 ServiceManagerNative asInterface (1.3) getIServiceManager().addService()

JavaBBinder 3. JavaBBinder 4.Java Binder architecture

In-depth analysis of Binder source code

As long as programmers, whether Java or Android, if not to read the source code, only to see API documents, it is just floating on the surface, which is detrimental to the establishment and completion of our knowledge system and the promotion of actual combat technology.

One of the best ways to exercise your ability is to read the source code directly, not only for Android, but also for all the great open source libraries.

Reading source code is always the fastest way to learn.

Binder source code analysis: Binder source code analysis

The first section Binder series – the beginning of the second section of the Binder Driver preliminary exploration of the third section Binder Driver to explore the fourth section Binder Driver start ServiceManager fifth section obtain ServiceManager

Binder interview question

First-line enterprise APP is multi-threaded and multi-process, and Android interprocess communication mechanism is Binder, native interthread communication is Handler, Binder and Handler is a knowledge point to understand Android operating mechanism must be mastered, but also first-line enterprise interview must ask knowledge point!

Binder interview questions:

  • What are the advantages of Binder? (Bytedance)
  • How does Binder make one copy? (tencent)
  • Explain the principle of MMAP; (tencent)
  • Why can’t IntEnts deliver big data? (ali)
  • Describe the details of the Java classes generated by AIDL; (Bytedance)
  • The communication mechanism at the bottom of the four components; (Bytedance)
  • Why can’t IntEnts deliver big data? (ali)
  • How do Binder mechanisms cross processes?
  • Binder mechanism principle
  • .

Well, here, the whole note is almost introduced, interested friends can【 Click on me 】It’s free.