Why does Android inter-process communication sometimes use Binder and sometimes Socket

Before explaining this problem, let's take a look at the question "Why can't directly communicate between processes, but need to use Binder or Socket communication", in fact, as long as the students who know the virtual machine will probably understand this problem:

Since a process corresponds to a virtual machine instance in the Android system, and there is only one heap area and one method area in the Java virtual machine instance, which are shared by all threads, only the heap area and methods within the virtual machine can communicate directly. Inter-process communication belongs to the communication between virtual machine objects and cannot communicate directly. Of course, you should also understand that a machine has only one virtual machine to create multiple virtual machine instances to achieve multiple processes, that is, multiple applications. As for what is stored in the stack area, heap area, and method area, students need to carefully understand the virtual machine, so I won't go into too much detail here.

sunshineBoy

Okay, let’s get back to business, let’s talk about why Android inter-process communication sometimes uses Binder and sometimes Socket. In fact, this is also related to the Java virtual machine. The inter-process communication in the Android system uses Socket communication. The system_server process uses Socket when communicating with the zygote process. The system_server process passes the parameter ActivityThread through the LocalSocket connection to the serverSocket of the zygote process to notify the zygote process to fork the Android application process to execute the main() method. Since the zygote process is the parent process of all Android application processes, the system_server process is also the eldest son of the fork of the zygote process based on the Dalvik (ART) VM virtual machine instance, and the zygote process is based on the JVM virtual machine instance, so the zygote process and the system_server process are The parent-child relationship can only use socket communication (the main reason is that JVM is based on Java api, DVM is based on Android api, and there is no Binder driver in JVM). Binder communication is based on the communication between the two process brothers of the Dalvik (ART) VM virtual machine the way.

The init process is the user process that is the first to start one of the basic processes when the Linux system starts. After the Linux Kernel is started, the main() method of /system/core/init/Init.cpp is called to determine the user level. Other processes of the Android system are Is created and controlled by the init process;

The zygote process is the first child process that the init process forks. The service of the init process starts the process through socket communication, and then the zygote process calls the startVm function to create a JVM virtual machine instance. Registration JNI is mainly used to communicate with the Dalvik (ART) virtual machine. All Dalvik (ART) virtual machines are fork out of the zygote process incubator. The so-called fork is actually a copy of the zygote process, so that it can be created quickly and can share system and framework resources.

The system_server process is the first child process that the zygote process forks through the startSystemServer method. It is mainly used to start the services in the Android system, such as WindowManagerServer (WMS), ActivityManagerSystemService (AMS), PackageManagerServer (PMS), etc., PackageManagerServer is mainly installed The apk is used to parse AndroidManifest.xml to obtain Activity, Service, Broadcast Receiver, Content Provider, and permission information stored in the Binder, and display it on the desktop in a friendly way (in the form of shortcut icons); ActivityManagerSystemService manages all activities, WindowManagerService manages all window.

The Application process is the application process, which is the application we installed. Click on the desktop luncher icon, and notify the system_server process LocalSocket to connect to the zygote process serverSocket to pass the ActivityThread parameter through the ActivityManagerSystemService, and then the system_server process forks the current Application process and schedules the main() function of the ActivityThread to start the application Program, of course, this process is also the startup process of the App.

 

 

Guess you like

Origin blog.csdn.net/xhf_123/article/details/108471394