Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Binder to realize Cross-process Communication in Android

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Binder to achieve cross-process communication in Android, I believe many inexperienced people are helpless about this, for this reason this article summarizes the causes and solutions of the problem, through this article I hope you can solve this problem.

directory

What exactly is Binder?

Chinese for adhesive, meaning to bond two different processes

There are many definitions of Binder on the Internet, but they are not clear: Binder is a cross-process communication method, it implements the IBinder interface, and it is a bridge blabla connecting ServiceManager. It is estimated that everyone is dizzy and cannot understand it very well.

In my opinion, the definition of Binder is different in different scenarios.

In the explanation of this article, Binder is analyzed according to large angle-> small angle, namely:

First, we analyze Binder's model of inter-process communication mechanism from the angle of mechanism and model.

where Binder drives in the model composition are analyzed in detail

Then from the source code implementation point of view, analysis Binder in Android specific implementation

This is a comprehensive introduction to Binder, and I hope you enjoy it.

2. knowledge reserve

Before we talk about Binder, let's understand some basics.

2.1 process space allocation

A process space is divided into user space & kernel space, i.e. separating user & kernel within a process

The difference between the two:

Data in user space is not shareable between processes, so user space = unshareable space

Between processes, kernel space data can be shared, so kernel space = shareable space

In-process user interaction with the kernel is called a system call

2.2 process isolation

To ensure security & independence, one process cannot directly operate or access another process, i.e. Android processes are independent and isolated from each other.

2.3 Interprocess Communication (IPC)

After isolation, cooperation/interaction between processes is required due to certain requirements

Principles of interprocess communication

Data is first exchanged through the kernel space between processes

Data interaction is carried out through user space and kernel space within the process, so as to realize data interaction in user space between processes.

Binder, on the other hand, acts as a channel connecting two processes (kernel space).

3. Binder Cross-Process Communication Mechanism Model 3.1 Model Principle

Binder's cross-process communication mechanism model is based on the Client-Server model. The schematic diagram of the model is as follows:

Trust me, a picture can solve a problem.

3.2 Additional Note 1: Interaction between Client processes, Server processes & Service Manager processes must be via Binder drivers (using open and ioctl file manipulation functions), not direct interaction **

Reason:

Client processes, Server processes & Service Manager processes belong to the user space of the process space and cannot interact with each other

Binder drivers belong to kernel space of process space and can interact between and within processes.

Therefore, the schematic diagram can be expressed as follows:

Dashed lines indicate no direct interaction

Note 2: Binder driver & Service Manager process belongs to Android infrastructure (that is, the system has been implemented); while Client process and Server process belong to Android application layer (need to be implemented by developers themselves)

Therefore, when communicating across processes, developers only need to customize Client & Server processes and explicitly use the above three steps, and finally use Android's basic architectural features to complete inter-process communication.

Note 3: Thread management for Binder requests

The Server process creates many threads to process Binder requests

The threads that manage the Binder model are Binder-driven thread pools and managed by the Binder driver itself

rather than managed by the Server process.

The default maximum number of Binder threads for a process is 16, beyond which requests are blocked waiting for idle Binder threads.

So, when dealing with concurrency issues in interprocess communication, such as using ContentProvider, its CRUD (Create, Retrieve, Update, and Delete) method can only have 16 threads working simultaneously

At this point, I believe that you have a very clear qualitative understanding of Binder's cross-process communication mechanism model.

Below, I will analyze the specific code implementation of Binder cross-process communication mechanism model in Android through an example.

That is, analyze how the above steps are implemented in Android code.

4. Binder mechanism in Android specific implementation principle

Binder mechanism in Android implementation mainly depends on Binder class, which implements the IBinder interface

This will be explained in detail below

Example Description: Client process needs to call the addition function of Server process (add integer a and b)

Namely:

The Client process needs to pass two integers to the Server process

The Server process needs to return the sum to the Client process

concrete steps

Next, I will analyze the steps according to Binder's cross-process communication mechanism model

Step 1: Register for Services

process description

Server processes register services with Service Manager processes via Binder drivers

code implementation

The Server process creates a Binder object

Binder entities are forms in which Server processes exist in Binder drivers

This object holds information about Server and ServiceManager (stored in kernel space)

Binder driver finds Server object in user space through Binder entity in kernel space

Binder drivers hold Binder entities created by Server processes after registering services

Step 2: Access to Services

Before the Client process uses a service (in this case, an addition function), it must obtain the corresponding Service information from the ServiceManager process through the Binder driver.

The specific code implementation process is as follows:

At this point, the Client process has established a connection with the Server process

Step 3: Use of Services

According to the acquired Service information (Binder proxy object), the Client process establishes a communication link with the Server process where the Service is located through the Binder driver, and starts to use the service.

process description

The Client process sends parameters (integers a and b) to the Server process

The Server process calls the target method (i.e., the addition function) as required by the Client process.

The Server process returns the result of the target method (that is, the result of the addition) to the Client process

code implementation process

Step 1: The Client process sends parameters (integers a and b) to the Server process

Step 2: The Server process calls the target method (i.e., the addition function) according to the client request.

Step 3: The Server process returns the result of the target method (i.e., the result of the addition) to the Client process.

summary

Below, I summarize step 3 with a schematic & flowchart

schematic

5. advantages

Compared with other process communication methods (pipes/message queues/shared memory/semaphores/sockets) on Linux (Android based on Linux), Binder mechanism has the following advantages:

efficient

Binder data copies only once, while pipes, message queues, and sockets all require 2 copies.

By copying data in kernel space via drivers, no additional synchronization processing is required

high safety

Binder mechanism assigns UID/PID to each process as an identification of identity, and Binder communication will be based on UID/PID validity check

Traditional process communication methods do not strictly verify the identity of both parties

For example, Socket communication ip address is manually filled in by the client, which is easy to be forged.

using simple

Client/Server architecture

Implement object-oriented invocation, i.e. use Binder as if it were invoking a local object instance

6. summary

This article mainly explains the Binder mechanism of the cross-process communication model in detail, summarized as follows:

defined

After reading the above content, do you know how to use Binder to achieve cross-process communication in Android? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report