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

移动开发 Android
ZygoteInit.runSelectLoopMode这个函数定义在frameworks/base/core/java/com/android/internal/os/ZygoteInit.java文件中。

上文中的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函数进一步处理了。

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

2014-06-20 11:09:35

Android应用程序进程启动

2014-06-19 14:25:04

Android应用程序进程启动

2014-06-20 11:24:34

Android应用程序进程启动

2014-06-19 14:30:28

Android应用程序进程启动

2014-06-19 14:54:11

Android应用程序进程启动

2014-06-20 11:20:37

Android应用程序进程启动

2014-06-19 14:59:40

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

2014-07-31 10:06:01

谷歌Google应用

2012-08-16 09:07:57

Erlang

2018-03-13 13:00:03

Linux运维启动分析

2022-08-29 17:34:05

鸿蒙操作系统

2014-05-22 15:45:58

Android消息处理机制Looper

2010-12-13 11:40:17

Android应用程序

2009-08-14 17:57:43

ASP.NET MVC
点赞
收藏

51CTO技术栈公众号