小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android 之 zygote 與進程創(chuàng)建

 老匹夫 2013-12-25
分類: Android 2011-07-03 16:24 4877人閱讀 評論(3) 收藏 舉報

    在android中,應(yīng)用程序的入口是ActivityThead中的main函數(shù),那么之后系統(tǒng)是怎樣為應(yīng)用程序創(chuàng)建進程的呢?SystemService又是怎樣創(chuàng)建的?答案是:zygote

    zygote翻譯成中文是受精卵的意思,名字比較奇怪、但是很有意思。在android中,大部分的應(yīng)用程序進程都是由zygote來創(chuàng)建的,為什么用大部分,因為還有一些進程比如系統(tǒng)引導(dǎo)進程、init進程等不是有zygote創(chuàng)建的。相反,zygote還是在init進程之后才被創(chuàng)建的。在android中提到zygote,主要兩塊,一個是C/C++編寫的zygote,主要用來為應(yīng)用和SystemService fork進程的。一個是java編寫的zygote接口,負責(zé)為應(yīng)用和service調(diào)用C/C++ zygote的接口執(zhí)行fork,從而創(chuàng)建VM進程。說明:在android中,service主要有NativeService和SystemService。SystemService主要是指系統(tǒng)中service,比如,InputMethodService、ActivityManagerService等。

    zygote在android中主要有兩個作用:

  1. 建立運行時環(huán)境并啟動虛擬機,執(zhí)行com.android.internal.os.ZygoteInit的main函數(shù),從而fork SystemService
    1. runtime.start("com.android.internal.os.ZygoteInit", startSystemServer);  

  2. 為應(yīng)用程序創(chuàng)建DVM進程。
  3. 啟動SystemServer:

    我們來看看zygote是怎樣創(chuàng)建SystemService進程的。在../base/cmds/app_process/app_main.cpp的主函數(shù)中,有這樣一段代碼,它執(zhí)行了

    runtime.start("com.android.internal.os.ZygoteInit", startSystemServer);   //runtime繼承自AndroidRuntime

    也就是說,在主函數(shù)中,初始化了運行時環(huán)境,并且建立虛擬機,然后運行再com.android.internal.os.ZygoteInit的main函數(shù)

    再來看看com.android.internal.os.ZygoteInit的main中都做了哪些事情。在看ZygoteInit之前,有必要先來看下相關(guān)的類,類圖如下:

     

    在ZygoteInit的main函數(shù)中,主要執(zhí)行了:

    1. registerZygoteSocket();//登記Listen端口   
    2. startSystemServer();//啟動SystemServer  

    startSystemServer()調(diào)用Zygote的native方法 forkSystemServer(); 到這里,java端的Zygote的準備工作就結(jié)束了,接下來就交給C/C++端的Zygote來執(zhí)行fork任務(wù)了。來看下代碼:

    在 ../dalvik/vm/native/dalvik_system_Zygote.c 中

    1. static void Dalvik_dalvik_system_Zygote_forkSystemServer(  
    2. const u4* args, JValue* pResult)  
    3. {  
    4.     pid_t pid;  
    5.     /*調(diào)用forkAndSpecializeCommon,執(zhí)行fork  */  
    6.     pid = forkAndSpecializeCommon(args, true);  
    7.     /* 檢查fork后返回的進程pid */  
    8.     if (pid > 0) {  
    9.         int status;  
    10.         LOGI("System server process %d has been created", pid);  
    11.         gDvm.systemServerPid = pid;  
    12.         if (waitpid(pid, &status, WNOHANG) == pid) {  
    13.             LOGE("System server process %d has died. Restarting Zygote!", pid);  
    14.             kill(getpid(), SIGKILL);  
    15.         }  
    16.     }  
    17.     RETURN_INT(pid);  
    18. }  

    在這個里面的fork進程主要是使用linux的fork進程。

    經(jīng)過這樣的過程SystemServer進程就創(chuàng)建起來了。android中的所有服務(wù)循環(huán)框架都是建立咋SystemServer上,接下來在SystemServer上,就可以建立所有系統(tǒng)服務(wù)??蓞⒖矗篠ystemServer.main();

    系統(tǒng)服務(wù)啟動后會調(diào)用ActivityManagerService的systemReady方法,并最終啟動HomeActivity。

    啟動應(yīng)用進程:

    我們在上一篇介紹ActivityThread和ActivityManagerService時已經(jīng)講過,程序的主入口是在ActivityThread的main函數(shù),activity的startActivity最終是在ActivityManagerService中執(zhí)行的,那么應(yīng)用程序的進程是怎么創(chuàng)建的?看下類圖:

     

     

    我們再來看看ActivityManagerService中的startProcessLocked方法。

    1. int pid = Process.start("android.app.ActivityThread",  
    2.                     mSimpleProcessManagement ? app.processName : null, uid, uid,  
    3.                     gids, debugFlags, null);  

    通過Process的start方法來創(chuàng)建進程。

    1. / **  
    2.   *通過Zygote進程來創(chuàng)建新的vm進程  
    3.   */  
    4.   
    5.   public static final int start(final String processClass,final String niceName,int uid, int gid, int[] gids,int debugFlags,String[] zygoteArgs)  
    6.   {  
    7.       if (supportsProcesses()) {  
    8.           try {  
    9.               return startViaZygote(processClass, niceName, uid, gid, gids,  
    10.                       debugFlags, zygoteArgs);     //argsForZygote.add("--runtime-init")初始化運行環(huán)境   
    11.           } catch (ZygoteStartFailedEx ex) {  
    12.               Log.e(LOG_TAG,  
    13.                       "Starting VM process through Zygote failed");  
    14.               throw new RuntimeException(  
    15.                       "Starting VM process through Zygote failed", ex);  
    16.           }  
    17.   
    18.       } else {  
    19.   
    20.           // Running in single-process mode   
    21.           Runnable runnable = new Runnable() {  
    22.                       public void run() {  
    23.                           Process.invokeStaticMain(processClass);  
    24.                       }  
    25.           };  
    26.           // Thread constructors must not be called with null names (see spec).   
    27.           if (niceName != null) {  
    28.               new Thread(runnable, niceName).start();  
    29.           } else {  
    30.               new Thread(runnable).start();  
    31.           }  
    32.           return 0;  
    33.       }  
    34.   }  

    在ZygoteConnection中獲取套接字連接,并解析啟動參數(shù)。來看下runOnce方法:

     從LocalSocket. mSocket中解析參數(shù)

    1. try {  
    2.           args = readArgumentList();  
    3.           descriptors = mSocket.getAncillaryFileDescriptors();  
    4.       } catch (IOException ex) {  
    5.           Log.w(TAG, "IOException on command socket " + ex.getMessage());  
    6.           closeSocket();  
    7.           return true;  
    8.       }  

  4. 調(diào)用Zygote的native方法forkAndSpecialize,執(zhí)行進程的創(chuàng)建工作。本地方法的實現(xiàn)也是在 ../dalvik/vm/native/dalvik_system_Zygote.c 中,底層調(diào)用linux的fork。
    1. pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid,  
    2.   
    3.                     parsedArgs.gids, parsedArgs.debugFlags, rlimits);  

    這樣應(yīng)用程序的進程就創(chuàng)建起來了。從ActivityManagerService開始的時序圖如下:

     

    總結(jié):

  5. 在android中SystemService的啟動是在Zygote進程創(chuàng)建好后進行的,并且由Zygote進程建立好DVM運行環(huán)境,加載ZygoteInit的main函數(shù),最終調(diào)用Zygote的本地方法forkSystemServer,并執(zhí)行l(wèi)inux的fork方法創(chuàng)建SystemServer進程。
  6. 應(yīng)用程序的進程也是由Zygote創(chuàng)建的,在ActivityManagerService中的startProcessLocked中調(diào)用了Process.start()方法。并通過連接調(diào)用Zygote的native方法forkAndSpecialize,執(zhí)行fork任務(wù)。
  7. 應(yīng)用進程和服務(wù)進程位于不同的進程中,他們之間是通過IPC進行數(shù)據(jù)傳遞的。接下來一篇會介紹在android中的進程間通信機制:Binder

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多