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

Source: Internet
Author: User

The preceding result obtained from peers. get (index) is a ZygoteConnection object, indicating a Socket connection.

Therefore, the next step is to call the ZygoteConnection. runOnce function for further processing.

Step 6. ZygoteConnection. runOnce

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

 
 
  1. [java] view plaincopyclass ZygoteConnection { 
  2. ...... 
  3. boolean runOnce() throws ZygoteInit.MethodAndArgsCaller { 
  4. String args[]; 
  5. Arguments parsedArgs = null; 
  6. FileDescriptor[] descriptors; 
  7. try { 
  8. args = readArgumentList(); 
  9. descriptors = mSocket.getAncillaryFileDescriptors(); 
  10. } catch (IOException ex) { 
  11. ...... 
  12. return true; 
  13. ...... 
  14. /** the stderr of the most recent request, if avail */ 
  15. PrintStream newStderr = null; 
  16. if (descriptors != null && descriptors.length >= 3) { 
  17. newStderr = new PrintStream( 
  18. new FileOutputStream(descriptors[2])); 
  19. int pid; 
  20. try { 
  21. parsedArgs = new Arguments(args); 
  22. applyUidSecurityPolicy(parsedArgs, peer); 
  23. applyDebuggerSecurityPolicy(parsedArgs); 
  24. applyRlimitSecurityPolicy(parsedArgs, peer); 
  25. applyCapabilitiesSecurityPolicy(parsedArgs, peer); 
  26. int[][] rlimits = null; 
  27. if (parsedArgs.rlimits != null) { 
  28. rlimits = parsedArgs.rlimits.toArray(intArray2d); 
  29. pid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, 
  30. parsedArgs.gids, parsedArgs.debugFlags, rlimits); 
  31. } catch (IllegalArgumentException ex) { 
  32. ...... 
  33. } catch (ZygoteSecurityException ex) { 
  34. ...... 
  35. if (pid == 0) { 
  36. // in child 
  37. handleChildProc(parsedArgs, descriptors, newStderr); 
  38. // should never happen 
  39. return true; 
  40. } else { /* pid != 0 */ 
  41. // in parent...pid of < 0 means failure 
  42. return handleParentProc(pid, descriptors, parsedArgs); 
  43. ...... 

Here is where the process is actually created:

 
 
  1. [java] view plaincopypid = Zygote.forkAndSpecialize(parsedArgs.uid, parsedArgs.gid, 
  2. parsedArgs.gids, parsedArgs.debugFlags, rlimits); 

Readers with Linux development experience can easily understand this function call. This function creates a process and has two return values. One is returned in the current process, one is returned in the newly created process, that is, returned in the current process sub-process. The return value in the current process is the pid value of the newly created sub-process, the return value in the sub-process is 0. Because we only care about the creation of new processes, we can continue to look at the sub-process execution path:

 
 
  1. [java] view plaincopy if (pid == 0) { 
  2. // in child 
  3. handleChildProc(parsedArgs, descriptors, newStderr); 
  4. // should never happen 
  5. return true; 
  6. } else { /* pid != 0 */ 
  7. ...... 

The handleChildProc function is called here.

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.