Android interprocess communication---Bind's past

Source: Internet
Author: User
Tags semaphore

Before analyzing the bind mechanism, I found that an article had been explained very clearly and raised a lot of questions.

Address: http://my.oschina.net/keeponmoving/blog/64218

I. What are the methods of inter-process communication between Linux systems?     1.socket; 2.name pipe name pipe; 3.message queue message queue; 4.singal semaphore; 5.share memory shared; two. What is the way the Java system communicates? 1.socket; 2.name pipe; Iii. What is the mode of communication for Android systems? Binder communication; What are the advantages of binder communication? High efficiency Five. What are the characteristics of binder communication? Is synchronous, not asynchronous; six. How is binder communication implemented?    1.binder communication is implemented through the binder driver of Linux,   2.binder communication operations like thread migration (thread migration), Two interprocess IPC looks like a process going into another process executing code and then returning;   3.binder user space with the results of execution to maintain an available thread pool for each process, the thread pool is used to process incoming IPC and to execute process local messages, Binder communication is synchronous rather than asynchronous. Seven. The Binder communication Implementation Essentials in Android:   1. Binder Communication in Android is based on the;   2 of the service-to-client working model. All processes that require ibinder communication must create a IBinder interface;   3. There is a process in the system that manages all system service:   4. Android does not allow users to add non-authorized system service;   5. Now that the source is open, we can modify some code to implement the purpose of adding the underlying system service;   6. For the user program, we also create the server, or the service is used for interprocess communication;   7. Activitymanagerservice manages all service creation and connection (connect),disconnect;   8 of the Java application layer. All activity is also initiated through this service to load the;   9. Activitymanagerservice is also loaded in the systems ServIn the CIE; eight. Android Service workflow   1.Android virtual machine starts before the system starts the Service Manager process;  2.service Manager opens binder driver and notifies binder Kernel Driver This process will act as system Service manager;  3. The process then enters a loop that waits for data from other processes to be processed.   4. After a user creates a system service, a remote ServiceManager interface is obtained via Defaultservicemanager, which allows us to call the AddService function to set the system The service is added to the Service Manager Process;  5. Then the client can obtain the IBinder object for the service that needs to be connected through GetService. This ibinder is a reference to the service Bbinder in binder kernel,  6. So service IBinder does not have the same two kernel objects in binder ibinder, Each client process also needs to open the binder driver. For the user program, we get this object to access the methods in the service object through binder kernel.   7.Client and service in different processes, in this way to achieve a similar thread of the migration of communication, to the user program when the service returned to the IBinder interface, access to the service in the method is like calling its own function.  

The following is a detailed description of binder.

1. Structure of binder

Binder is a tool to solve IPC and RPC. Android uses the Linux OS, which supports multi-process and multi-threaded. Also, for the Java app, a VM occupies a process. As a complete development framework, Android must support IPC. Binder is the way to solve the IPC problem, further, Binde also supports RPC.

The process has its own address space, and communication between different processes does not directly refer to the address. The general solution is that the sending process converts the data that needs to be transmitted in a certain format (Marshall) into binary/specific format data, to the receiving process, and the receiving process receives the binary form, the data in a particular format, and the inverse conversion (unmarshall) into the original data, and then uses it. This is the kind of step that Binder uses.

Binder uses the synchronous C/S model, S loop blocking on the receiving data operation, processing C data at any time, processing and sending back to c;c the request service data sent to S, blocking in the collection of S return data, after receiving data, continue their work.

The binder frame can be divided into 3 layers,

The bottom layer is the Linux OS and binder driver. Binder driver is essentially a shared memory between processes, and the data that each process will send to another process is written to driver, and the data sent by other processes is read from driver.

The middle layer is the framework implemented by CPP, which completes the data receiving and sending transformation, and the support of C/s process.

In fact, in the middle layer, the binder structure has been fully equipped. But Android uses Java as the development language for general apps. So we also need the support of JNI and the corresponding Java Binder class, which is the third layer above the middle tier: Jni/java

Assuming that c,s are all Java implementations, the app client collects the IPC data, passes through JNI, processes the pass, and writes the driver through the framework. The data is uploaded by driver, processed via the framework, through JNI, and uploaded to the app server.

For RPC support, a little bit of finesse is required, and direct transfer of function pointers is not available. For the sake of convenience of narration, first clarify a pair of concepts: native local and remote remote. The process that defines the RPC function is called local, and the process calling the RPC function is called the remote. RPC implementation is actually a remote definition of an RPC proxy, the far end of the proxy to do the local implementation of RPC, seemingly local function operation is generally used. And this proxy actually simply registers itself in the local handle and the import parameter sends the local, local according to the handle, knows is which remote process proxy, invokes the function, given the entrance parameter, after executes completes, returns the exit parameter to the far end proxy ( Because we have handle). The correspondence between handle and local functions in the implementation of binder is saved in driver.

2. Binder Driver

Binder driver is the basis of the binder mechanism and is the channel for implementing IPC.

The functional operation of binder driver and the framework and app is implemented by the IOCTL method. The most basic operation is data read and write operations. A read-write operation consists of two sub-operations: Write sub-operation and read-child operation. Driver maintains a data structure for each process and thread, with a list that hooks up data written by other processes and a semaphore. The write sub-operation is responsible for hooking the data to the list on the receiving process's corresponding data structure. The read operation is responsible for processing the data on the list in its own process and passing it back to the framework and app.

The second function of binder driver is to support RPC, which is to maintain the correspondence between the local function and the handle of the remote proxy. More complex is to maintain the correspondence between the handle of the two remote proxies, the two proxies are the same RPC proxy.

3. Binder Framework

The framework of binder is implemented by CPP, which is divided into two halves of the local local/s and the remote remote/c to describe.  The public class IBinder derives two subclasses, and Bbinder is used for local,bpbinder for remote.  The locally implemented class derives from Bbinder as the base class, processing after receiving the data, and sending the structure to the far end.  The remote proxy class uses Bpbinder to receive and send data locally. Further facilitates the development, introduced the IInterface class, the developer derives the subclass from the interface, defines the RPC operation which oneself needs.

Under IInterface's decoration, a local key class was derived from Bbinder bninterface<interface>; derived from bprefbase the remote key class containing the member Bpbinder object Bpinterface <INTERFACE>.

Thus, in the Century of creation, God said, ' To have light ', there is light. God saw that the light was good, and he parted the dark. God called the Light the day and called it darkness as night. There is a night, there is morning, this is the first day.

4. Binder Jni,java class and Aidl tools

The binder framework mechanism is used by Java and needs to be packaged. In addition to the JNI-related sections, Android also defines several related interfaces and classes in Java, ibinder,binder,binderproxy,iinterface,binderinternal.

Aidl tool is a convenient Java implementation of the C/s structure of a tool, developers write a simple interface to describe the IDL file, then aidl automatically generate local and remote binder class. Let developers focus on real-world functional development without the need to expend unnecessary effort on the binder mechanism.

5. What is parcel?

To facilitate the manipulation of data passed between the IPC, Binder introduces the concept of parcel. Parcel can think of as a courier company's box, need to pass the various types of data are packaged into the parcel class, Binder is responsible for passing parcel objects, the receiving end from the parcel to solve the data. Such a mechanism reduces the complexity of the various data types on delivery, and can be easily extended by increasing the data type of the package/unpack parcel.

Parcel already supports accommodating basic data types and some composite data types.  At the framework level, parcel provides a flattenable base class that can extend the data types that parcel accommodates. At the Java parcel level, parcel provides a parcelable interface that extends the data types that are accommodated by parcel.

6. Service that relies on binder

IPC/RPC has been solved by the binder mechanism, the only problem with the service is how the service allows the client that wants to use the service to recruit the service. The solution is Service Manager. Service Manager is a special process in which each service is registered to Service Manager, and the client can query from Service Manager for the service it needs to use.

Android interprocess communication---Bind's past

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.