An overview of the

Binder level Android is a Linux kernel based operating system, Binder level Linux, process isolation, and virtual address:

Process isolation

Process isolation is a set of different hardware and software techniques designed to protect processes in an operating system from interfering with each other. This technique is used to prevent process A from writing to process B. Process isolation implementation, using virtual address space.

A virtual address

A virtual address is an abstract description of the entire memory. It is relative to physical memory and can be directly understood as “unreal” or “false” memory. For example, a 0x08000000 memory address does not correspond to the address element 0x08000000-1 in the large array of physical addresses; This is the case because modern operating systems provide an abstraction of memory management, known as virtual memory. The process takes an address in virtual memory, and the operating system assists the hardware in “translating” it into a real physical address.

The body of the

Linux process isolation prevents direct communication between processes. Is there any other way for Linux to communicate? If so, why use Binder as an IPC communication mode? With these questions, the Binder mechanism is analyzed step by step.

The IPC principle

The IPC mechanism

The Client process communicates with the Server process by using the shared kernel memory space between the processes to complete the underlying communication work. The Client and Server processes often use iocTL and other methods to interact with the driver of the kernel space.

The IPC types

The IPC types

  • 1. Pipe: Pipe can be used for communication between related processes. Named Pipe overcomes the restriction that pipes have no name, so it allows communication between unrelated processes in addition to the functions of pipes.
  • 2. Signal: A Signal is a complex communication method used to notify a receiving process of an event. In addition to interprocess communication, a process can also send signals to the process itself
  • 3. (Message) queue (Message queue) : A Message queue is a linked table of messages, including Posix Message queue – column System V Message queue. A process with sufficient permissions can add messages to the queue, and a process with read permissions can read messages from the queue. Message queue overcomes the disadvantages of signal carrying less information, pipe carrying only unformatted byte stream and limited buffer size.
  • 4. Shared memory: Enables multiple processes to access the same memory space, which is the fastest form of IPC available. It is designed for the low efficiency of other communication mechanisms. Often used in conjunction with other communication mechanisms such as semaphores to achieve synchronization and mutual exclusion between processes.
  • 5. Socket: a more general interprocess communication mechanism, which can be used for interprocess communication between different machines.

Principle of Binder

Binder is the Binder mechanism that binds Linux processes to communicate with each other in various ways. Binder is the main mechanism that binds Linux processes to communicate with each other.

  • Binder is intuitively an Android class that inherits the IBinder interface
  • 2. Binder is a cross-process communication method in Android from an IPC perspective. Binder can also be understood as a virtual physical device driven by /dev/binder, which does not exist in Linux
  • 3. From the perspective of the Android Framework, Binder is the bridge between ServiceManager managers (ActivityManager, WindowManager, etc) and corresponding ManagerService
  • 4. From the Android application layer, Binder is the communication medium between client and server. When you bindService, the server will return a Binder object containing the service invocation of the server, through which the client can obtain services or data provided by the server. Services here include normal services and AIDL-based services
Principle of Binder

Android’s Binder mechanism consists of a system of components called Client, Server, Service Manager, and drivers. Where Client, Server, and Service Manager run in user space, Binder drivers run in kernel space. Binder is the Binder that binds these four components together. The core component is the Binder driver. Service Manager provides management assistance. Client and Server communicate with each other over the infrastructure provided by the Binder drivers and Service Manager. The Service Manager and Binder drivers are already available on the Android platform, and developers just need to implement their own Client and Server components according to the specification.

Binder architecture

Binder architecture

  • Java application layer: For the upper application by calling amp. startService, can not care about the bottom layer, after layer upon layer of calls, will eventually call ams. startService.
  • Java IPC layer: Binder communication is based on C/S architecture. Binder client class BinderProxy and service class Binder have been designed in the Infrastructure of Android system.
  • Native IPC layer: For Native layer, Binder and BBinder(as well as JavaBBinder) can be used directly if you need to use binders (such as media), and the communication of the previous Java IPC layer is also based on this layer.
  • The first three Binder drivers run in user space. The memory resources of user space are not shared. Each Android process can only run in its own virtual address space, but the Kernel space is shared. The core of real communication lies in the Binder Driver.

Binder communication process

Binder communication process

  • The Server is registered with the ServiceManager. – To call the getName method of the Server, the Client needs to get the Server object first. However, SM does not return the real Server object to the Client. Instead, SM returns a Proxy object of the Server to the Client, that is, Proxy.
  • Then, the Client calls the Proxy’s getName method. SM calls the Server’s getName method for him and returns the result to the Client.

IPC mode comparison

Although Android has many IPC methods, Binder is still used to communicate between threads, mainly for performance and security and stability reasons.

  1. Pipes: Allocate a page-size memory at creation time, and the cache size is limited;
  2. Message queuing: Information is copied twice, extra CPU consumption; Not suitable for frequent or informative communication;
  3. Shared memory: no replication, shared buffer directly attached to the process virtual address space, fast; However, the synchronization problem between processes cannot be realized by the operating system and must be solved by each process using the synchronization tool.
  4. Socket: As a more general interface, low transmission efficiency, mainly used for communication between different machines or across networks;
  5. Semaphore: Often used as a locking mechanism to prevent other processes from accessing a shared resource while one process is accessing it. Therefore, it is mainly used as a means of synchronization between processes and between different threads within the same process.
  6. Signal: not suitable for information exchange, more suitable for process interrupt control, such as illegal memory access, kill a process and so on;

The reason why Android adopts Binder mechanism for communication is mainly based on performance, security and stability. Binder is based on client-server communication mode, and the transmission process only needs one copy. UID/PID identity is added for sending and sending. Support both real name and anonymous Binder, high security;

The resources

Gityuan.com/2015/10/31/… Segmentfault.com/a/119000000…