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

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

Step 5. ZygoteInit.runSelectLoopMode

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

 
 
  1. [java] view plaincopypublic class ZygoteInit { 
  2.   ...... 
  3.   /** 
  4.   * Runs the zygote process's select loop. Accepts new connections as 
  5.   * they happen, and reads commands from connections one spawn-request's 
  6.   * worth at a time. 
  7.   * 
  8.   * @throws MethodAndArgsCaller in a child process when a main() should 
  9.   * be executed. 
  10.   */ 
  11.   private static void runSelectLoopMode() throws MethodAndArgsCaller { 
  12.   ArrayList fds = new ArrayList(); 
  13.   ArrayList peers = new ArrayList(); 
  14.   FileDescriptor[] fdArray = new FileDescriptor[4]; 
  15.   fds.add(sServerSocket.getFileDescriptor()); 
  16.   peers.add(null); 
  17.   int loopCount = GC_LOOP_COUNT; 
  18.   while (true) { 
  19.   int index; 
  20.   /* 
  21.   * Call gc() before we block in select(). 
  22.   * It's work that has to be done anyway, and it's better 
  23.   * to avoid making every child do it. It will also 
  24.   * madvise() any free memory as a side-effect. 
  25.   * 
  26.   * Don't call it every time, because walking the entire 
  27.   * heap is a lot of overhead to free a few hundred bytes. 
  28.   */ 
  29.   if (loopCount <= 0) { 
  30.   gc(); 
  31.   loopCount = GC_LOOP_COUNT; 
  32.   } else { 
  33.   loopCount--; 
  34.   } 
  35.   try { 
  36.   fdArray = fds.toArray(fdArray); 
  37.   index = selectReadable(fdArray); 
  38.   } catch (IOException ex) { 
  39.   throw new RuntimeException("Error in select()", ex); 
  40.   } 
  41.   if (index < 0) { 
  42.   throw new RuntimeException("Error in select()"); 
  43.   } else if (index == 0) { 
  44.   ZygoteConnection newPeer = acceptCommandPeer(); 
  45.   peers.add(newPeer); 
  46.   fds.add(newPeer.getFileDesciptor()); 
  47.   } else { 
  48.   boolean done; 
  49.   done = peers.get(index).runOnce(); 
  50.   if (done) { 
  51.   peers.remove(index); 
  52.   fds.remove(index); 
  53.   } 
  54.   } 
  55.   } 
  56.   } 
  57.   ...... 
  58.   } 

当Step 4将数据通过Socket接口发送出去后,就会下面这个语句:

 
 
  1. [java] view plaincopydone = peers.get(index).runOnce(); 

这里从peers.get(index)得到的是一个ZygoteConnection对象,表示一个Socket连接,因此,接下来就是调用ZygoteConnection.runOnce函数进一步处理了。

新闻名称:Android应用程序进程启动过程的源代码分析(五)
URL标题:http://www.hantingmc.com/qtweb/news11/426761.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联