編輯:關於Android編程
Android啟動過程中的第一個進程init,在啟動過程中會啟動兩個關鍵的系統服務進程ServiceManager和Zygote。本文要介紹的就是Zygote進程的啟動,Zygote俗稱孵化器,專門用於生產(啟動)新的進程。Zygote是在Init.rc(aosp/system/core/rootdir)裡描述並由init進程啟動的。相關代碼如下:
service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server class main socket zygote stream 660 root system onrestart write /sys/android_power/request_state wake onrestart write /sys/power/state on onrestart restart media onrestart restart netdinit.rc文件的語法描述可以參考aosp/system/core/init/readme.txt。
zygote的path路徑為/system/bin/app_process,-Xzygote /system/bin --zygote --start-system-server是它的參數,所屬class是main,同屬同一個class的進程會同時啟動或者停止,會啟動一個socket名為zygote,重啟時將做四個操作。
下面是Zygote進程啟動的大致時序圖:
Android啟動過程中,init進程使用aZ喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Avc3lzdGVtL2NvcmUvaW5pdC9Jbml0X3BhcnNlci5jveLO9mluaXQucmO9xbG+zsS8/qGjPC9wPgo8cD4gICAgIHN0ZXAxo6zRrbu3xvS2r2luaXQucmPOxLz+w+jK9rXEy/nT0Ne0zKyyu86qU1ZDX0RJU0FCTEVEtcRzZXJ2aWNlo6xmb3JrKCnQwr34s8yhozwvcD4KPHA+ICAgICBzdGVwMqOsxvS2r1p5Z290ZaOs1rTQ0GFwcF9tYWluLmNwcLXEbWFpbigpt723qKOsPC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;"> // Parse runtime arguments. Stop at first unrecognized option. bool zygote = false; bool startSystemServer = false; bool application = false; const char* parentDir = NULL; const char* niceName = NULL; const char* className = NULL; while (i < argc) {//main的參數匹配 const char* arg = argv[i++]; if (!parentDir) { parentDir = arg; } else if (strcmp(arg, "--zygote") == 0) { zygote = true; niceName = "zygote"; } else if (strcmp(arg, "--start-system-server") == 0) { startSystemServer = true; } else if (strcmp(arg, "--application") == 0) { application = true; } else if (strncmp(arg, "--nice-name=", 12) == 0) { niceName = arg + 12; } else { className = arg; break; } } if (niceName && *niceName) { setArgv0(argv0, niceName); set_process_name(niceName); } runtime.mParentDir = parentDir; if (zygote) { runtime.start("com.android.internal.os.ZygoteInit", startSystemServer ? "start-system-server" : ""); } else if (className) { ...... } else { ...... }step3,啟動AppRuntime,AppRuntime繼承自AndroidRuntime,這裡需要做的事情有:
/* start the virtual machine */ JniInvocation jni_invocation; jni_invocation.Init(NULL); JNIEnv* env; if (startVm(&mJavaVM, &env) != 0) {//step4,創建JVM return; } onVmCreated(env); /* * Register android functions. */ if (startReg(env) < 0) {//step6,在JVM中注冊android本地方法 ALOGE("Unable to register all android natives\n"); return; }
step9,registerZygoteSocket()注冊服務端socket,這個socket就是前面提到的zygote,在init進程解析init.rc時創建。
public static void main(String argv[]) { try { // Start profiling the zygote initialization. SamplingProfilerIntegration.start(); registerZygoteSocket(); ....... preload(); ....... // Do an initial gc to clean up after startup gc(); ...... if (argv[1].equals("start-system-server")) { startSystemServer();//step11 } else if (!argv[1].equals("")) { throw new RuntimeException(argv[0] + USAGE_STRING); } runSelectLoop(); closeServerSocket(); } catch (MethodAndArgsCaller caller) { caller.run(); } catch (RuntimeException ex) { Log.e(TAG, "Zygote died with exception", ex); closeServerSocket(); throw ex; }
private static boolean startSystemServer() throws MethodAndArgsCaller, RuntimeException { ...... /* Hardcoded command line to start the system server */ String args[] = { "--setuid=1000", "--setgid=1000", "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1032,3001,3002,3003,3006,3007", "--capabilities=" + capabilities + "," + capabilities, "--runtime-init", "--nice-name=system_server", "com.android.server.SystemServer", }; ZygoteConnection.Arguments parsedArgs = null; int pid; try { ....... /* Request to fork the system server process */ pid = Zygote.forkSystemServer( parsedArgs.uid, parsedArgs.gid, parsedArgs.gids, parsedArgs.debugFlags, null, parsedArgs.permittedCapabilities, parsedArgs.effectiveCapabilities); } catch (IllegalArgumentException ex) { throw new RuntimeException(ex); } /* For child process */ if (pid == 0) { handleSystemServerProcess(parsedArgs);//step12 } return true; }
參考:
http://blog.csdn.net/luoshengyang/article/details/6768304
www.cnblogs.com/bastard/archive/2012/08/28/2660389.html
右鍵復制圖片地址,在浏覽器中打開即可查看大圖。
未完待續,有不對的地方,請指正。
前言最近剛好在做關於異步通信的需求,那麼,今天我們來講解下Android開發中的Handler異步通信傳遞機制(包括Looper、Message Queue)目錄定義An
本文的目的是用比較容易理解的方式,介紹一下整個Android項目的編譯。至少知道大概的編譯流程是怎麼樣的,項目裡面的Android.mk文件包含些什麼內容。makefil
任何程序都是靜態代碼,我們把這些靜態代碼打包好,然後放到運行環境當中,通過事件流的驅動使這些代碼運行起來。Android的環境也不例外。靜態的代碼,在動態事件的驅動下,才
意圖是指兩個UI主界面的轉換,要想了解意圖就必須學習ACtivity的生命周期 默認在UI界面顯示的為運行為運行狀態,而在後台的為onPause方法 主線: