How to start a new process for Android?

来源:互联网 发布:非遗文化知多少 编辑:程序博客网 时间:2024/06/04 23:36

We need start a new process when we tap application launcher or start a new service which is in a different process. This artical will describe how a new process is created but no matter whoever the caller is.

1.startProcessLocked in ActivityManagerService.java
    Ignore rest of the function and focus on below code.

 


    According to above code,we can find that another process created with a nice name "app.processName" or NULL where the first args is the first class started by the new process.

    Now, we look into the start function of the Process class.

2.startViaZygote in Process.java

 
    The first sentence means that we need to init runtime when create this Process, the purpose of this initialization will be discussed later.
    We should know the communication between ActivityManagerService and  zygote relies on socket, AM writes the arguments of the new process into the buffer for zygote socket.
    Then the starter's work is done, let's turn over into the zygote at looked at the other socket communication side.

3.
    The zygote process will be in a loop in order to detect any connect into the zygote socket and fork the request process after being started by init deamon process, all this work is running in function runSelectLoopMode in ZygoteInit.java and calls runOnce function in ZygoteConnection.java to fork new process.

4.runOnce in ZygoteConnection.java
 
 
    Above code forks a new process, It is very easy to understand and  unnecessary to look into.
 
   

    As we know, parent process and child process will execute the code simultaneously after fork operation, therefore, the parent process will get the real pid of child process and call handleParentProc method, meanwhile, the child process will get a zero pid value and call handleChildProc.
    We ignore the handleParentProc in which there is nothing important but cleanup of parent process.

The belowing operations are in the new process.

5.handleChildProc in ZygoteConnection.java

    Function handleChildProc will check if the process starter needs the runtime initialization which is set in step 2. Here need to init runtime while every process is being created.


6.zygoteInit in RuntimeInit.java

   
6.1 zygoteInitNative
    This function is a native function which spawns a pool thread to detect binder IPCs. Its prototype in JNI layer is underlying:

 

    gCurRuntime is a global variable which is initialized when app_main starts. We can find this process in AndroidRuntime constructor. So we confirm that the gCurRuntime is an AppRuntime instance and class AppRuntime extends AndroidRuntime.According to all the facts, we can conclude the onZygoteInit function belongs to class AppRuntime.

6.2 invokeStaticMain

After creating process and corresponding pool thread for binder IPC, the last job here is to call the "main" method of the process's first class. It should be "android.app.ActivityThread" for AM to start a new activity or service of different processes.
ActivityThread instance is the main thread of the new process.

7.main method in ActivityThread.java
   
In above code, we can not find any sentence create the new activity or service. where is the operation hiding?

The implementation is very complicated, the above code has a sentence "thread.attach(false)" where all the stuff is hiding.

I will discuss how activity starts in later artical.

原创粉丝点击