An article to get "Understanding Binder cross-process communication"

foreword

I call Binder the old mother of Android
. As the top priority of Android, the previous "JVM Perfect Diagram" and "MMKV Principle Analysis" are all foreshadowing. Brothers have been afraid to
write this Binder article, but for an article The series is relatively complete, so write it down as a comprehensive review.
The structure of this article:

  • What is Binder (list the common use of Android to Binder)
  • Basic knowledge reserve
  • Binder model diagram
  • Advantages over other IPC mechanisms

What is Binder

Binder is an inter-process communication (IPC) mechanism in Android, which is used to realize communication between different processes.
It runs through our entire Android system: simply speaking, Binder is a cross-process communication method, it implements the IBinder interface, and is a bridge connecting various Services, Manages and components.
for example:

  • AIDL
  • Activity startup process

In fact, these are just some of the scenarios we encountered. The real use of Binder is the Service and Manager of our Android framework.

  • ActivityManagerService : It is responsible for managing and scheduling the life cycle of the application, process management, task management, etc. It uses the Binder mechanism for inter-process communication and interacts with other components and system services.
  • PackageManagerService : It is a component of the Android system, responsible for application installation, uninstallation, application information query and management. It uses the Binder mechanism to communicate and manage components such as ActivityManagerService. Through the Binder mechanism, PackageManagerService can accept requests from other components, handle application installation and uninstallation, and provide application information query services.
  • WindowManagerService : It is a component of the Android system, responsible for the addition, removal and management of windows, and the hierarchical management of windows. It also uses the Binder mechanism to communicate with other components and works with the ActivityManagerService to manage the application's tasks and windows. Through the Binder mechanism, WindowManagerService can receive instructions from ActivityManagerService to manage application windows, task switching and other operations.
  • ActivityThread : It is one of the core components of the Android system, responsible for the startup of the application, the management of the Activity, and the communication with the ActivityManagerService. It also uses the Binder mechanism to communicate with the ActivityManagerService, such as initiating a request to the ActivityManagerService to start the Activity, receiving the callback of the Activity life cycle, and so on. ActivityThread interacts with ActivityManagerService through the Binder mechanism to coordinate the operation of the application and the management of the Activity.
  • ServiceManager : is a component of the Android system, used for inter-process service discovery and management. It uses the Binder mechanism to provide applications with access to many system services. Applications can obtain instances of system services through ServiceManager, and communicate with these system services through the Binder mechanism.
    These include ActivityManagerService, PackageManagerService, WindowManagerService, ActivityThread, and ServiceManager, which are the core components of the Android system. They communicate between processes through the Binder mechanism, and realize the collaboration between different components, the management of applications, and the access and management of system services. The Binder mechanism provides an efficient and secure inter-process communication capability for the Android system.

Basic knowledge reserve

process space division

A process space is divided into user space & kernel space, that is, to isolate users between processes, and then use kernel space to interact (this is a common memory isolation)

  • User space : Because our APP runs in our independent APP process. So you can think of APP as a user space. The two user space data are not shared (that is, the space cannot be shared)
  • Kernel space : It is the storage area for operating system kernel code and data. Data in kernel space can be shared. (that is, shareable space)
  • All processes use the same kernel space .
  • The traditional IPC communication process is as follows :
    insert image description here
  1. copy_from_user(): Copy user space data to kernel space
  2. copy_to_user(): Copy kernel space data to user space

It can be seen that in order to ensure security & independence, one process cannot directly operate or access another process, that is, the APP processes in Android are independent and isolated from each other. If you want to communicate, you need to use the kernel space for IPC communication
.

IPC communication

Some of the IPC communications provided to us by the Linux system are:

  • Shared Memory: Shared memory is a mechanism for sharing storage areas between multiple processes. Processes can communicate by reading and writing shared memory.
  • Socket (Socket): A socket is a mechanism for inter-process communication in a network, through which multiple hosts can communicate.
  • Pipe (Pipe): A pipe is a half-duplex, memory-based channel that can be used for communication between a parent process and a child process.
  • Signal (Signal): Signal is an asynchronous communication mechanism for passing notification and interrupt information between processes.
  • Semaphore (Semaphore): A semaphore is a mechanism for synchronization and mutual exclusion between processes, which can be used to protect access to shared resources.
  • Message Queue (Message Queue): Message Queue is a mechanism for transferring data between processes. Processes can send messages to the queue, and other processes can receive messages from the queue.

When we compare later, we will illustrate the process of some IPC

Binder model diagram

A lot of IPC communication mechanisms have been mentioned above.
In fact, Binder is not a normal Linux communication method, it is an inter-process communication in the Android operating system.
But Android is developed based on the Linux kernel, so it can be considered that Binder is also an IPC communication mechanism of Linux.

Binder communication process

Let's talk about how Binder communicates first. Directly above the picture,
insert image description here
you can see that compared with the traditional IPC communication method above, there is one less CPU copy process.
Send data workflow: (also marked in the figure)

  • 1. The Binder driver creates a receive buffer
  • 2. Realize the address mapping relationship: create a mapping between kernel space and user space through mmap (also mentioned in MMKV)
  • 3. The sending process sends data to the virtual memory area through CPU copy (copy_from_user)
  • 4. Space mapping in kernel space
  • 5. Because of the mapping relationship of mmap, the cache area of ​​Binder is equivalent to accepting the user space of the process, so as to realize communication.

Return result process: (not marked in the figure, I believe you can think of it)

  • 1. Because of the mmap mapping, the acceptance process returns the result to the Binder buffer
  • 2. Space mapping of kernel space
  • 3. Send data to the sending process through CPU copy (copy_to_user)

Binder's C/S model

The Binder communication method mentioned above is just the most important part of our Binder model. Let's take a look at the entire Binder model! !
The Binder cross-process communication mechanism model is based on the Client-Server mode
Description:

  1. Client process, Server process & Service Manager process belong to the user space of the process space, and inter-process interaction is not allowed
  2. The Binder driver belongs to the kernel space of the process space, and can perform inter-process & intra-process interaction. The
    old picture is first shown: (The next article will use the picture of a complete example of AIDL)
    insert image description here
  • Client process: The process that uses the service. (Android client)
  • Server process: A process that provides services. (Android service process)
  • ServiceManager process: manage Service registration and query (equivalent to a table that manages the references of Serve and Client to Binder)
  • Binder driver: Binder communication above. (mmap mapping communication)
  • The thread management of the Binder model adopts the thread pool driven by Binder, and is managed by the Binder driver itself. The default maximum number of Binder threads in a process is 16, so in the CRUD method of ContentProvider, only 16 threads can work at the same time.

Explain the above process in text form:
1. Register the service (the ServiceManager process has the information of the Server process)

  • The Server process initiates a service registration request to the Binder driver
  • The Binder driver forwards the registration request to the ServiceManager
  • Service Manager process adds Server process information

2. Obtain the service (at this time, not only the ServiceManager has the information of the Client process, but also establishes a connection with the Server)

  • Client initiates a service request to the Binder driver, passing the service information to be obtained
  • The Binder driver forwards the request to the ServiceManager
  • The ServiceManager finds the service information of the Server corresponding to the Client's service request
  • The Binder driver returns the found service information to the Client process

3. Using the service

  • At this time, after the connection is established, we can start using our service through our Binder communication process above, and carry out the communication process of sending process -> receiving process.
  • Paste the picture here for easy viewing. This is the following process.
    insert image description here

Advantages over other IPC mechanisms

Performance:

  • Traditional pipes, message queues, and sockets all need to be copied twice: copy_from_user, copy_to_user
  • Binder uses the mmap mechanism to copy data only once, because CPU copy consumes performance, which makes Binder's performance higher
  • The efficiency of shared memory is the highest: it will open up a shared memory segment of two processes in the kernel space, and then both processes can operate this process segment (then some students asked: why not use shared memory, because the security is not good) see below)

On security:

  • Shared memory has no security: the upper layer needs to control the security by itself, because the two processes operate on the same block of kernel memory. There is no security control, and it can be invaded by others at will. (The upper layer is required to control access rights and synchronization mechanisms)
  • In traditional IPC communication, identities can be faked. For example: Socket communication ip is manually filled in by the client
  • Binder mechanism: Each process has UID/PID as a sign of identity. Then communicate after verifying the identity during communication. Security is high.

Use:

  • Binder uses the C/S structure, and using Binder is as simple as using an object.

Summarize

The content of Binder needs to read the source code and actual combat to be proficient.
But the introduction to this article is what Android programmers must know.

Guess you like

Origin blog.csdn.net/weixin_45112340/article/details/131841774