1. What is IPC
IPC stands for inter-process-communication, which means inter-process or inter-process Communication. Speaking of interprocess communication, we should first understand what a process is. According to the description of the operating system, the thread is the smallest unit of CPU scheduling, and the process generally refers to an execution unit, in mobile devices refers to a program or application; A process can contain multiple threads;
IPC usage scenarios:
For some reason, the application itself needs to adopt the multi-process pattern to implement. Possible causes are as follows: Some modules run in a separate process for special reasons. To increase the available memory for an application, multiple processes are required to obtain multiple memory Spaces.
The current application needs to obtain data from other applications
Android process architecture:
Each Android process is independent and consists of two parts, one user space and the other kernel space, as shown below
Why multiple processes?
By default, an Android application has only one process. Each process has its own independent resources and memory space. Other processes cannot access the memory and resources of the current process, and the memory allocated to each process is limited. If a process, a memory than the memory limit will be submitted to the OOM issues, many involves frequent operation of the big picture or need to read a large section of the data in memory when using, it is easy to quote OOM issues, in order to thoroughly solve the problems of the application memory, Android introduced the concept of multiple processes, it allows within the same application, To share the load of the main process, a separate process is created for some pages that occupy memory, such as Flash, video playing pages, and frequently drawn pages. Androidmanifest.xml: “Android: Process” : “Androidmanifest.xml” : “Androidmanifest.xml” : “AndroidManifest.xml” : “AndroidManifest.xml” : “AndroidManifest.xml” : “AndroidManifest.xml” : “AndroidManifest.xml” : “AndroidManifest.xml” : “Android: Process” : “Belongs to a private process. Other application components cannot run in the same process. A process that does not start with a colon (:) belongs to a global process. Other applications can run in the same process with this process by using ShareUID.
However, the multi-process mode has the following problems: 1. Static member mode and singleton mode are completely invalid; 2. Thread synchronization mechanism is completely invalid; 3
Therefore, in order to avoid these problems, There are various IPC mechanisms in Android, such as AIDL, Messenger, Socket, ContentProvider, but all of these mechanisms are implemented by Binder mechanism. What is Binder? Binder is the first thing you need to know about IPC in Android;
Principle of Binder mechanism
1. Binder mechanism
Binder is an IPC interprocess communication structure in Android. The entire design of a Binder is a C/S structure in which a client process accesses a server process proxy and communicates data to and from the proxy interface method. Binder is Android’s choice for two reasons. 1 is security. Each process will be assigned UID and PID by the Android system. Unlike the traditional method of adding UID in data, malicious processes cannot directly communicate with other processes, which improves the security of interprocess communication. 2 is efficient. IPC such as Socket requires 2 data copies per time, while Binder only needs 1 copy, which is very important in resource-constrained situations such as mobile phones.
Binder mechanism schematic diagram:
1. The client obtains the proxy object from the server. We need to clear the client process can not direct manipulation methods on the server, if you want to operating methods on the server, so there is a feasible solution is to build a server process on the client proxy objects, the proxy objects and the function of the server process, to access a method in the process of the service side, You only need to access the corresponding method in the proxy object; 2. The client invokes the proxy object to send a request to the server. 3. Proxy objects send user requests to server processes through Binder drivers; 4. The server process processes the request sent by the client, and returns the processing result to the server proxy object of the client through Binder driver. 5. The proxy object further returns the request result to the client process. Through the above five steps, a Binder communication is completed.
2. Composition of Binder mechanism
Binder mechanism consists of three parts: 1.Client; 2. 2.Server; 3. ServiceManager.
The relationship between the three components is as follows: 1.Client, Server, and ServiceManager are implemented in user space, while Binder drivers are implemented in kernel space. 2. With Binder, the Server process registers some Services with the ServiceManager. The ServiceManager manages these services and provides interfaces for clients. 3. To communicate with a specific Service, the Client process must obtain the Service information from the ServiceManager. The Client process establishes communication with the Server process where the Service resides based on the Service information. The Clent can then interact with the Service; 4.Binder drivers provide device files /dev/binder to interact with user space. Client, Server, and ServiceManager communicate with Binder drivers through open and IOCtl file manipulation functions. 5. The interaction between Client, Server and ServiceManager is based on the communication with Binder, so the mechanism of Binder can be explained through any relationship between the two.
Reference:
-
www.jianshu.com/p/b9b3051a4…
-
Blog.csdn.net/songzi1228/…