Analysis of Java Interface Source Code of Android System Interprocess Communication Binder Mechanism in Application Framework Layer

In the Android system, each application is composed of some activities and services, these activities and services may run in the same process, it may also run in different processes. So, how do activities or services that are not in the same process communicate? This is the Binder inter-process communication mechanism to be introduced in this article.

The book "Analysis of Android System Source Code Scenarios" is being serialized on the Attack Programmer Network ( http://0xcc0xcd.com ), click to enter!

        We know that the Android system is based on the Linux kernel, and the Linux kernel inherits and is compatible with the rich inter-process communication (IPC) mechanism of the Unix system. There are traditional pipeline (Pipe), signal (Signal) and trace (Trace), these three communication methods can only be used between parent process and child process, or between sibling processes; later added command pipeline (Named Pipe) ), so that inter-process communication is no longer limited to parent-child processes or between sibling processes; in order to better support transaction processing in commercial applications, in AT&T's Unix System V, three more called "System V IPC" have been added. The inter-process communication mechanisms are message queue (Message), shared memory (Share Memory) and semaphore (Semaphore); later BSD Unix made important extensions to the "System V IPC" mechanism, providing a method called "System V IPC". Socket (Socket) interprocess communication mechanism. If you want to learn more about these inter-process communication mechanisms, it is recommended to refer to the book "Linux Kernel Source Code Scenario Analysis" mentioned in the article Android Learning Startup .

        However, the Android system does not use the various inter-process communication mechanisms mentioned above, but uses the Binder mechanism. Is it because the mobile device has the characteristics of poor hardware performance and low memory? unknown. Binder is not actually a new inter-process communication mechanism proposed by Android, it is implemented based on OpenBinder . OpenBinder was first developed by Be Inc. and then Palm Inc. followed suit. Now OpenBinder author Dianne Hackborn is working at Google and is responsible for the development of the Android platform.

        As mentioned repeatedly, Binder is an inter-process communication mechanism. It is a distributed component architecture similar to COM and CORBA. In general, it provides remote procedure call (RPC) functions. From the literal meaning of English, Binder has the meaning of binder, so what does it bind together? In the Binder mechanism of the Android system, it is composed of a system component, namely Client, Server, Service Manager and Binder driver, in which Client, Server and Service Manager run in user space, and Binder driver runs in kernel space. Binder is a kind of adhesive that glues these four components together. Among them, the core component is the Binder driver, and the Service Manager provides auxiliary management functions. Client and Server are in the Binder driver and Service Manager. On the provided infrastructure, the communication between Client-Server is carried out. The Service Manager and Binder drivers have been implemented in the Android platform, and developers only need to implement their own Client and Server components according to the specifications. It is easy to say, but difficult to do. For beginners, the Binder mechanism of the Android system is the most difficult to understand, and the Binder mechanism is the most important component of the Android system from the perspective of system development or application development. , therefore, it is necessary to have a deep understanding of how Binder works. To understand how Binder works, the best way is to read the source code related to Binder. Linus Torvalds, the originator of Linux, once said a famous saying RTFSC: Read The Fucking Source Code.

        Although reading the source code of Binder is the best way to learn the mechanism of Binder, it must not be unprepared, because the relevant source code of Binder is relatively boring and difficult to understand. If it can be supplemented with some theoretical knowledge, then even better. Without further ado, there is still a lot of information about the Binder mechanism on the Internet. I don’t want to write it again in detail here. The following two articles are strongly recommended:

        The Binder mechanism of Android in simple language

        Android Binder Design and Implementation - Design Chapter

        Android Binder Mechanism Explained in Simple Language This article starts from the situation and introduces in depth the relationship between the three components of Binder in user space, Client, Server and Service Manager. The Android Binder Design and Implementation article introduces the Binder driver in kernel space in detail. data structures and design principles. Many thanks to these two authors for bringing us such good Binder learning materials. To sum up, the relationship between the four components Client, Server, Service Manager and Binder driver in the Android system Binder mechanism is shown in the following figure:

        

        1. Client, Server and Service Manager are implemented in user space, and Binder driver is implemented in kernel space

        2. The Binder driver and Service Manager have been implemented in the Android platform, and developers only need to implement their own Client and Server in the user space

        3. Binder driver provides device file /dev/binder to interact with user space, Client, Server and Service Manager communicate with Binder driver through open and ioctl file operation functions

        4. The inter-process communication between Client and Server is achieved indirectly through the Binder driver

        5. The Service Manager is a daemon process that manages the Server and provides the Client with the ability to query the Server interface

        At this point, I finally have a perceptual understanding of the Binder mechanism, but I still feel that it cannot run through the entire IPC communication process from top to bottom. Therefore, I plan to analyze the Binder source code through the following four scenarios to further understand the Binder mechanism. :

        1. How does Service Manager become a daemon? That is, how the Service Manager informs the Binder driver that it is the context manager of the Binder mechanism.

        2. How do the Server and Client obtain the Service Manager interface? That is how the defaultServiceManager interface is implemented.

        3. How does Server start its own service? How does the Service Manager provide services for the Server during the server startup process? That is how the IServiceManager::addService interface is implemented.

        4   How does Service Manager provide services for Client? That is, how the IServiceManager::getService interface is implemented.

        In the next four articles, the Binder source code will be analyzed according to these four scenarios, which will involve the Binder related source code from user space to kernel space. Why is there no scenario of how Client and Server communicate between processes? This is because Service Manager acts as a daemon while it also acts as a Server. Therefore, as long as we can understand the third and fourth scenarios, we also understand how the Client and Server in the Binder mechanism communicate between processes through the Binder driver.

        In order to describe the principle and implementation of the Binder mechanism for inter-process communication in the Android system, in the next four articles, we will introduce the implementation of the Binder mechanism based on the C/C++ language. However, when we develop applications in the Android system , are based on the Java language, therefore, in the last article, we will introduce the implementation of the Java interface of the Android system inter-process communication Binder mechanism in the application framework layer in detail:

        5.  Analysis of the Java interface source code of the Android system inter-process communication Binder mechanism in the application framework layer.

Lao Luo's Sina Weibo: http://weibo.com/shengyangluo , welcome to follow!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326086711&siteId=291194637