Android Inter-process communication (IPC) Mechanism Binder Brief Introduction _android

Source: Internet
Author: User
Tags semaphore

In Android, each application is made up of activity and service that are likely to run in the same process or run in different processes. So how do you communicate with an activity or service that is not in the same process? This is the binder interprocess communication mechanism to be introduced in this article.

We know that the Android system is based on the Linux kernel, and the Linux kernel inherits and is compatible with the rich Unix system interprocess communication (IPC) mechanism. There are traditional pipelines (Pipe), Signal (Signal) and Trace (trace), which can only be used between the parent process and the subprocess, or between the sibling process, and then the command pipeline (Named Pipe), which is no longer confined between the parent-child process or the sibling process In order to better support transaction processing in business applications, in At&t Unix System V, there are three more interprocess communication mechanisms called System V IPC, message queues (messages), Shared memory (Share Memory) and Semaphore (semaphore); Later, BSD Unix expanded the system V IPC mechanism, providing a interprocess communication mechanism called socket. For further details on these interprocess communication mechanisms, it is recommended to refer to the Linux kernel source scenario analysis in the Android learning launch article.

However, the Android system does not use the above mentioned various interprocess communication mechanism, but the adoption of binder mechanism, because of the poor performance of mobile device hardware, low memory characteristics? Unknown. Binder is not actually a set of new interprocess communication mechanisms proposed by Android, which is based on Openbinder. Openbinder was first developed by Be Inc., followed by Palm Inc. Now Openbinder's author, Dianne Hackborn, works at Google and is responsible for the development of the Android platform.

As mentioned earlier, Binder is a interprocess communication mechanism that is similar to the COM and CORBA Distributed component architecture, and, more generally, provides remote procedure call (RPC) functionality. From the English literal point of view, Binder has the meaning of the binder, then it put what thing together? The binder mechanism of the Android system consists of a system component, client, server, Service Manager, and binder driver, where client, server, and service Manager run in user space , the binder driver runs kernel space. Binder is a binder that glues these four components together, where the core component is the binder driver, and Service Manager provides the ability to assist management, where client and server are binder drives and service The manager provides the infrastructure for communication between Client-server. Service Manager and Binder drivers are already implemented on the Android platform, and developers simply implement their own client and server components as per the specification. Simple to say, difficult to do, for beginners, the Android system is the most difficult to understand the binder mechanism, and the binder mechanism, whether from the system development or application development point of view, are the most important components of the Android system, therefore, It is necessary to have a deeper understanding of how binder works. To learn more about the way binder work, the best way is to read binder related source code, the originator of Linux Linus Torvalds once said a famous saying rtfsc:read the fucking Source code.

Although reading binder source code is the best way to learn the binder mechanism, but also must not be able to fight unprepared, because the binder of the relevant sources is more boring and more difficult to understand, if it can be supplemented by some theoretical knowledge, it is better. Less gossip, online information about the binder mechanism is still a lot of, here would not want to write it again, strongly recommend the following two articles:

The binder mechanism of Android

Android Binder Design and implementation-Design chapter

The binder mechanism of Android in simple terms from the scenario, the article delves into the interrelationships between the three components of binder in user space, client, server, and service Manager, Android Binder design and implementation of the paper is a detailed introduction of the kernel space of the binder driver data structure and design principles. Thanks to these two authors for bringing us such good binder learning materials. To summarize, the relationship between the four components of the Android binder mechanism, client, Server, Service Manager, and binder driver, is shown in the following illustration:

1. Client, server, and Service Manager implemented in user space, binder driver implementation in kernel space

2. The binder driver and service Manager are implemented on the Android platform, and developers only need to implement their own client and server in user space

3. The binder driver provides device files/dev/binder interact with user space, and Client, server, and Service Manager communicates with the binder driver through the open and IOCTL file action functions

4. interprocess communication between client and server is implemented indirectly through the binder driver

5. Service Manager is a daemon that manages the server and provides client with the ability to query the server interface

At this point, the binder mechanism has finally had a perceptual understanding, but still feel not well from top to bottom through the entire IPC communication process, so, the following four scenarios to analyze the binder source code 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 does server and client obtain the Service Manager interface? That is how the Defaultservicemanager interface is implemented.

3. How does the server start its own service? How does Service Manager serve the server during server startup? That is how the Iservicemanager::addservice interface is implemented.

How does the 4 Service Manager serve the 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 binder related source code of user space to kernel space. Why isn't there a scenario where the client and server are communicating between processes? This is because Service Manager, as a daemon, also acts as a server role. So, as long as we can understand the third and fourth scenarios, we understand how client and server in the binder mechanism communicate with each other through binder drivers.

In order to facilitate the description of the principle and implementation of the binder mechanism of the Android system interprocess communication, in the next four articles, we will introduce the implementation of the binder mechanism based on the C + + language, but when we develop the application in the Android system, we are all based on the Java language, Therefore, in the last article, we will detail the implementation of the Android interprocess communication Binder mechanism in the application framework layer Java interface:

5. The Android system interprocess communication Binder mechanism in the application framework Layer Java Interface Source code analysis.

The above is about the binder mechanism of a simple introduction, follow-up to continue to collate relevant information, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.