This article is compiled from: Binder Series

First: Starting an Activity that belongs to a different process triggers Application onCreate() multiple times:

IPC principle, not BINDER principle:

Each Android process can only run in its own virtual address space. It corresponds to a 4GB virtual address space. 3GB is the user space and 1GB is the kernel space. Of course, the size of the kernel space can be adjusted by parameter configuration. User space and different processes are not shareable with each other, while kernel space is shareable. Then the Client process communicates with the Server process, relying on the kernel space that the two processes can share. The interaction between the Client and Server processes can be achieved through methods such as IOCtl.

That is, since user space between processes is not shared, binders communicate through shared kernel space (as is the case with all IPC).

Principle of BINDER

As you can see from the figure, both registering the Service and obtaining the Service are inseparable from Service Manager. The ServiceManager is the master of the Binder communication mechanism and is the Binder’s daemon.

After the Service Manager is started, the Client and Server must obtain the Service Manager interface before communicating with each other.

Difference between BpBinder and BBinder

The role of ServiceManager

The core work of a Servicemanager is to register services and query services.

ServiceManger startup

It is started by the init process calling init.rc. When we do security, we know that services that are started by the init process cannot be terminated, nor can children that are forked by the init process. See this article.

Therefore, various services of the system are initialized and registered with the ServiceManager during startup.

ServiceManager is the init process by parsing the init. Rc file created, its corresponding executable program/system/bin/ServiceManager, the corresponding source file is service_manager. C, The process is called/system/bin/servicemanager.

The following three lines of code are part of the code that calls WindowManger to implement a floating window.

WindowManager wm = (WindowManager)getSystemService(getApplication().WINDOW_SERVICE);  
View view=LayoutInflater.from(getApplication()).inflate(R.layout.float_layout, null);  
wm.addView(view, layoutParams);
Copy the code

These three lines of code correspond to the three processes of registering (addService), getting (getService), and consuming the service. The third line calls the WindowManger addView function running on the systemServer process.

The registration service

AddService creates a binder_node in the process where the service resides and a binder_ref in the Servicemanager process. Where the desc of binder_ref is unique within the same process:

Request service

The getService process queries the Servicemanager process for a specified service. When binder_transaction() is executed, the process in which the service is requested is identified.