App developed by Binder wrote to the principle of series

Foreword

Binder grasp the underlying principle is the cornerstone principle of the system is also essential advanced knowledge of senior engineers, this article will not introduce too much Binder principle, but to explain the study to master the knowledge points before Binder needed.

I think the secret to learn Binder There are two main principles:

  1. Learn pre-knowledge Binder principle involved, such as the type of IPC mechanism of Linux and other knowledge points.
  2. The Binder principle divided into several parts, separately.

The purpose of this article is to help you complete the first point, the second point will be described in detail in subsequent series.

IPC mechanism 1.Linux species and Android

IPC full name inter-Process Communication, meaning inter-process communication, refers to a process of exchange of data between two processes. In Android and Linux has its own IPC mechanisms are here to introduce the next.

Kind of IPC mechanism in the 1.1 Linux

Linux provides a lot of inter-process communication mechanism, the main pipe (pipe), the signal (Sinal), semaphores (semophore), the message queue (the Message), shared memory (Share Memory), a socket (the Socket) the like.

pipeline

Pipeline is the communication mechanism between inherited by Linux Unix process over there, it is an important early Unix communication mechanism. The main idea of ​​the pipeline is to create a shared file in memory, so that both parties communicate using the shared file to transmit information. The shared file is rather special, it does not belong to the file system and exists only in memory. Another point, the pipe is used in half-duplex communication mode, data can only flow in one direction.

Simple model as shown in FIG.

App developed by Binder wrote to the principle of series

signal

Is an analog signal to interrupt mechanism on the software level, is an asynchronous communication, process does not have to wait for the signal to reach by any operation. The signal can be direct interaction between user space processes and kernel, user-space processes may be notified of system events which occurred with a signal. Signals does not apply to the exchange of information, more suitable for the process interrupt control.

signal

Semaphore is a counter used to control multiple processes access to shared resources. It is often used as a locking mechanism to prevent access to shared resources is a process, other processes can also access the resource. Mainly as a means of inter-process and synchronization between different threads within the same process.

message queue

Message queue is a linked list of message having a specific format, identified by the message queue identifier stored in memory, one or more processes and allowing it to write and read the message. Information is copied twice, so for a large amount of information frequently or not a communication using message queues.

Shared memory

Multiple processes can be directly read and write a block of memory space, other communication mechanism for lower operating efficiency and design.

To exchange information between multiple processes, kernel set aside a piece of memory area can be accessed by the process needs to be mapped to its own private address space. This process can directly read and write memory without the need for a copy of the data, thus greatly improving efficiency.

Sockets

Sockets are among more fundamental process communication mechanism that is different from the other way, sockets can be used for inter-process communication between different machines.

IPC mechanism in the 1.2 Android

Android system is based on Linux kernel, the Linux kernel basis, and expand the number of IPC mechanism. In addition to the Android system supports a socket, also support serialization, Messenger, AIDL, Bundle, file sharing, ContentProvider, Binder and so on. Binder will be introduced in the back, in front of the first to understand the IPC mechanism.

Serialization

Serialization refers Serializable / Parcelable, Serializable is provided a Java serialization interface is an empty interface that provides the standard serialization and deserialization operation object. Android Parcelable interfaces are serialized in a manner more suitable for use in the Android platform, with too much trouble, a very high efficiency.

Messenger

Messenger frequency of use in Android application development is not high, it can pass Message objects in different processes, adding the data we want to pass on the Message will be passed in the data between processes. Messenger is a lightweight IPC program and AIDL is encapsulated.

AIDL

AIDL full name of the Android interface definition Language, namely Android Interface Definition Language. Messenger is a serial way to deal with the information sent by the client, if there are a large number of messages sent to the server, the server is still a process and then a response to the client is clearly inappropriate. Another point, Messenger for data transfer but did not meet the cross-process method call between processes, this time you need to use a AIDL.

Bundle

Bundle implements Parcelable interface, so it can be easily transferred between different processes. Acitivity, Service, Receiver data transfer is performed by the Intent Bundle.

File Sharing

Two processes to share data by reading and writing the same file, the file can be shared text, XML, JOSN. File sharing is suitable for less demanding synchronization between the data communication process.

ContentProvider

ContentProvider to store and retrieve data to provide a unified interface that can share data between different applications, is itself suitable for inter-process communication. ContentProvider underlying implementation is Binder, but easier to use than many AIDL. Many operating systems have adopted ContentProvider, such as contacts, audio and video, these actions themselves inter-process communication.

IPC communication theory and Binder of 2.Linux

Before Linux talked about the process of communication theory, we need to understand a few concepts in Liunx.

App developed by Binder wrote to the principle of series

Kernel space and user space

When we are exposed to Liunx, will inevitably hear two words, User space (user space) and Kernel space (kernel space), then they mean what is it?

To protect user processes can not directly manipulate kernel, ensure the safety of the kernel, the operating system logically divided from the virtual space to the user space and kernel space. Linux operating system to the highest byte 1GB for kernel called kernel space, 3GB low byte for each process uses, called user space.

Kernel space is running Linux kernel space, user space is the space of the user running the program. For safety, they are isolated, even if the user program crashes, the kernel will not be affected. Kernel space data can be shared between processes, and the user space can not. For example, in FIG space user process A and the user is not shared space B process.

Process Isolation

Process isolation means that a process can not directly operate or access another process. A process that is not directly access the process data of B.

System calls

Users need access to space kernel space, you need the help of system calls to achieve. System call is the only way to access user space kernel space, ensures that all resource access are carried out under the control of the kernel, the user program to avoid unauthorized access to system resources, improve system security and stability.

User space processes A and B may interact through the following system functions and kernel space.

  • copy_from_user: copy data user space to kernel space.
  • copy_to_user: copy data kernel space to user space.

Memory map

Because the application can not directly operate the hardware address of the device, the operating system provides a mechanism: memory mapping, mapping device address to process virtual memory area.

For example, if the user disk space needs to read the file, if do not use memory-mapped, you need to establish a kernel space in the page cache, the page cache to copy the files on the disk, and then copy the user space pages cached files, which We need two copies.

With memory mapped, as shown in FIG.

App developed by Binder wrote to the principle of series

Due to the new virtual memory area, the disk files and virtual memory area can be directly mapped, one less copy.

The full name of the memory map Memory Map, to achieve memory-mapped by mmap system call functions in Linux. An area of ​​memory mapping user space to kernel space. After the mapping relationship, the user can modify the responses to this memory area directly to the kernel space, and vice versa. Memory mapping data can reduce the number of copies, efficient interaction between user space and kernel space.

2.1 Linux IPC communication principle of

Understand concepts Liunx in, you can learn the IPC communication principle of Linux, as shown below.

App developed by Binder wrote to the principle of series

内核程序在内核空间分配内存并开辟一块内核缓存区,发送进程通过copy_from_user函数将数据拷贝到到内核空间的缓冲区中。同样的,接收进程在接收数据时在自己的用户空间开辟一块内存缓存区,然后内核程序调用 copy_to_user() 函数将数据从内核缓存区拷贝到接收进程。这样数据发送进程和数据接收进程完成了一次数据传输,也就是一次进程间通信。

Linux的IPC通信原理有两个问题:

  1. 一次数据传递需要经历:用户空间 --> 内核缓存区 --> 用户空间,需要2次数据拷贝,这样效率不高。
  2. 接收数据的缓存区由数据接收进程提供,但是接收进程并不知道需要多大的空间来存放将要传递过来的数据,因此只能开辟尽可能大的内存空间或者先调用API接收消息头来获取消息体的大小,浪费了空间或者时间。

2.2 Binder的通信原理

Binder是基于开源的OpenBinder实现的,OpenBinder最早并不是由Google公司开发的,而是Be Inc公司开发的,接着由Palm, Inc.公司负责开发。后来OpenBinder的作者Dianne Hackborn加入了Google公司,并负责Android平台的开发工作,顺便把这项技术也带进了Android。

Binder是基于内存映射来实现的,在前面我们知道内存映射通常是用在有物理介质的文件系统上的,Binder没有物理介质,它使用内存映射是为了跨进程传递数据。

App developed by Binder wrote to the principle of series

Binder通信的步骤如下所示。

1.Binder驱动在内核空间创建一个数据接收缓存区。

2.在内核空间开辟一块内核缓存区,建立内核缓存区和数据接收缓存区之间的映射关系,以及数据接收缓存区和接收进程用户空间地址的映射关系。

3.发送方进程通过copy_from_user()函数将数据拷贝 到内核中的内核缓存区,由于内核缓存区和接收进程的用户空间存在内存映射,因此也就相当于把数据发送到了接收进程的用户空间,这样便完成了一次进程间的通信。

整个过程只使用了1次拷贝,不会因为不知道数据的大小而浪费空间或者时间,效率更高。

3.为什么要使用Binder

Android是基于Linux内核的 ,Linux提供了很多IPC机制,而Android却自己设计了Binder来进行通信,主要是因为以下几点。

性能方面

性能方面主要影响的因素是拷贝次数,管道、消息队列、Socket的拷贝次书都是两次,性能不是很好,共享内存不需要拷贝,性能最好,Binder的拷贝次书为1次,性能仅次于内存拷贝。

稳定性方面

Binder是基于C/S架构的,这个架构通常采用两层结构,在技术上已经很成熟了,稳定性是没有问题的。共享内存没有分层,难以控制,并发同步访问临界资源时,可能还会产生死锁。从稳定性的角度讲,Binder是优于共享内存的。

安全方面

Android是一个开源的系统,并且拥有开放性的平台,市场上应用来源很广,因此安全性对于Android 平台而言极其重要。

传统的IPC接收方无法获得对方可靠的进程用户ID/进程ID(UID/PID),无法鉴别对方身份。Android 为每个安装好的APP分配了自己的UID,通过进程的UID来鉴别进程身份。另外,Android系统中的Server端会判断UID/PID是否满足访问权限,而对外只暴露Client端,加强了系统的安全性。

语言方面

Linux是基于C语言,C语言是面向过程的,Android应用层和Java Framework是基于Java语言,Java语言是面向对象的。Binder本身符合面向对象的思想,因此作为Android的通信机制更合适不过。

从这四方面来看,Linux提供的大部分IPC机制根本无法和Binder相比较,而共享内存只在性能方面优于Binder,其他方面都劣于Binder,这些就是为什么Android要使用Binder来进行进程间通信,当然系统中并不是所有的进程通信都是采用了Binder,而是根据场景选择最合适的,比如Zygote进程与AMS通信使用的是Socket,Kill Process采用的是信号。

4.为什么要学习Binder?

Binder机制在Android中的地位举足轻重,我们需要掌握的很多原理都和Binder有关:

  1. 系统中的各个进程是如何通信的?
  2. Android系统启动过程
  3. AMS、PMS的原理
  4. 四大组件的原理,比如Activity是如何启动的?
  5. 插件化原理
  6. 系统服务的Client端和Server端是如何通信的?(比如MediaPlayer和MeidaPlayerService)

上面只是列了一小部分,简单来说说,比如系统在启动时,SystemServer进程启动后会创建Binder线程池,目的是通过Binder,使得在SystemServer进程中的服务可以和其他进程进行通信了。再比如我们常说的AMS、PMS都是基于Binder来实现的,拿PMS来说,PMS运行在SystemServer进程,如果它想要和DefaultContainerService通信(是用于检查和复制可移动文件的系统服务),就需要通过Binder,因为DefaultContainerService运行在com.android.defcontainer进程。

还有一个比较常见的C/S架构间通信的问题,Client端的MediaPlayer和Server端的MeidaPlayerService不是运行在一个进程中的,同样需要Binder来实现通信。

可以说Binder机制是掌握系统底层原理的基石。根据Android系统的分层,Binder机制主要分为以下几个部分。

App developed by Binder wrote to the principle of series

上图并没有给出Binder机制的具体的细节,而是先给出了一个概念,根据系统的Android系统的分层,我将Binder机制分为了Java Binder、Native Binder、Kernel Binder,实际上Binder的内容非常多,完全可以写一本来介绍,但是对于应用开发来说,并不需要掌握那么多的知识点。

Android学习PDF+架构视频+面试文档+源码笔记

最后

感谢大家能耐着性子,看完我啰哩啰嗦的文章。

Here I also share a copy of your collection of finishing Android studying architecture PDF + Video + Interview + document source notes , as well as advanced technical architecture Advanced Brain Mapping, Android interview with thematic development, advanced materials advanced architecture to help you enhance Advanced Learning , but also saves everyone time online in search of information to learn, you can also share with close friends studying together

If you have a need, you can point like , follow me , then join the Android developer exchange group (820 198 451) to receive free

App developed by Binder wrote to the principle of series

App developed by Binder wrote to the principle of series

Guess you like

Origin blog.51cto.com/14573572/2444727