Understanding processes and IPC Architecture

Source: Internet
Author: User
1. What is IPC communication?
  • IPC (inter-process communication) communication that spans two different processes
  • Generally, all components (such as activity and Service) in an android application are executed in the same process. This communication in the same process, also known as short-range communication, means that two activities are executed in the same process.
  • Relatively: remote communication means that two components (activity or service) are executed in different processes respectively. The IPC communication between the two components is also called remote communication.
By default, the android program runs in the same process, but different components can be run in different processes by default.

IPC communication efficiency
  • When we start an application, a new process of zygote service incubation (fork) in the Android system is given to it, so that it (the app) load it into this new process.
  • Linux-based security restrictions and basic features of processes (address spaces of different processes are independent ). If two classes (or their objects) are executed in the same process, the communication between them is convenient and fast.
  • However, when they are executed in different processes, communication between the two is a cross-process communication (IP address conversion) between IPC, which is not as convenient and slow as the former.
2. Android process concept

  • A process is an independent execution space and is not infringed by programs in other processes. This protection method is an important security mechanism for Android.
  • In the android process, there is a virtual machine (VM) object that can execute Java code and guide JNI local program execution, implement communication between Java and C/C ++; for example:

Each process includes a VM object, main thread, MQ, and logoff.

  • At the birth of each process, a main thread will be created, and a logoff class object and a message queue data structure will be created. Every time the main thread finishes the task, it will execute the logoff class. At this time, we constantly observe the dynamics of MQ. For example:
The address space of different processes is independent.



  • The main task of the main thread is to process the event of the UI image. When a UI event occurs, the android framework will lose the message to MQ. When the main thread sees that MQ has new information, it retrieves the information and then executes the specific function based on the information content. After the execution is complete, continue to execute the logoff class and observe the dynamics of MQ.
  • When both classes are executed in the same process, the communication between the two is fast and convenient as long as a general function call is adopted. Once the two classes are executed in different processes, the communication between the two cannot take a general function call approach, so we have to adopt IPC communication.
3. Set IPC communication-use the androidmanifest. xml file

  • In the android framework, an application package usually contains multiple Java classes that can be executed in the same process; it can also be executed in different procedures.
  • Generally, a process only has one app. However, an app can occupy multiple processes.
  • For example, the androidmanifest. xml file of an app contains the following content:
//........     <activity android:name=".FirstActivity" >          <intent-filter>               <action android:name="android.intent.action.MAIN" />               <category android:name="android.intent.category.LAUNCHER" />           </intent-filter>      </activity>     <activity android:name=".LoadActivity"> </activity>     <service android:name=".LoadService" android:process=":remote" >           <intent-filter>              <action android:name ="com.misoo.pkm.REMOTE_SERVICE" />          </intent-filter>     </service></manifest>
Based on this configuration file, deploy various types in two processes for execution



  • The firstactivity and loadactivity classes do not load the preset process. The loadservice is loaded into an independent process named "remote. In addition, the main thread of process #1 executes firstactivity and loadactivity ). The main thread of process #2 executes loadservice.
4. IPC ibinder interface-definition and implementation

  • The IPC communication in the android framework depends on a single ibinder interface. At this time, the client calls the transact () function of the ibinder interface and calls the ontransact () function of remote data through the IPC Mechanism.
  • In the source code of Android, the Java-layer ibinder interface is defined in the ibinder. Java code document. The program file is as follows:
  // IBinder.java     // .......     public interface IBinder {          // ..........          public boolean transact(int code, Parcel data, Parcel reply, int flags)               throws RemoteException; // ...........     }

  • Based on this ibinder. Java definition file, we can develop a category to implement it and then provide it to other apps for calling. In the android framework, the binder base class and binderproxy class are also written to implement the ibinder interface.


Understanding processes and IPC Architecture

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.