Zygote鐨勫惎鍔?/h1>

瀹冧互鏈嶅姟鐨勫舰寮忚鍚姩銆?/p>

/* start the virtual machine. */startVM(&mJavaVM, &env);/* Register android functions. */startReg(env);

鎺ヤ笅鏉ユ墽琛屸€渃om.android.internal.os.ZygoteInit鈥滼ava绫荤殑main鏂规硶缁х画鎵ц鍚姩銆?/p>

ZygoteInit.main

package com.android.internal.os;import android.net.LocalServerSocket;...public class ZygoteInit {  private static LocalServerSocket sServerSocket;  public static void main(String argv[]) {    ...    registerZygoteSocket();    ...    if (argv[1].equals("true")) {        startSystemServer();    } else if (!argv[1].equals("false")) {        throw new RuntimeException(argv[0] + USAGE_STRING);    }    ...    if (ZYGOTE_FORK_MODE) {        runForkMode();    } else {        runSelectLoopMode();    }    ...    closeServerSocket();    ...  }}

鍒涘缓Socket

Zygote璋冪敤registerZygoteSocket();鍒涘缓涓€涓?code>LocalServerSocket sServerSocket鐨凷erver绔疭ocket锛岀瓑寰呬互鍚庤繍琛屽湪System杩涚▼涓殑鏈嶅姟ActivityManagerService鍒涘缓鐨凜lient绔疭ocket杩炴帴锛岀劧鍚庨€氳繃Socket杩涚▼闂撮€氫俊閫氱煡Zygote鍒涘缓鏂扮殑搴旂敤绋嬪簭杩涚▼銆?/p>

鍒涘缓System杩涚▼

/** * Prepare the arguments and fork for the system server process. */private static boolean startSystemServer()        throws MethodAndArgsCaller, RuntimeException {    /* Hardcoded command line to start the system server */    String args[] = new String[] {            "--setuid=1000",            "--setgid=1000",            "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,3001,3002,3003,3006",            "--capabilities=130104352,130104352",            "--rlimit=8,",            "--runtime-init",            "--nice-name=system_server",            "com.android.server.SystemServer",        };        ...    ZygoteConnection.Arguments parsedArgs = null;    int pid;    try {        parsedArgs = new ZygoteConnection.Arguments(args);        /* Request to fork the system server process */        pid = Zygote.forkSystemServer(                parsedArgs.uid, parsedArgs.gid,                parsedArgs.gids, debugFlags, rlimits,                parsedArgs.permittedCapabilities,                parsedArgs.effectiveCapabilities);    } catch (IllegalArgumentException ex) {        throw new RuntimeException(ex);    }    /* For child process */    if (pid == 0) {        handleSystemServerProcess(parsedArgs);    }    return true;}

鍙橀噺args淇濆瓨鍚姩System杩涚▼鐨勫弬鏁般€?br> Zygote.forkSystemServer()澶嶅埗褰撳墠杩涚▼鏉ュ垱寤哄瓙杩涚▼銆?br> handleSystemServerProcess()缁х画澶勭悊System杩涚▼鐨勫惎鍔ㄣ€?/p>

杞绛夊緟AMS璇锋眰鍒涘缓App杩涚▼

private static void runSelectLoopMode() throws MethodAndArgsCaller {  ArrayList fds = new ArrayList();  ArrayList peers = new ArrayList();  FileDescriptor[] fdArray = new FileDescriptor[4];  fds.add(sServerSocket.getFileDescriptor());  peers.add(null);  while (true) {    ...      try {          fdArray = fds.toArray(fdArray);          index = selectReadable(fdArray);      } catch (IOException ex) {          throw new RuntimeException("Error in select()", ex);      }      if (index < 0) {          throw new RuntimeException("Error in select()");      } else if (index == 0) {          ZygoteConnection newPeer = acceptCommandPeer();          peers.add(newPeer);          fds.add(newPeer.getFileDesciptor());      } else {          boolean done;          done = peers.get(index).runOnce();          if (done) {              peers.remove(index);              fds.remove(index);          }      }  }}/** * Waits for and accepts a single command connection. Throws * RuntimeException on failure. */private static ZygoteConnection acceptCommandPeer() {    try {        return new ZygoteConnection(sServerSocket.accept());    } catch (IOException ex) {        throw new RuntimeException(                "IOException during accept()", ex);    }}

鏃犻檺寰幆锛岀瓑寰呮潵鑷狝MS鍒涘缓鐨凷ocket杩炴帴銆?br> sServerSocket鍦╢ds[0]浣嶇疆銆?br> 姣忓綋accept()杩斿洖涓€涓繛鎺ュ悗锛屽皢瀵瑰簲姝よ繛鎺ョ殑newPeer.getFileDesciptor()濂楁帴瀛楁弿杩版坊鍔犲埌fds锛堢0浣嶇疆鍚庯級锛屼笅涓€娆¤鍙栧埌鏁版嵁鏃讹紝鑻ュ湪fds[0]浠ュ悗鐨勶紝璇存槑鏄墠闈㈢殑newPeer杩炴帴鏀跺埌鐨凙MS鐨勫垱寤烘柊搴旂敤绋嬪簭杩涚▼鐨勮姹傘€?br> runOnce()鐢ㄦ潵澶勭悊AMS鍒涘缓鏂板簲鐢ㄧ▼搴忚繘绋嬬殑璇锋眰銆?/p>

ZygoteInit.handleSystemServerProcess()

private static void handleSystemServerProcess(      ZygoteConnection.Arguments parsedArgs)      throws ZygoteInit.MethodAndArgsCaller {  closeServerSocket();  /*   * Pass the remaining arguments to SystemServer.   * "--nice-name=system_server com.android.server.SystemServer"   */  RuntimeInit.zygoteInit(parsedArgs.remainingArgs);  /* should never reach here */}

Zygote澶嶅埗鑷韩鍒涘缓鐨勫瓙杩涚▼鍋氫负System杩涚▼锛岃繖鏍峰畠寰楀埌浜哯ygote鐨凷erver Socket锛屼絾鏄敤涓嶅埌锛屾墍浠ョ涓€鍙?code>closeServerSocket()鍏抽棴姝ゅ鎺ュ瓧銆?/p>

RuntimeInit.zygoteInit

package com.android.internal.os;public class RuntimeInit {  public static final void zygoteInit(String[] argv)          throws ZygoteInit.MethodAndArgsCaller {      ...      commonInit();      zygoteInitNative();      ...      // Remaining arguments are passed to the start class's static main      String startClass = argv[curArg++];      String[] startArgs = new String[argv.length - curArg];      System.arraycopy(argv, curArg, startArgs, 0, startArgs.length);      invokeStaticMain(startClass, startArgs);  }  }

zygoteInitNative()鍦⊿ystem杩涚▼涓惎鍔ㄤ竴涓狟inder绾跨▼姹犮€?br> RuntimeInit.invokeStaticMain()闈欐€佹柟娉曡皟鐢?code>"com.android.server.SystemServer"鐨刴ain鏂规硶銆?/p>

SystemServer.main

浼犻€掕皟鐢╪ative鏂规硶init1()锛?/p>

class SystemServer {  /**     * This method is called from Zygote to initialize the system. This will cause the native     * services (SurfaceFlinger, AudioFlinger, etc..) to be started. After that it will call back     * up into init2() to start the Android services.     */    native public static void init1(String[] args);}

init1()鐨勫伐浣滐細
娉ㄥ唽Service Manager鐨勬浜¢€氱煡锛氳皟鐢╞inderDied()銆係ystem杩涚▼鎵цkill缁撴潫鑷繁銆?/p>

鍒涘缓SurfaceFlinger銆佸拰SensorService涓や釜鏈嶅姟銆?/p>

杩斿洖SystemServer.init2()缁х画鍚姩java璇█寮€鍙戠殑绯荤粺鏈嶅姟銆?/p>

SystemServer.init2

public static final void init2() {    Thread thr = new ServerThread();    thr.setName("android.server.ServerThread");    thr.start();}

ServerThread缁ф壙鑷猅hread銆?/p>

ServerThread.run

class ServerThread extends Thread {  @Override  public void run() {      Looper.prepare();      ...      // Critical services...      try {          ...          Slog.i(TAG, "Activity Manager");          context = ActivityManagerService.main(factoryTest);          ...          Slog.i(TAG, "Package Manager");          pm = PackageManagerService.main(context,                  factoryTest != SystemServer.FACTORY_TEST_OFF);          ...          Slog.i(TAG, "Content Manager");          ContentService.main(context,                  factoryTest == SystemServer.FACTORY_TEST_LOW_LEVEL);          ...          Slog.i(TAG, "Window Manager");          wm = WindowManagerService.main(context, power,                  factoryTest != SystemServer.FACTORY_TEST_LOW_LEVEL);          ServiceManager.addService(Context.WINDOW_SERVICE, wm);          ...          ((ActivityManagerService)ServiceManager.getService("activity"))                  .setWindowManager(wm);          ...      } catch (RuntimeException e) {          Slog.e("System", "Failure starting core service", e);      }      ...      Looper.loop();      Slog.d(TAG, "System ServerThread is exiting!");  }}

鍚姩鍚勪釜Service鐒跺悗娉ㄥ唽鍒癝erviceManager銆?br> 鍚勪釜鏈嶅姟閮戒娇鐢˙inder鍜屽叾瀹冩湇鍔′娇鐢ㄨ€呰繘绋嬭繘琛屽氨琛屼氦浜掋€?/p>

锛堟湰鏂囦娇鐢ˋtom缂栧啓锛?/p>

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android内核详解之Low memory killer
  2. Android(安卓)4.4(KitKat)窗口管理子系统
  3. 玩玩Android
  4. Android中贪吃蛇游戏的学习(一)
  5. 深入理解Android消息处理系统——Looper
  6. Android智能指针 (sp & wp)
  7. java代码调试,Android代码调试。
  8. [导入]“开放”Android难敌Apple iOS?
  9. Android:最全面的 Webview 详解
  10. 推送 从入门到放弃