Android应用程序进程启动过程的源代码分析(四)

移动开发 Android
函数将创建进程的参数放到argsForZygote列表中去,如参数"--runtime-init"表示要为新创建的进程初始化运行时库,然后调用zygoteSendAndGetPid函数进一步操作。

上文中的函数将创建进程的参数放到argsForZygote列表中去。

如参数"--runtime-init"表示要为新创建的进程初始化运行时库,然后调用zygoteSendAndGetPid函数进一步操作。

Step 4. Process.zygoteSendAndGetPid

这个函数定义在frameworks/base/core/java/android/os/Process.java文件中:

  1. [java] view plaincopypublic class Process { 
  2.   ...... 
  3.   private static int zygoteSendArgsAndGetPid(ArrayList args) 
  4.   throws ZygoteStartFailedEx { 
  5.   int pid; 
  6.   openZygoteSocketIfNeeded(); 
  7.   try { 
  8.   /** 
  9.   * See com.android.internal.os.ZygoteInit.readArgumentList() 
  10.   * Presently the wire format to the zygote process is: 
  11.   * a) a count of arguments (argc, in essence) 
  12.   * b) a number of newline-separated argument strings equal to count 
  13.   * 
  14.   * After the zygote process reads these it will write the pid of 
  15.   * the child or -1 on failure. 
  16.   */ 
  17.   sZygoteWriter.write(Integer.toString(args.size())); 
  18.   sZygoteWriter.newLine(); 
  19.   int sz = args.size(); 
  20.   for (int i = 0; i < sz; i++) { 
  21.   String arg = args.get(i); 
  22.   if (arg.indexOf('\n') >= 0) { 
  23.   throw new ZygoteStartFailedEx( 
  24.   "embedded newlines not allowed"); 
  25.   } 
  26.   sZygoteWriter.write(arg); 
  27.   sZygoteWriter.newLine(); 
  28.   } 
  29.   sZygoteWriter.flush(); 
  30.   // Should there be a timeout on this? 
  31.   pid = sZygoteInputStream.readInt(); 
  32.   if (pid < 0) { 
  33.   throw new ZygoteStartFailedEx("fork() failed"); 
  34.   } 
  35.   } catch (IOException ex) { 
  36.   ...... 
  37.   } 
  38.   return pid; 
  39.   } 
  40.   ...... 
  41.   } 
  42.   这里的sZygoteWriter是一个Socket写入流,是由openZygoteSocketIfNeeded函数打开的: 
  43.   [java] view plaincopypublic class Process { 
  44.   ...... 
  45.   /** 
  46.   * Tries to open socket to Zygote process if not already open. If 
  47.   * already open, does nothing. May block and retry. 
  48.   */ 
  49.   private static void openZygoteSocketIfNeeded() 
  50.   throws ZygoteStartFailedEx { 
  51.   int retryCount; 
  52.   if (sPreviousZygoteOpenFailed) { 
  53.   /* 
  54.   * If we've failed before, expect that we'll fail again and 
  55.   * don't pause for retries. 
  56.   */ 
  57.   retryCount = 0
  58.   } else { 
  59.   retryCount = 10
  60.   } 
  61.   /* 
  62.   * See bug #811181: Sometimes runtime can make it up before zygote. 
  63.   * Really, we'd like to do something better to avoid this condition, 
  64.   * but for now just wait a bit... 
  65.   */ 
  66.   for (int retry = 0 
  67.   ; (sZygoteSocket == null) && (retry < (retryCount + 1)) 
  68.   ; retry++ ) { 
  69.   if (retry > 0) { 
  70.   try { 
  71.   Log.i("Zygote""Zygote not up yet, sleeping..."); 
  72.   Thread.sleep(ZYGOTE_RETRY_MILLIS); 
  73.   } catch (InterruptedException ex) { 
  74.   // should never happen 
  75.   } 
  76.   } 
  77.   try { 
  78.   sZygoteSocket = new LocalSocket(); 
  79.   sZygoteSocket.connect(new LocalSocketAddress(ZYGOTE_SOCKET, 
  80.   LocalSocketAddress.Namespace.RESERVED)); 
  81.   sZygoteInputStream 
  82.   = new DataInputStream(sZygoteSocket.getInputStream()); 
  83.   sZygoteWriter = 
  84.   new BufferedWriter( 
  85.   new OutputStreamWriter( 
  86.   sZygoteSocket.getOutputStream()), 
  87.   256); 
  88.   Log.i("Zygote""Process: zygote socket opened"); 
  89.   sPreviousZygoteOpenFailed = false
  90.   break
  91.   } catch (IOException ex) { 
  92.   ...... 
  93.   } 
  94.   } 
  95.   ...... 
  96.   } 
  97.   ...... 
  98.   } 

这个Socket由frameworks/base/core/java/com/android/internal/os/ZygoteInit.java文件中的ZygoteInit类在runSelectLoopMode函数侦听的。

责任编辑:闫佳明 来源: bbs.9ria
相关推荐

2014-06-20 11:24:34

Android应用程序进程启动

2014-06-20 11:09:35

Android应用程序进程启动

2014-06-20 11:05:56

Android应用程序进程启动

2014-06-19 14:25:04

Android应用程序进程启动

2014-06-19 14:30:28

Android应用程序进程启动

2014-06-19 14:54:11

Android应用程序进程启动

2014-06-20 11:20:37

Android应用程序进程启动

2012-02-20 14:47:08

JavaPlay

2014-05-22 15:00:16

Android消息处理机制Looper

2011-08-17 16:16:29

iPhone应用程序启动过程

2011-07-28 10:34:38

Cocoa 程序 启动

2014-06-23 10:31:09

Android启动过程

2011-06-28 13:27:13

ARM Linux

2012-08-16 09:07:57

Erlang

2018-03-13 13:00:03

Linux运维启动分析

2014-07-31 10:06:01

谷歌Google应用

2022-08-29 17:34:05

鸿蒙操作系统

2010-12-13 11:40:17

Android应用程序

2009-03-11 13:38:37

构造块ActivityIntent Rece

2014-05-22 15:45:58

Android消息处理机制Looper
点赞
收藏

51CTO技术栈公众号