当前位置:   article > 正文

Zygote进程【2】——Zygote的分裂_starting vm process through zygote failed

starting vm process through zygote failed

欢迎转载,转载请注明:http://blog.csdn.net/zhgxhuaa


Zygote的诞生一文中init进程是如何一步步创建Zygote进程的,也了解了Zygote的进程的作用。Zygote进程的诞生对于整个Java世界可以说有着”开天辟地“的作用,它创建了Java虚拟机,并且繁殖了Java世界的核心服务system_server进程,在完成Java世界的初创工作以后,Zygote并没有死去,它只是暂时的沉睡(socket事件堵塞)在那里,一旦有需要(有客户端请求的到来),它便马上起来工作。本文接下来就将分析一下Zygote是如何监听和处理socket事件的。

首先让我们一起来回忆一下Zygote的main方法:

@frameworks/base/core/java/com/android/internal/os/ZygoteInit.java

  1.     public static void main(String argv[]) {
  2.         try {
  3.             registerZygoteSocket();//注册zygote用的socket
  4.             ......
  5.             runSelectLoop();//变成守护进程,接收socket信息进行处理
  6.             closeServerSocket();
  7.         } catch (MethodAndArgsCaller caller) {
  8.             caller.run();
  9.         } catch (RuntimeException ex) {
  10.             Log.e(TAG, "Zygote died with exception", ex);
  11.             closeServerSocket();
  12.             throw ex;
  13.         }
  14.     }

main()方法首先在registerZygoteSocket中注册了Zygote的 服务端Socket对象,然后在完成一系列初创工作后调用runSelectLoop进入到死循环中,等待客户端事件的到来。到了这里我们不禁会问,Zygote进程作为服务端,那客户端是谁呢?Zygote接收到客户端连接以后又是如何处理的呢?下面我们就带着这两个问题一起来分析。


客户端请求

@/frameworks/base/services/java/com/android/server/am/ActivityManagerService.java

  1. private final void startProcessLocked(ProcessRecord app,
  2. String hostingType, String hostingNameStr) {
  3. ......
  4. try {
  5. ......
  6. // Start the process. It will either succeed and return a result containing
  7. // the PID of the new process, or else throw a RuntimeException.
  8. Process.ProcessStartResult startResult = Process.start("android.app.ActivityThread",
  9. app.processName, uid, uid, gids, debugFlags, mountExternal,
  10. app.info.targetSdkVersion, app.info.seinfo, null);
  11. .......
  12. }

@/frameworks/base/core/java/android/os/Process.java

  1. /**
  2. * Start a new process.
  3. *
  4. * <p>If processes are enabled, a new process is created and the
  5. * static main() function of a <var>processClass</var> is executed there.
  6. * The process will continue running after this function returns.
  7. *
  8. * <p>If processes are not enabled, a new thread in the caller's
  9. * process is created and main() of <var>processClass</var> called there.
  10. *
  11. * <p>The niceName parameter, if not an empty string, is a custom name to
  12. * give to the process instead of using processClass. This allows you to
  13. * make easily identifyable processes even if you are using the same base
  14. * <var>processClass</var> to start them.
  15. *
  16. * @param processClass The class to use as the process's main entry
  17. * point.
  18. * @param niceName A more readable name to use for the process.
  19. * @param uid The user-id under which the process will run.
  20. * @param gid The group-id under which the process will run.
  21. * @param gids Additional group-ids associated with the process.
  22. * @param debugFlags Additional flags.
  23. * @param targetSdkVersion The target SDK version for the app.
  24. * @param seInfo null-ok SE Android information for the new process.
  25. * @param zygoteArgs Additional arguments to supply to the zygote process.
  26. *
  27. * @return An object that describes the result of the attempt to start the process.
  28. * @throws RuntimeException on fatal start failure
  29. *
  30. * {@hide}
  31. */
  32. public static final ProcessStartResult start(final String processClass,
  33. final String niceName,
  34. int uid, int gid, int[] gids,
  35. int debugFlags, int mountExternal,
  36. int targetSdkVersion,
  37. String seInfo,
  38. String[] zygoteArgs) {
  39. try {
  40. return startViaZygote(processClass, niceName, uid, gid, gids,
  41. debugFlags, mountExternal, targetSdkVersion, seInfo, zygoteArgs);
  42. } catch (ZygoteStartFailedEx ex) {
  43. Log.e(LOG_TAG,
  44. "Starting VM process through Zygote failed");
  45. throw new RuntimeException(
  46. "Starting VM process through Zygote failed", ex);
  47. }
  48. }
startViaZygote()方法的实现如下:

  1. /**
  2. * Starts a new process via the zygote mechanism.
  3. *
  4. * @param processClass Class name whose static main() to run
  5. * @param niceName 'nice' process name to appear in ps
  6. * @param uid a POSIX uid that the new process should setuid() to
  7. * @param gid a POSIX gid that the new process shuold setgid() to
  8. * @param gids null-ok; a list of supplementary group IDs that the
  9. * new process should setgroup() to.
  10. * @param debugFlags Additional flags.
  11. * @param targetSdkVersion The target SDK version for the app.
  12. * @param seInfo null-ok SE Android information for the new process.
  13. * @param extraArgs Additional arguments to supply to the zygote process.
  14. * @return An object that des
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/227887
推荐阅读
相关标签
  

闽ICP备14008679号