How to start a new process for Android?

来源:互联网 发布:linux 密码策略 编辑:程序博客网 时间:2024/05/16 04:57

http://blog.csdn.net/windskier/article/details/6417061


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.

 

[c-sharp] view plaincopy
  1. int pid = Process.start("android.app.ActivityThread",  
  2.         mSimpleProcessManagement ? app.processName : null, uid, uid,  
  3.         gids, debugFlags, null);    


    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

[java] view plaincopy
  1. argsForZygote.add("--runtime-init");  
  2. argsForZygote.add("--setuid=" + uid);  
  3. argsForZygote.add("--setgid=" + gid);   
 
    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
 
[c-sharp] view plaincopy
  1. pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid,  
  2.         parsedArgs.gids, parsedArgs.debugFlags, rlimits);  
 
    Above code forks a new process, It is very easy to understand and  unnecessary to look into.
 
[c-sharp] view plaincopy
  1. if (pid == 0) {  
  2.     // in child  
  3.     handleChildProc(parsedArgs, descriptors, newStderr);  
  4.     // should never happen  
  5.     return true;  
  6. else { /* pid != 0 */  
  7.     // in parent...pid of < 0 means failure  
  8.     return handleParentProc(pid, descriptors, parsedArgs);  
  9. }    
   

    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.

[c-sharp] view plaincopy
  1. if (parsedArgs.runtimeInit) {  
  2.     RuntimeInit.zygoteInit(parsedArgs.remainingArgs);  
  3. }      

6.zygoteInit in RuntimeInit.java

   
[c-sharp] view plaincopy
  1. public static final void zygoteInit(String[] argv)  
  2.            throws ZygoteInit.MethodAndArgsCaller {  
  3.        // TODO: Doing this here works, but it seems kind of arbitrary. Find  
  4.        // a better place. The goal is to set it up for applications, but not  
  5.        // tools like am.  
  6.        System.setOut(new AndroidPrintStream(Log.INFO, "System.out"));  
  7.        System.setErr(new AndroidPrintStream(Log.WARN, "System.err"));  
  8.   
  9.        commonInit();  
  10.        zygoteInitNative();  
  11.   
  12.        int curArg = 0;  
  13.        for ( /* curArg */ ; curArg < argv.length; curArg++) {  
  14.            String arg = argv[curArg];  
  15.   
  16.            if (arg.equals("--")) {  
  17.                curArg++;  
  18.                break;  
  19.            } else if (!arg.startsWith("--")) {  
  20.                break;  
  21.            } else if (arg.startsWith("--nice-name=")) {  
  22.                String niceName = arg.substring(arg.indexOf('=') + 1);  
  23.                Process.setArgV0(niceName);  
  24.            }  
  25.        }  
  26.   
  27.        if (curArg == argv.length) {  
  28.            Slog.e(TAG, "Missing classname argument to RuntimeInit!");  
  29.            // let the process exit  
  30.            return;  
  31.        }  
  32.   
  33.        // Remaining arguments are passed to the start class's static main  
  34.   
  35.        String startClass = argv[curArg++];  
  36.        String[] startArgs = new String[argv.length - curArg];  
  37.   
  38.        System.arraycopy(argv, curArg, startArgs, 0, startArgs.length);  
  39.        invokeStaticMain(startClass, startArgs);  
  40.    }  

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:

[c-sharp] view plaincopy
  1. static void com_android_internal_os_RuntimeInit_zygoteInit(JNIEnv* env, jobject clazz)  
  2. {  
  3.     gCurRuntime->onZygoteInit();  
  4. }  

 

    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.

[c-sharp] view plaincopy
  1. virtual void onZygoteInit()  
  2. {  
  3.     sp<ProcessState> proc = ProcessState::self();  
  4.     if (proc->supportsProcesses()) {  
  5.         LOGV("App process: starting thread pool./n");  
  6.         proc->startThreadPool();  
  7.     }         
  8. }  

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
   
[c-sharp] view plaincopy
  1. public static final void main(String[] args) {  
  2.        SamplingProfilerIntegration.start();  
  3.   
  4.        Process.setArgV0("<pre-initialized>");  
  5.   
  6.        Looper.prepareMainLooper();  
  7.        if (sMainThreadHandler == null) {  
  8.            sMainThreadHandler = new Handler();  
  9.        }  
  10.   
  11.        ActivityThread thread = new ActivityThread();  
  12.        thread.attach(false);  
  13.   
  14.        if (false) {  
  15.            Looper.myLooper().setMessageLogging(new  
  16.                    LogPrinter(Log.DEBUG, "ActivityThread"));  
  17.        }  
  18.   
  19.        Looper.loop();  
  20.   
  21.        if (Process.supportsProcesses()) {  
  22.            throw new RuntimeException("Main thread loop unexpectedly exited");  
  23.        }  
  24.   
  25.        thread.detach();  
  26.        String name = (thread.mInitialApplication != null)  
  27.            ? thread.mInitialApplication.getPackageName()  
  28.            : "<unknown>";  
  29.        Slog.i(TAG, "Main thread of " + name + " is now exiting");  
  30.    }  

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.