Source code analysis of Android Application Process Startup Process (5)

Source: Internet
Author: User

The preceding Socket is listened by the ZygoteInit class in the frameworks/base/core/java/com/android/internal/OS/ZygoteInit. java file in the runSelectLoopMode function.

Step 5. ZygoteInit. runSelectLoopMode

This function is defined in the frameworks/base/core/java/com/android/internal/OS/ZygoteInit. java file:

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

After Step 4 sends data through the Socket interface, the following statement is executed:

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

Here, we get a ZygoteConnection object from peers. get (index), indicating a Socket connection. Therefore, we will call the ZygoteConnection. runOnce function for further processing.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.