Seven, Android in IPC mechanism (4) analysis of the underlying Binder ---

Reprinted:
https://blog.csdn.net/universus/article/details/6211589  &  https://blog.csdn.net/carson_ho/article/details/73560642

Summary

    Binder is one way among Android system process communication (IPC) is. Although Linux system such as pipes, message queues, Socket and other IPC way, but the Android system is still dependent on Binder to achieve inter-process communication, explained Binder have an unparalleled advantage. Binder and understand the underlying IPC it with the traditional way of comparison helps us to understand in depth the principles and optimize the performance of the IPC. The combination of the above two articles in this paper, simple to conduct some understanding of the underlying Binder.

Introduction (Why Android system introduced Binder interprocess communication?)

    Based Client-Server communication is widely used in the Internet, database access, and embedded devices. Smart phones (especially Android system) in order to provide a rich variety of features to application developers, this means of communication is everywhere, such as media players, audio and video capture and a variety of sensors in the phone by a different  Server is responsible for managing , developers to develop applications that only require a Client-side with these  Server to establish a connection can easily use these services. Because Client-side and Server-side in separate processes, so this issue will be designed to inter-process communication, and linux systems Socket support such inter-process communication, but the transmission performance and comprehensive security is, Android system Binder communication mechanism was introduced as one of Android in IPC mode, the following look at the difference between the traditional Socket and other cross-process communication and Binder:

1. Transmission Properties

    In the Linux system, a process space that contains the user space and kernel space. Between different processes, data space for each user process private space, other processes can not access; and kernel space as a shared space, that is, all processes share the same kernel space, they can access the spatial data.

    In one process, the user space and kernel space can exchange data through two following system functions:

1. copy_from_user()  // 将用户空间的数据拷贝到内核空间
2. copy_to_user()    // 将内核空间的数据拷贝到用户空间

Icon:

FIG traditional IPC as follows:

    Binder mechanism and requires only one copy of the data, the higher the transmission efficiency. (Specific Analysis below).

2. Security

    Android system as an open platform, with many developers, application wide variety of sources, so ensure the safety of terminal equipment is very important. For example, if an application very easily be able to obtain information to other applications (such as contacts, text messages), it will lead to leakage of user privacy and so on. IPC does not have any traditional security measures, rely entirely on upper layer protocols to ensure. The Binder mechanism uses Android system provides each process UID / PID as a logo to identify the identity and improve the security of inter-process communication.

    For these reasons, Android system established a new IPC mechanism to meet the system requirements for communication, transmission performance and security, which is Binder. Binder mechanism is based on Client- Server communication, as long as a copy of the data transmission and authentication by UID / PID process safe.

Object-oriented Binder

    Binder using Client-Server Communication: Server provides a process such as video / audio decoding, video capture, network connection and other services, other processes can initiate service requests to a Client Server, a connection to obtain the appropriate service. To achieve the Client-Server communication must implement two things: First Server must determine the address of the access point or as a Client request identifier; Second Protocol Server and Client terminal side data communication. Network communication protocol such as the Server Server access point is an IP host address + port number, c is a data communication protocol is TCP. In terms of the Server, Binder can be seen as an access point or address to achieve a particular service provided by the Server, Client through the "address" to the Server sends a request to use the service; for Client concerned, Binder can see Server is the gateway into the pipe inlet, and a Server in order to communicate must first build the pipeline and get this pipe inlet.

    Unlike the IPC other, Binder using object-oriented thinking be described as access points of its inlet and Binder in the Client: Binder Server Object is an entity located in the object it provides a method to achieve request service as a member of the class method of the same. Client throughout the entry point to the Binder object can be seen as a "pointer", once acquired this "pointer" can call the object's method to access Server. In the Client opinion, by Binder "pointer" and call methods that provide local call any other object through the "pointer" method there is no difference, although the former at a remote entity Server, the latter entity is located in local memory. "Pointer" is the term C / C ++ is, but more often said that the reference, i.e., by reference Binder Client Access Server. Another term of art and the software "handle" may also be used to describe the present embodiment in Binder Client. From the point of view of communication, the Client Binder can also be seen as a "proxy" Binder entities Server, to provide services for local representatives in the distal Client Server.

    Object-oriented thinking will be the introduction of inter-process communication to call the object method by reference to a Binder object, but it is unique in that Binder object is an object that can be cross-referenced process, it's an entity located in a process, and it was cited among the various processes throughout the system. The most attractive is that the references and java, references, either strongly typed, can also be a weak type, but also can be passed from one process to other processes, so that everyone can access the same Server, like an object assigned to another reference or references the same. Binder blurred process boundaries, played down the inter-process communication process, the whole system seems to run into the same object-oriented programming. Binder object and a plurality of kinds of reference as if each application of an adhesive glue, which in English is meant Binder --- "adhesive";

    Of course, it is just for object-oriented application, the kernel and drivers for Binder as other modules are used, there is no concept of C language classes and objects. Binder driver provides the underlying support for object oriented inter-process communication methods.

Binder communication model

    Binder framework defines four roles: Server, Client, Service Manager and Binder drive . Among Server, Client, ServiceManager run in user space, Binder drivers run in kernel space.

As shown below:

Binder driver:

    It is a virtual device driver. Although the name of the drive, and in fact nothing to do with hardware, but hardware implementations and device drivers are the same, such as its work in kernel mode, providing open (), mmap (), poll (), ioctl (), etc. standard file operations, the characters in the drive device in the device registration apparatus misc directory (/ dev), the user can access it through the / dev / binder. Binder drive is connected Server process, Client Service Manager processes and bridge process. Its interior is through memory-mapped (ie call mmap () function), through the establishment of the received data buffer in kernel space and the establishment Server process, the mapping between the Client and process the received data buffer that was created in order to achieve efficient cross-process communication. It is also the core Binder mechanisms underlying implementation.

Server, Client, Service Manager and Binder-driven role models and principles in the following figure:

    You can see from the chart, in fact, the Binder mechanism to copy data only once, that user space processes Client -> kernel buffer, the rest are interacting directly through the mapping relationship established Binder driven by a shared object, so that high transmission efficiency Binder mechanism. These are the underlying mechanisms Binder Android system simple implementation process.

Note: When using, Binder drivers and Service Manager processes belonging Android infrastructure, that system has achieved good; and Client Server processes and Android processes belonging to the application layer, so we need to realize themselves. So we have to complete the cross-process communication mechanism Binder by implementing processes and Client Server process.

Guess you like

Origin blog.csdn.net/yz_cfm/article/details/90245193