正文
Binder
这个名词大家或许有些许陌生,但是在 Android 系统中却具有至关重要的作用,作为 Android 系统所特有的特征之一,无论是系统服务的调度还是跨进程通信中,处处皆可见其身影,接下来我会从四个层面逐一介绍 Binder(简称 Binder 的 WH2W),即:
-
WHAT :
What Is Binder?这部分从Binder的引入以及背景来粗浅地为 Binder 下定义;
-
HOW :
How To Use Binder? 知道 Binder 的概念并不够,还要知道怎么用它,这一部分从 Binding Service(绑定服务)入手,浅析 Binder 的应用场景,进而验证第一部分的概念;
-
HOW :
How It Works? 知道了 Binder 的概念和使用场景,接下来这部分就是讲述它是如何在这些场景中发挥作用的。
-
WHY:
众所周知,Android 基于 Linux 内核,而 Linux 本身具有众多跨进程通信机制,为什么非得使用 Binder?这部分主要比较 Binder 与其他跨进程机制的区别,以说明使用 Binder 的必要性。
这一部分中我们讲介绍 Binder 相关的一些 Linux 基础知识,为了便于大家更好的理解,为中英文对照部分,各位英文好的看英文,英文不好的看中文。
Multitasking,Processes and Threads
Multitasking is the ability to execute multiple instances of programs or processes at the same time. An operating system therefore creates for every binary exe-cutable le a certain memory frame with its own stack, heap, data and shared mapped libraries. It also assigns special internal management structures. This is called a process .The operating system must provide fair proportioning, because only one process can use the CPU at the same time. All processes must be interruptible. The operating system sends them to sleep or wakes them on their time slot. This work is done by a scheduler , supplying each process with an optimal time slot.A thread is a process without own address space in memory, it shares the address space with the parent process. Processes can have child threads, and a thread must be assigned to a process.
上述英文中描述了对任务,进程和线程的基本概念,学过操作系统的小伙伴对这些应该很熟悉了,我把这部分英文翻译成如下三句定义:
-
多任务 :
多任务是操作系统同时执行多个程序或进程实例的能力;
-
进程 :
操作系统为一个二进制可执行文件创建了一个载有该文件自己的栈,堆、数据映射以及共享库的内存片段,还为其分配特殊的内部管理结构。这就是一个进程。操作系统必须提供公平的比例,因为在同一时间只有一个进程在使用CPU。所有进程都是可以中断的。操作系统在流水线上唤醒或中断进程,这项工作是由调度器完成,提供每个流水线的最佳时段;
-
线程 :
线程是没有自己内存地址空间的过程,它与父进程共享内存地址空间。进程可以有多个子线程,一个线程必然拥有唯一的一个进程
Process Isolation(进程隔离)
Due to security and safety reasons, one process must not manipulate the data of another process. For this purpose an operating system must integrate a concept for process isolation. In linux, the virtual memory mechanism achieves that by assigning each process accesses to one linear and contiguous memory space. This virtual memory space is mapped to physical memory by the operating system.Each process has its own virtual memory space, so that a process cannot manip-ulate the memory space of another process. The memory access of a process is limited to its virtual memory. Only the operating system has access to physical memory。The process isolation ensures for each process memory security, but in many cases the communication between process is wanted and needed. The operating system must provide mechanisms to approve interprocess communication。