【Android系统启动】 Android Zygote 进程启动流程源码解析

深入研究源码:Android10.0系统启动流程(一) 前言 针对系统启动流程的专题研究也进行了差不多一周了,虽然进度缓慢,但也算是有一些阅读源码的心得,以及对源码的一点理解 由于系统启动流程涉及的源码相对较多,所以决定分几篇文章来写,本篇主要是对启动流程进行一个简单的概述,以及谈一下我个人的读源码的方法 正文 启动流程概述 这里说的启动流程是指Android系统的启动流程 让我们来回想一下,我们是如何“启动”一个全新未开封的Android手机的: 按电源键 进入开机动画 经过漫长的等待(大概几分钟?哈哈) 开机动画结束 正式开机,进入设置画面 进入系. 阅读详情

【Android系统启动】 Android Zygote 进程启动流程源码解析

前言

  1. 文章源码按照AOSP官网源码platform/superproject/main - Android Code Search代码进行解读;
  2. 如有不当之处,麻烦指出作者进行修正

代码位置

  1. frameworks/base/cmds/app_process/app_main.cpp

题外话, 之前找zygote源码的时候,本来想直接通过进程名找这个模块的源码,结果发现根本没有zygote这个名字的命令。通过命令查找到对应的进程目录发现,原来的进程的名字当时编译为app_process,实际文件一般在/system/bin下面。

源码解析

Init拉起zygote

在system/core/rootdir/init.rc中,

# It is recommended to put unnecessary data/ initialization from post-fs-data
# to start-zygote in device's init.rc to unblock zygote start.
on zygote-start
    wait_for_prop odsign.verification.done 1
    # A/B update verifier that marks a successful boot.
    exec_start update_verifier
    start statsd
    start netd
    start zygote
    start zygote_secondary

根据之前init.rc的官方README说明文档,以及源码解析,均可得知这里start zygote开始了zygote服务;分析见下方。

Start a service running if it is not already running. Note that this is not synchronous, and even if it were, there is no guarantee that the operating system’s scheduler will execute the service sufficiently to guarantee anything about the service’s status. See the exec_start command for a synchronous version of start.
如果服务尚未运行,则启动该服务。
请注意,这不是同步的,即使它是同步的,也不能保证操作系统的调度程序会充分执行服务以确保服务的状态。
参见 exec_start 命令,以获取 start 的同步版本。

在init第二阶段的时候,有获取内置命令的逻辑,这部分命令用于解析执行init.rc

// system/core/init/init.cpp
	const BuiltinFunctionMap& function_map = GetBuiltinFunctionMap();
    Action::set_function_map(&function_map);

这里的Map实际对应下面这个Map,并且可以得知start方法调用的是do_start;

do_start先从服务列表里面找到这个服务,然后调用封装好的Service::Start()方法进行调用。


// system/core/init/builtins.cpp
// Builtin-function-map start
const BuiltinFunctionMap& GetBuiltinFunctionMap() {
    constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max();
    // clang-format off
    static const BuiltinFunctionMap builtin_functions = {
        {"bootchart",               {1,     1,    {false,  do_bootchart}}},
        {"chmod",                   {2,     2,    {true,   do_chmod}}},
        {"chown",                   {2,     3,    {true,   do_chown}}},
        {"class_reset",             {1,     1,    {false,  do_class_reset}}},
        {"class_restart",           {1,     2,    {false,  do_class_restart}}},
        {"class_start",             {1,     1,    {false,  do_class_start}}},
        {"class_stop",              {1,     1,    {false,  do_class_stop}}},
        {"copy",                    {2,     2,    {true,   do_copy}}},
        {"copy_per_line",           {2,     2,    {true,   do_copy_per_line}}},
        {"domainname",              {1,     1,    {true,   do_domainname}}},
        {"enable",                  {1,     1,    {false,  do_enable}}},
        {"exec",                    {1,     kMax, {false,  do_exec}}},
        {"exec_background",         {1,     kMax, {false,  do_exec_background}}},
        {"exec_start",              {1,     1,    {false,  do_exec_start}}},
        {"export",                  {2,     2,    {false,  do_export}}},
        {"hostname",                {1,     1,    {true,   do_hostname}}},
        {"ifup",                    {1,     1,    {true,   do_ifup}}},
        {"init_user0",              {0,     0,    {false,  do_init_user0}}},
        {"insmod",                  {1,     kMax, {true,   do_insmod}}},
        {"installkey",              {1,     1,    {false,  do_installkey}}},
        {"interface_restart",       {1,     1,    {false,  do_interface_restart}}},
        {"interface_start",         {1,     1,    {false,  do_interface_start}}},
        {"interface_stop",          {1,     1,    {false,  do_interface_stop}}},
        {"load_exports",            {1,     1,    {false,  do_load_exports}}},
        {"load_persist_props",      {0,     0,    {false,  do_load_persist_props}}},
        {"load_system_props",       {0,     0,    {false,  do_load_system_props}}},
        {"loglevel",                {1,     1,    {false,  do_loglevel}}},
        {"mark_post_data",          {0,     0,    {false,  do_mark_post_data}}},
        {"mkdir",                   {1,     6,    {true,   do_mkdir}}},
        // TODO: Do mount operations in vendor_init.
        // mount_all is currently too complex to run in vendor_init as it queues action triggers,
        // imports rc scripts, etc.  It should be simplified and run in vendor_init context.
        // mount and umount are run in the same context as mount_all for symmetry.
        {"mount_all",               {0,     kMax, {false,  do_mount_all}}},
        {"mount",                   {3,     kMax, {false,  do_mount}}},
        {"perform_apex_config",     {0,     1,    {false,  do_perform_apex_config}}},
        {"umount",                  {1,     1,    {false,  do_umount}}},
        {"umount_all",              {0,     1,    {false,  do_umount_all}}},
        {"update_linker_config",    {0,     0,    {false,  do_update_linker_config}}},
        {"readahead",               {1,     2,    {true,   do_readahead}}},
        {"remount_userdata",        {0,     0,    {false,  do_remount_userdata}}},
        {"restart",                 {1,     2,    {false,  do_restart}}},
        {"restorecon",              {1,     kMax, {true,   do_restorecon}}},
        {"restorecon_recursive",    {1,     kMax, {true,   do_restorecon_recursive}}},
        {"rm",                      {1,     1,    {true,   do_rm}}},
        {"rmdir",                   {1,     1,    {true,   do_rmdir}}},
        {"setprop",                 {2,     2,    {true,   do_setprop}}},
        {"setrlimit",               {3,     3,    {false,  do_setrlimit}}},
        {"start",                   {1,     1,    {false,  do_start}}},
        {"stop",                    {1,     1,    {false,  do_stop}}},
        {"swapon_all",              {0,     1,    {false,  do_swapon_all}}},
        {"enter_default_mount_ns",  {0,     0,    {false,  do_enter_default_mount_ns}}},
        {"symlink",                 {2,     2,    {true,   do_symlink}}},
        {"sysclktz",                {1,     1,    {false,  do_sysclktz}}},
        {"trigger",                 {1,     1,    {false,  do_trigger}}},
        {"verity_update_state",     {0,     0,    {false,  do_verity_update_state}}},
        {"wait",                    {1,     2,    {true,   do_wait}}},
        {"wait_for_prop",           {2,     2,    {false,  do_wait_for_prop}}},
        {"write",                   {2,     2,    {true,   do_write}}},
    };
    // clang-format on
    return builtin_functions;
}

static Result<void> do_start(const BuiltinArguments& args) {
    Service* svc = ServiceList::GetInstance().FindService(args[1]);
    if (!svc) return Error() << "service " << args[1] << " not found";
    errno = 0;
    if (auto result = svc->Start(); !result.ok()) {
        return ErrorIgnoreEnoent() << "Could not start service: " << result.error();
    }
    return {};
}

Service::Start()比较冗长,省略了部分代码;其实质是通过判断配置值决定是调用 fork() 还是 clone() 来创建子进程或克隆进程。

Result<void> Service::Start() {
    
    // ...
    
    pid_t pid = -1;
    if (namespaces_.flags) {
        pid = clone(nullptr, nullptr, namespaces_.flags | SIGCHLD, nullptr);
    } else {
        pid = fork();
    }

    if (pid == 0) {
        umask(077);
        cgroups_activated.CloseWriteFd();
        setsid_finished.CloseReadFd();
        RunService(descriptors, std::move(cgroups_activated), std::move(setsid_finished));
        _exit(127);
    } else {
        cgroups_activated.CloseReadFd();
        setsid_finished.CloseWriteFd();
    }

    if (pid < 0) {
        pid_ = 0;
        return ErrnoError() << "Failed to fork";
    }

    // ...

    LOG(INFO) << "... started service '" << name_ << "' has pid " << pid_;

    return {};
}

然后补充一下,这个是system/core/rootdir/init.zygote32.rc,zygote通过initr.rc拉起时会逐一解析这些命令并且启动。(如果是64位系统,则为init.zygote64.rc)

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
    class main
    priority -20
    user root
    group root readproc reserved_disk
    socket zygote stream 660 root system
    socket usap_pool_primary stream 660 root system
    onrestart exec_background - system system -- /system/bin/vdc volume abort_fuse
    onrestart write /sys/power/state on
    # NOTE: If the wakelock name here is changed, then also
    # update it in SystemSuspend.cpp
    onrestart write /sys/power/wake_lock zygote_kwl
    onrestart restart audioserver
    onrestart restart cameraserver
    onrestart restart media
    onrestart restart media.tuner
    onrestart restart netd
    onrestart restart wificond
    task_profiles ProcessCapacityHigh
    critical window=${zygote.critical_window.minute:-off} target=zygote-fatal

Zygote Main

上面已经讲完Init如何拉起zygote了。下面进入zygote模块本身的代码。从进程名查找到该命令为app_process,然后再从bp或者mk源码中找app_process,即可找到这个命令的位置了。

int main(int argc, char* const argv[])
{
    
    // --------- LOG -----------
    if (!LOG_NDEBUG) {
      String8 argv_String;
      for (int i = 0; i < argc; ++i) {
        argv_String.append("\"");
        argv_String.append(argv[i]);
        argv_String.append("\" ");
      }
      ALOGV("app_process main with argv: %s", argv_String.c_str());
    }

    // ------------ Runtime初始化 ------------
    AppRuntime runtime(argv[0], computeArgBlockSize(argc, argv));
    // Process command line arguments
    // ignore argv[0]
    argc--;
    argv++;

    // Everything up to '--' or first non '-' arg goes to the vm.
    //
    // The first argument after the VM args is the "parent dir", which
    // is currently unused.
    //
    // After the parent dir, we expect one or more the following internal
    // arguments :
    //
    // --zygote : Start in zygote mode
    // --start-system-server : Start the system server.
    // --application : Start in application (stand alone, non zygote) mode.
    // --nice-name : The nice name for this process.
    //
    // For non zygote starts, these arguments will be followed by
    // the main class name. All remaining arguments are passed to
    // the main method of this class.
    //
    // For zygote starts, all remaining arguments are passed to the zygote.
    // main function.
    //
    // Note that we must copy argument string values since we will rewrite the
    // entire argument block when we apply the nice name to argv0.
    //
    // As an exception to the above rule, anything in "spaced commands"
    // goes to the vm even though it has a space in it.
    const char* spaced_commands[] = { "-cp", "-classpath" };
    // Allow "spaced commands" to be succeeded by exactly 1 argument (regardless of -s).
    bool known_command = false;

    // ------------ 解析参数
    int i;
    for (i = 0; i < argc; i++) {
        if (known_command == true) {
          runtime.addOption(strdup(argv[i]));
          // The static analyzer gets upset that we don't ever free the above
          // string. Since the allocation is from main, leaking it doesn't seem
          // problematic. NOLINTNEXTLINE
          ALOGV("app_process main add known option '%s'", argv[i]);
          known_command = false;
          continue;
        }

        for (int j = 0;
             j < static_cast<int>(sizeof(spaced_commands) / sizeof(spaced_commands[0]));
             ++j) {
          if (strcmp(argv[i], spaced_commands[j]) == 0) {
            known_command = true;
            ALOGV("app_process main found known command '%s'", argv[i]);
          }
        }

        if (argv[i][0] != '-') {
            break;
        }
        if (argv[i][1] == '-' && argv[i][2] == 0) {
            ++i; // Skip --.
            break;
        }

        runtime.addOption(strdup(argv[i]));
        // The static analyzer gets upset that we don't ever free the above
        // string. Since the allocation is from main, leaking it doesn't seem
        // problematic. NOLINTNEXTLINE
        ALOGV("app_process main add option '%s'", argv[i]);
    }

    // Parse runtime arguments.  Stop at first unrecognized option.
    bool zygote = false;
    bool startSystemServer = false;
    bool application = false;
    String8 niceName;
    String8 className;

    ++i;  // Skip unused "parent dir" argument.
    while (i < argc) {
        const char* arg = argv[i++];
        if (strcmp(arg, "--zygote") == 0) {
            zygote = true;
            niceName = ZYGOTE_NICE_NAME;
        } else if (strcmp(arg, "--start-system-server") == 0) {
            startSystemServer = true;
        } else if (strcmp(arg, "--application") == 0) {
            application = true;
        } else if (strncmp(arg, "--nice-name=", 12) == 0) {
            niceName = (arg + 12);
        } else if (strncmp(arg, "--", 2) != 0) {
            className = arg;
            break;
        } else {
            --i;
            break;
        }
    }

    Vector<String8> args;
    if (!className.empty()) {
        // We're not in zygote mode, the only argument we need to pass
        // to RuntimeInit is the application argument.
        //
        // The Remainder of args get passed to startup class main(). Make
        // copies of them before we overwrite them with the process name.
        args.add(application ? String8("application") : String8("tool"));
        runtime.setClassNameAndArgs(className, argc - i, argv + i);

        if (!LOG_NDEBUG) {
          String8 restOfArgs;
          char* const* argv_new = argv + i;
          int argc_new = argc - i;
          for (int k = 0; k < argc_new; ++k) {
            restOfArgs.append("\"");
            restOfArgs.append(argv_new[k]);
            restOfArgs.append("\" ");
          }
          ALOGV("Class name = %s, args = %s", className.c_str(), restOfArgs.c_str());
        }
    } else {
        // We're in zygote mode.
        maybeCreateDalvikCache();

        if (startSystemServer) {
            args.add(String8("start-system-server"));
        }

        char prop[PROP_VALUE_MAX];
        if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
            LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
                ABI_LIST_PROPERTY);
            return 11;
        }

        String8 abiFlag("--abi-list=");
        abiFlag.append(prop);
        args.add(abiFlag);

        // In zygote mode, pass all remaining arguments to the zygote
        // main() method.
        for (; i < argc; ++i) {
            args.add(String8(argv[i]));
        }
    }

    if (!niceName.empty()) {
        runtime.setArgv0(niceName.c_str(), true /* setProcName */);
    }

    // -------------------- runtime start 
    if (zygote) {
        runtime.start("com.android.internal.os.ZygoteInit", args, zygote);
    } else if (!className.empty()) {
        runtime.start("com.android.internal.os.RuntimeInit", args, zygote);
    } else {
        fprintf(stderr, "Error: no class name or --zygote supplied.\n");
        app_usage();
        LOG_ALWAYS_FATAL("app_process: no class name or --zygote supplied.");
    }
}

上面主要干了这几件大事:

Debug Logging:如果 LOG_NDEBUG 为 false,将命令行参数以调试日志的形式输出。

AppRuntime 初始化:创建 AppRuntime 实例,用于处理应用程序的运行时环境。

处理命令行参数

  • 忽略程序名 argv[0]。
  • 将一些特定的命令行参数添加到运行时选项中。
  • 根据 “–” 或非 ‘-’ 开头的参数分隔命令行参数,一部分传递给虚拟机(VM),一部分用于启动类的 main() 方法。

解析运行时参数

  • 根据特定参数设置标志和属性(如 --zygote、--start-system-server、--application、--nice-name=<name>)。
  • 如果遇到 “–” 开头的参数或非 ‘-’ 开头的参数,则停止解析。

准备运行时参数列表

  • 如果存在类名 (className),则将其作为参数传递给 RuntimeInit。
  • 在 zygote 模式下,准备 ABI 列表和其他参数传递给 ZygoteInit。

设置进程名称

  • 如果指定了 --nice-name=<name>,则设置进程名称。

根据模式调用 runtime 的 start() 方法

  • 如果是 zygote 模式,则调用 start() 方法启动 ZygoteInit。
  • 如果存在类名 (className),则调用 start() 方法启动 RuntimeInit。
  • 否则,输出错误信息并退出程序。

总的来说就是,准备AppRuntime,解析参数,设置进程名,调用runtime start()方法。

从上面zygote的rc文件也可以看到,一开始的参数为–start-system-server

service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server

AppRuntime start()

frameworks/base/core/jni/AndroidRuntime.cpp

/*
 * Start the Android runtime.  This involves starting the virtual machine
 * and calling the "static void main(String[] args)" method in the class
 * named by "className".
 *
 * Passes the main function two arguments, the class name and the specified
 * options string.
 */
void AndroidRuntime::start(const char* className, const Vector<String8>& options, bool zygote)
{
    // 打印启动信息,包括类名和用户ID
    ALOGD(">>>>>> START %s uid %d <<<<<<\n",
            className != NULL ? className : "(unknown)", getuid());

    // 判断是否是主要的 zygote 进程
    static const String8 startSystemServer("start-system-server");
    bool primary_zygote = false;

    // 检查是否需要打印启动事件
    for (size_t i = 0; i < options.size(); ++i) {
        if (options[i] == startSystemServer) {
            primary_zygote = true;
            const int LOG_BOOT_PROGRESS_START = 3000;
            LOG_EVENT_LONG(LOG_BOOT_PROGRESS_START,  ns2ms(systemTime(SYSTEM_TIME_MONOTONIC)));
        }
    }

    // 设置环境变量
    const char* rootDir = getenv("ANDROID_ROOT");
    if (rootDir == NULL) {
        rootDir = "/system";
        if (!hasDir("/system")) {
            LOG_FATAL("No root directory specified, and /system does not exist.");
            return;
        }
        setenv("ANDROID_ROOT", rootDir, 1);
    }

    const char* artRootDir = getenv("ANDROID_ART_ROOT");
    if (artRootDir == NULL) {
        LOG_FATAL("No ART directory specified with ANDROID_ART_ROOT environment variable.");
        return;
    }

    const char* i18nRootDir = getenv("ANDROID_I18N_ROOT");
    if (i18nRootDir == NULL) {
        LOG_FATAL("No runtime directory specified with ANDROID_I18N_ROOT environment variable.");
        return;
    }

    const char* tzdataRootDir = getenv("ANDROID_TZDATA_ROOT");
    if (tzdataRootDir == NULL) {
        LOG_FATAL("No tz data directory specified with ANDROID_TZDATA_ROOT environment variable.");
        return;
    }

    // 初始化 JNI 调用
    JniInvocation jni_invocation;
    jni_invocation.Init(NULL);
    JNIEnv* env;

    // 启动虚拟机
    if (startVm(&mJavaVM, &env, zygote, primary_zygote) != 0) {
        return;
    }
    onVmCreated(env);

    // 注册 Android 原生方法
    if (startReg(env) < 0) {
        ALOGE("Unable to register all android natives\n");
        return;
    }

    // 创建 String 数组,用于传递给 Java 的 main 方法
    jclass stringClass;
    jobjectArray strArray;
    jstring classNameStr;

    stringClass = env->FindClass("java/lang/String");
    assert(stringClass != NULL);
    strArray = env->NewObjectArray(options.size() + 1, stringClass, NULL);
    assert(strArray != NULL);
    classNameStr = env->NewStringUTF(className);
    assert(classNameStr != NULL);
    env->SetObjectArrayElement(strArray, 0, classNameStr);

    for (size_t i = 0; i < options.size(); ++i) {
        jstring optionsStr = env->NewStringUTF(options.itemAt(i).c_str());
        assert(optionsStr != NULL);
        env->SetObjectArrayElement(strArray, i + 1, optionsStr);
    }

    // 调用 Java 的 main 方法启动应用程序
    char* slashClassName = toSlashClassName(className != NULL ? className : "");
    jclass startClass = env->FindClass(slashClassName);
    if (startClass == NULL) {
        ALOGE("JavaVM unable to locate class '%s'\n", slashClassName);
        /* keep going */
    } else {
        jmethodID startMeth = env->GetStaticMethodID(startClass, "main",
            "([Ljava/lang/String;)V");
        if (startMeth == NULL) {
            ALOGE("JavaVM unable to find main() in '%s'\n", className);
            /* keep going */
        } else {
            env->CallStaticVoidMethod(startClass, startMeth, strArray);
        }
    }
    free(slashClassName);

    ALOGD("Shutting down VM\n");
    // 分离主线程
    if (mJavaVM->DetachCurrentThread() != JNI_OK)
        ALOGW("Warning: unable to detach main thread\n");
    // 销毁虚拟机
    if (mJavaVM->DestroyJavaVM() != 0)
        ALOGW("Warning: VM did not shut down cleanly\n");
}

这里的主要职责:

  1. 初始化:打印启动信息、判断是否为systemserver进程、设置环境变量等;
  2. 初始化 JNI 调用和启动虚拟机
    1. 使用 JniInvocation 初始化 JNI 调用;
    2. 调用 startVm 方法启动虚拟机,并在虚拟机创建后调用 onVmCreated;
    3. 调用 startReg 方法注册 Android 原生方法,里面遍历调用gRegJNI数组,数组是一组 JNI 函数的函数指针;
  3. 创建并传递参数给 Java 的 main 方法
    1. 创建 String 类型的数组 strArray,用于传递给 Java 的 main 方法;
    2. 将类名和选项字符串转换为 Java 的 String 对象,并设置到 strArray 中;
  4. 调用 Java 的 main 方法
    1. 根据类名查找 startClass,并获取 main 方法的 startMeth;
    2. 使用 env->CallStaticVoidMethod 调用 Java 的 main 方法启动应用程序;
  5. 关闭虚拟机
    1. 执行完 main 方法后,分离主线程并销毁虚拟机。

这里总体就是初始化打印、环境变量、参数等,然后启动虚拟机-传递参数-调用main方法-关闭虚拟机。

不过通过观察这里会发现:

  1. 每次上层的进程启动会调用这个start方法,而start方法开启且仅开启了一个虚拟机,那就是zygote基础上每一个进程,都会对应一个自己的虚拟环境;
  2. 每个上层应用需要逐步调到系统层,都通过zygote这里JNI方式进行了调用。

ZygoteInit.Main()

通过上面传下来的参数,“com.android.internal.os.ZygoteInit”,可以得知Java层的包名,以此找到Java代码。

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

/**
 * This is the entry point for a Zygote process.  It creates the Zygote server, loads resources,
 * and handles other tasks related to preparing the process for forking into applications.
 *
 * This process is started with a nice value of -20 (highest priority).  All paths that flow
 * into new processes are required to either set the priority to the default value or terminate
 * before executing any non-system code.  The native side of this occurs in SpecializeCommon,
 * while the Java Language priority is changed in ZygoteInit.handleSystemServerProcess,
 * ZygoteConnection.handleChildProc, and Zygote.childMain.
 *
 * @param argv  Command line arguments used to specify the Zygote's configuration.
 */
@UnsupportedAppUsage
public static void main(String[] argv) {
    ZygoteServer zygoteServer = null;

    // 标记 Zygote 启动 确保在此后创建线程会抛出错误。
    // Mark zygote start. This ensures that thread creation will throw
    // an error.
    ZygoteHooks.startZygoteNoThreadCreation();

    // 调用 Os.setpgid(0, 0) 将 Zygote 放入自己的进程组。
    // Zygote goes into its own process group.
    try {
        Os.setpgid(0, 0);
    } catch (ErrnoException ex) {
        throw new RuntimeException("Failed to setpgid(0,0)", ex);
    }

    Runnable caller;
    try {
        // 解析命令行参数 argv,包括是否启动系统服务器 (start-system-server)、是否启用延迟预加载 (--enable-lazy-preload)、ABI 列表等。
        // Store now for StatsLogging later.
        final long startTime = SystemClock.elapsedRealtime();
        final boolean isRuntimeRestarted = "1".equals(
                SystemProperties.get("sys.boot_completed"));

        String bootTimeTag = Process.is64Bit() ? "Zygote64Timing" : "Zygote32Timing";
        TimingsTraceLog bootTimingsTraceLog = new TimingsTraceLog(bootTimeTag,
                Trace.TRACE_TAG_DALVIK);
        bootTimingsTraceLog.traceBegin("ZygoteInit");
        RuntimeInit.preForkInit();

        boolean startSystemServer = false;
        String zygoteSocketName = "zygote";
        String abiList = null;
        boolean enableLazyPreload = false;
        for (int i = 1; i < argv.length; i++) {
            if ("start-system-server".equals(argv[i])) {
                startSystemServer = true;
            } else if ("--enable-lazy-preload".equals(argv[i])) {
                enableLazyPreload = true;
            } else if (argv[i].startsWith(ABI_LIST_ARG)) {
                abiList = argv[i].substring(ABI_LIST_ARG.length());
            } else if (argv[i].startsWith(SOCKET_NAME_ARG)) {
                zygoteSocketName = argv[i].substring(SOCKET_NAME_ARG.length());
            } else {
                throw new RuntimeException("Unknown command line argument: " + argv[i]);
            }
        }

        final boolean isPrimaryZygote = zygoteSocketName.equals(Zygote.PRIMARY_SOCKET_NAME);
        if (!isRuntimeRestarted) {
            if (isPrimaryZygote) {
                FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                        BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__ZYGOTE_INIT_START,
                        startTime);
            } else if (zygoteSocketName.equals(Zygote.SECONDARY_SOCKET_NAME)) {
                FrameworkStatsLog.write(FrameworkStatsLog.BOOT_TIME_EVENT_ELAPSED_TIME_REPORTED,
                        BOOT_TIME_EVENT_ELAPSED_TIME__EVENT__SECONDARY_ZYGOTE_INIT_START,
                        startTime);
            }
        }

        if (abiList == null) {
            throw new RuntimeException("No ABI list supplied.");
        }

        // 记录启动时间、预加载
        // In some configurations, we avoid preloading resources and classes eagerly.
        // In such cases, we will preload things prior to our first fork.
        // 根据配置,预加载资源和类。如果未启用延迟预加载,则在第一次fork()之前预加载。
        if (!enableLazyPreload) {
            bootTimingsTraceLog.traceBegin("ZygotePreload");
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_START,
                    SystemClock.uptimeMillis());
            preload(bootTimingsTraceLog);
            EventLog.writeEvent(LOG_BOOT_PROGRESS_PRELOAD_END,
                    SystemClock.uptimeMillis());
            bootTimingsTraceLog.traceEnd(); // ZygotePreload
        }

        // Do an initial gc to clean up after startup
        bootTimingsTraceLog.traceBegin("PostZygoteInitGC");
        gcAndFinalize();
        bootTimingsTraceLog.traceEnd(); // PostZygoteInitGC

        bootTimingsTraceLog.traceEnd(); // ZygoteInit

        // 初始化native状态
        Zygote.initNativeState(isPrimaryZygote);

        // 停止 Zygote 无线程创建标记
        ZygoteHooks.stopZygoteNoThreadCreation();

        // 创建 ZygoteServer 实例。
        zygoteServer = new ZygoteServer(isPrimaryZygote);

        
        // 如果需要启动SystemServer,则调用 forkSystemServer 创建SystemServer的子进程,并在子进程中运行。
        if (startSystemServer) {
            Runnable r = forkSystemServer(abiList, zygoteSocketName, zygoteServer);

            // {@code r == null} in the parent (zygote) process, and {@code r != null} in the
            // child (system_server) process.
            if (r != null) {
                r.run();
                return;
            }
        }

        
        // 接受命令套接字连接
        Log.i(TAG, "Accepting command socket connections");

        // select 循环在 fork 后在子进程中提前返回,并在 zygote 中永远循环
        // The select loop returns early in the child process after a fork and
        // loops forever in the zygote.
        caller = zygoteServer.runSelectLoop(abiList);
    } catch (Throwable ex) {
        Log.e(TAG, "System zygote died with fatal exception", ex);
        throw ex;
    } finally {
        // 异常处理和资源清理 在最后关闭 zygoteServer 的服务器套接字。
        if (zygoteServer != null) {
            zygoteServer.closeServerSocket();
        }
    }

    // We're in the child process and have exited the select loop. Proceed to execute the
    // command.
    // 执行命令 如果 caller 不为 null,则在子进程中执行命令。
    if (caller != null) {
        caller.run();
    }
}

补充一下注释翻译(这里告诉了修改进程优先级的native层和java层的地方):

这是 Zygote 进程的入口点。它创建 Zygote 服务器、加载资源并处理与准备进程以分叉到应用程序相关的其他任务。

此进程以 -20(最高优先级)的 nice 值启动。所有流入新进程的路径都必须将优先级设置为默认值或在执行任何非系统代码之前终止。native方面发生在 SpecializeCommon 中, 而 Java 语言优先级在 ZygoteInit.handleSystemServerProcess、ZygoteConnection.handleChildProc 和 Zygote.childMain 中更改。

这里主要的职责有:

  1. 初始化:标记zygote无线程启动、设置进程组、处理命令行参数;

    int setpgid(pid_t pid, pid_t pgid) 可以将一个进程加入一个现有的进程组,或者创建一个新的进程组且当前调用进程作为进程组组长(setpgid(0, 0))。pid 参数只能是 0 (当前进程)、当前进程 id、子进程 id(孙子进程不行)。

  2. 记录启动时间,并根据是否为主要 Zygote 进程写入启动时间事件;

  3. 预加载,根据配置在第一次fork前预加载;

  4. 初始化native层状态;

  5. 停止标记zygote无线程启动;

  6. 创建 ZygoteServer,并且启动systemserver;

  7. zygoteServer.runSelectLoop(abiList),建立systemserver和zygote的socket通信;

  8. 关闭zygoteserver的socket;

然后挑一些重要的职责细讲一下:

ZygoteInit PreLoad

这里预加载的主要是一些系统类、资源、共享库so、文字、webview等等;

    static void preload(TimingsTraceLog bootTimingsTraceLog) {
        Log.d(TAG, "begin preload");
        bootTimingsTraceLog.traceBegin("BeginPreload");
        beginPreload();
        bootTimingsTraceLog.traceEnd(); // BeginPreload
        bootTimingsTraceLog.traceBegin("PreloadClasses");
        preloadClasses();
        bootTimingsTraceLog.traceEnd(); // PreloadClasses
        bootTimingsTraceLog.traceBegin("CacheNonBootClasspathClassLoaders");
        cacheNonBootClasspathClassLoaders();
        bootTimingsTraceLog.traceEnd(); // CacheNonBootClasspathClassLoaders
        bootTimingsTraceLog.traceBegin("PreloadResources");
        Resources.preloadResources();
        bootTimingsTraceLog.traceEnd(); // PreloadResources
        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadAppProcessHALs");
        nativePreloadAppProcessHALs();
        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
        Trace.traceBegin(Trace.TRACE_TAG_DALVIK, "PreloadGraphicsDriver");
        maybePreloadGraphicsDriver();
        Trace.traceEnd(Trace.TRACE_TAG_DALVIK);
        preloadSharedLibraries();
        preloadTextResources();
        // Ask the WebViewFactory to do any initialization that must run in the zygote process,
        // for memory sharing purposes.
        WebViewFactory.prepareWebViewInZygote();
        endPreload();
        warmUpJcaProviders();
        Log.d(TAG, "end preload");

        sPreloadComplete = true;
    }

而且preload也有个前提条件 enableLazyPreload = true --> 带有–enable-lazy-preload,只有system/core/rootdir/init.zygote64_32.rc才有这个参数 --> 只有在32和64位均需要启动的时候,才需要preload;

  • init.zygote32.rc:zygote 进程对应的执行程序是 app_process (纯 32bit 模式)
  • init.zygote64.rc:zygote 进程对应的执行程序是 app_process64 (纯 64bit 模式)
  • init.zygote32_64.rc:启动两个 zygote 进程 (名为 zygote 和 zygote_secondary),对应的执行程序分别是 app_process32 (主模式)、app_process64
  • init.zygote64_32.rc:启动两个 zygote 进程 (名为 zygote 和 zygote_secondary),对应的执行程序分别是 app_process64 (主模式)、app_process32
ZygoteInit forkSystemServer
    /**
     * Prepare the arguments and forks for the system server process.
     *
     * @return A {@code Runnable} that provides an entrypoint into system_server code in the child
     * process; {@code null} in the parent.
     */
    private static Runnable forkSystemServer(String abiList, String socketName,
            ZygoteServer zygoteServer) {
        long capabilities = posixCapabilitiesAsBits(
                OsConstants.CAP_IPC_LOCK,
                OsConstants.CAP_KILL,
                OsConstants.CAP_NET_ADMIN,
                OsConstants.CAP_NET_BIND_SERVICE,
                OsConstants.CAP_NET_BROADCAST,
                OsConstants.CAP_NET_RAW,
                OsConstants.CAP_SYS_MODULE,
                OsConstants.CAP_SYS_NICE,
                OsConstants.CAP_SYS_PTRACE,
                OsConstants.CAP_SYS_TIME,
                OsConstants.CAP_SYS_TTY_CONFIG,
                OsConstants.CAP_WAKE_ALARM,
                OsConstants.CAP_BLOCK_SUSPEND
        );
        /* Containers run without some capabilities, so drop any caps that are not available. */
        StructCapUserHeader header = new StructCapUserHeader(
                OsConstants._LINUX_CAPABILITY_VERSION_3, 0);
        StructCapUserData[] data;
        try {
            data = Os.capget(header);
        } catch (ErrnoException ex) {
            throw new RuntimeException("Failed to capget()", ex);
        }
        capabilities &= Integer.toUnsignedLong(data[0].effective) |
                (Integer.toUnsignedLong(data[1].effective) << 32);

        /* Hardcoded command line to start the system server */
        String[] args = {
                "--setuid=1000",
                "--setgid=1000",
                "--setgroups=1001,1002,1003,1004,1005,1006,1007,1008,1009,1010,1018,1021,1023,"
                        + "1024,1032,1065,3001,3002,3003,3005,3006,3007,3009,3010,3011,3012",
                "--capabilities=" + capabilities + "," + capabilities,
                "--nice-name=system_server",
                "--runtime-args",
                "--target-sdk-version=" + VMRuntime.SDK_VERSION_CUR_DEVELOPMENT,
                "com.android.server.SystemServer",
        };
        ZygoteArguments parsedArgs;

        int pid;

        try {
            ZygoteCommandBuffer commandBuffer = new ZygoteCommandBuffer(args);
            try {
                parsedArgs = ZygoteArguments.getInstance(commandBuffer);
            } catch (EOFException e) {
                throw new AssertionError("Unexpected argument error for forking system server", e);
            }
            commandBuffer.close();
            Zygote.applyDebuggerSystemProperty(parsedArgs);
            Zygote.applyInvokeWithSystemProperty(parsedArgs);

            if (Zygote.nativeSupportsMemoryTagging()) {
                String mode = SystemProperties.get("persist.arm64.memtag.system_server", "");
                if (mode.isEmpty()) {
                  /* The system server has ASYNC MTE by default, in order to allow
                   * system services to specify their own MTE level later, as you
                   * can't re-enable MTE once it's disabled. */
                  mode = SystemProperties.get("persist.arm64.memtag.default", "async");
                }
                if (mode.equals("async")) {
                    parsedArgs.mRuntimeFlags |= Zygote.MEMORY_TAG_LEVEL_ASYNC;
                } else if (mode.equals("sync")) {
                    parsedArgs.mRuntimeFlags |= Zygote.MEMORY_TAG_LEVEL_SYNC;
                } else if (!mode.equals("off")) {
                    /* When we have an invalid memory tag level, keep the current level. */
                    parsedArgs.mRuntimeFlags |= Zygote.nativeCurrentTaggingLevel();
                    Slog.e(TAG, "Unknown memory tag level for the system server: \"" + mode + "\"");
                }
            } else if (Zygote.nativeSupportsTaggedPointers()) {
                /* Enable pointer tagging in the system server. Hardware support for this is present
                 * in all ARMv8 CPUs. */
                parsedArgs.mRuntimeFlags |= Zygote.MEMORY_TAG_LEVEL_TBI;
            }

            /* Enable gwp-asan on the system server with a small probability. This is the same
             * policy as applied to native processes and system apps. */
            parsedArgs.mRuntimeFlags |= Zygote.GWP_ASAN_LEVEL_LOTTERY;

            if (shouldProfileSystemServer()) {
                parsedArgs.mRuntimeFlags |= Zygote.PROFILE_SYSTEM_SERVER;
            }

            /* Request to fork the system server process */
            pid = Zygote.forkSystemServer(
                    parsedArgs.mUid, parsedArgs.mGid,
                    parsedArgs.mGids,
                    parsedArgs.mRuntimeFlags,
                    null,
                    parsedArgs.mPermittedCapabilities,
                    parsedArgs.mEffectiveCapabilities);
        } catch (IllegalArgumentException ex) {
            throw new RuntimeException(ex);
        }

        /* For child process */
        if (pid == 0) {
            if (hasSecondZygote(abiList)) {
                waitForSecondaryZygote(socketName);
            }

            zygoteServer.closeServerSocket();
            return handleSystemServerProcess(parsedArgs);
        }

        return null;
    }

里面的整体逻辑还是很简单的 , 就是整理出参数,然后调用zygote的方法;主要调用了applyDebuggerSystemProperty、applyInvokeWithSystemProperty、forkSystemServer方法,

  1. applyDebuggerSystemProperty:

    Applies debugger system properties to the zygote arguments.
    For eng builds all apps are debuggable with JDWP and ptrace.
    On userdebug builds if persist.debug.dalvik.vm.jdwp.enabled is 1 all apps are debuggable with JDWP and ptrace. Otherwise, the debugger state is specified via the “–enable-jdwp” flag in the
    spawn request.
    On userdebug builds if persist.debug.ptrace.enabled is 1 all apps are debuggable with ptrace.

    将调试器系统属性应用于 zygote 参数。
    对于 eng 版本,所有应用均可使用 JDWP 和 ptrace 进行调试。
    在 userdebug 版本中,如果 persist.debug.dalvik.vm.jdwp.enabled 为 1,则所有应用均可使用 JDWP 和 ptrace 进行调试。否则,调试器状态通过 spawn 请求中的“–enable-jdwp”标志指定。
    在 userdebug 版本中,如果 persist.debug.ptrace.enabled 为 1,则所有应用均可使用 ptrace 进行调试。

  2. applyInvokeWithSystemProperty:将invoke-with系统属性应用于zygote参数

  3. forkSystemServer 最终会调用到native层,fork出子进程;

  4. closeServerSocket 关闭socket通信;

  5. handleSystemServerProcess:完成新建子进程后剩余工作;

Q&A

为什么zygote要用socket,而不是用binder?

Zygote通信为什么用Socket,而不是Binder? - 简书 (jianshu.com)

参考

  1. platform/superproject/main - Android Code Search
  2. Android系统启动(二)- Zygote篇 - 掘金 (juejin.cn)
  3. Android O系统启动流程–zygote篇 - Vane的博客 | Vane’s Blog (vanelst.site)
从源码角度看Android系统init进程启动过程 init进程是Linux系统中用户空间的第一个进程,进程号为1。Kernel启动后,在用户空间启动init进程,并调用/system/core/init.cpp中的main方法执行一些重要的工作。 备注:本文将结合Android8.0的源码看init进程的启动过程以及init进程做了哪些重要工作。 1. init进程启动前系统的启动流程 在引入init进程前,我们需要大致了解系统是如何走到init... 阅读详情

相关推荐

【AAOS】【源码分析】电源管理(一)-- 基本介绍

Android车机电源管理及启动流程

码农的历程 948

Android加密之全盘加密详解

然后,vold会装载已解密的实际/data分区,并准备新的分区(如果加密时采用了首次发布不支持的数据清除选项,则可能永远无法准备就绪)。它会将vold.post_fs_data_done属性设为0,接着将vold.decrypt设为trigger_post_fs_data。如果密码正确(通过以下方式判定在临时位置成功装载已解密的/data,然后将其卸载),vold会将已解密块设备的名称保存在ro.crypto.fs_crypto_blkdev属性中,并向界面返回状态0。...

LVXIANGAN的专栏 5123

深入探究Android Framework核心:Zygote进程孵化与fork机制详解

本文深入剖析了Android系统中Zygote进程及其fork机制的核心作用。Zygote作为系统启动的关键守护进程,通过预加载共享资源和fork操作显著提升了应用启动效率。文章首先解析了Zygote的基本原理和fork机制的技术细节,包括其在Linux内核中的起源及Android中的创新应用;其次通过源码分析揭示了Zygote在Android启动流程中的具体实现;最后提供了常见面试问题和实战优化案例,如如何解决游戏应用启动慢等性能问题。

郑伟强dev的专栏 82

android 启动服务失败,Android init.rc 服务启动不成功

问题在开发过程中发现一个问题,我们需要在开机的时候判断硬件版本号去启动服务,服务的名字是ledservice和ledservice4,但是发现每次烧录完固件后,服务启动不成功,需要再复位重启一次,服务才可以正常启动。日志如下[ 5.328699] init: Service 'logd-reinit' (pid 257) exited with status 0[ 5.329681] init: ...

weixin_31556767的博客 2778

Android 进阶分水岭:吃透 Framework 四大核心,面试不再被底层问题难住

本文系统梳理Android Framework底层原理,重点解析系统启动全链路、Zygote进程机制、AMS服务管理和Binder通信原理四大核心模块。文章从硬件引导到应用启动的完整时序入手,结合Android 11源码路径,详细拆解Zygote的Socket通信设计、Copy-On-Write优化原理,AMS的四大组件管理和ANR监控机制,以及Binder的mmap内存映射优势。同时提供由浅入深的学习路径和大厂高频面试题解析,帮助应用开发者构建底层知识体系,解决ANR、冷启动等实际问题,提升技术竞争力。

郑伟强dev的专栏 295

微信支付V2,jsapi支付拉起收银台报错:调用支付JSAPI缺少参数:total_fee

拉起收银台报错

m0_54355172的博客 3884

Android Framework学习笔记(2)----系统启动

详细介绍android系统的启动过程。 1. Bootload初始化硬件设备,并加载linux core。 2. linux core创建用户空间中的第一个进程Init. 3. Init进程加载init.rc配置文件,创建zygote进程。 4. zygote进程读取自己的.rc文件,初始化JVM, 加载JNI接口,并启动system service. 5. zygote用来fork用户空间中的其他进程。 5. system service启动AMS等一众服务。 6. AMS启动launch,显示界面。

breakloop 1347

VS2103+Intel2013+MSMPI运行Fortran并行程序

VS2103+Intel2013+MSMPI运行Fortran并行程序MSMPI安装新建Fortran90项目编写Fortran并行程序在cmd运行编译完成的执行文件 MSMPI安装 我这里采用的是Microsoft MPI v9.0.1,可以点此下载。下载完成后同时安装msmpisdk.msi、msmpisetup.exe两个文件,安装位置默认即可。 其中msmpisdk.msi的默认安装路径在C:\Program Files (x86)\Microsoft SDKs\MPI,该文件包含了mpi环境所需

weixin_43176703的博客 2947

深入理解Android之设备加密Device Encryption

深入理解Android之设备加密Device EncryptionAndroid从4.4开始就支持一项功能,那就是对设备进行加密。加密自然是为了安全性考虑,由于/system目录是只读的,手机里那些存储设备分区中需要保护的就剩下/data/分区和sdcard了。显然,/data/和sdcard大量存储了用户数据(比如app运行时存储的数据),对他们进行加密非常非常有必要。Android 5.0发布

Innost的专栏 6万+

Oracle到金仓数据库信创改造迁移实施规划方案

**性能测试和优化**:对金仓数据库的性能进行测试,根据业务负载情况进行必要的调优,确保系统能够稳定高效运行。- **数据兼容性**:评估Oracle数据库中的数据结构和金仓数据库的兼容性,明确需要重构的数据模型。- **性能测试**:通过对金仓数据库的性能测试,预估在实际业务环境下的表现,是否能达到业务需求。- **工具选择**:选择适合的迁移工具,如金仓数据库提供的迁移工具,能够提升迁移效率和准确性。- **全面迁移**:在初步迁移成功后,进行全面数据迁移,确保所有数据完整迁移到金仓数据库上。

hdrhdrjdididrr的博客 489

保姆级教程:手把手教你从NASA Earthdata下载ASTER L2地表温度数据(附shp文件拖拽技巧)

本文提供了一份详细的NASA Earthdata平台ASTER L2地表温度数据下载教程,涵盖账号注册、数据筛选、shp文件拖拽技巧及质量检查等关键步骤。特别适合遥感领域初学者,帮助快速掌握高效获取ASTER L2T数据的方法,适用于城市热岛效应和农业监测等研究。

weixin_30736301的博客 475

手机版本回刷,导致不能开机一直处在开机动画界面

(这里只是可能,因为tombstoned先出现,不过,我们不用关心,这两个服务,我们都没有更改,不应该是他们引起的错误。(2)查看红色的部分,看起来有问题,第一个红色的部分,和正常日志一样,说明没有问题。(1)从上面的日志,查看,黑色粗体部分,是userdata挂载的时候,要经过的步骤,没有报错,最后也是挂载成功了的。可以看到,在rc文件中,系统是有动作创建这个文件的,但是失败了,原因是data 是只读的。(1)按道理,程序在打开这个文件的时候,如果这个路径没有,那么就会创建一个。然后就打印如下崩溃信息。

huibin147852369的专栏 2316

Python爬虫实战:手把手教你如何抓取农作物品种名录,构建标准化种业索引数据库!

我长期专注 Python 爬虫工程化实战,主理专栏 《Python爬虫实战》:从采集策略到反爬对抗,从数据清洗到分布式调度,持续输出可复用的方法论与可落地案例。内容主打一个“能跑、能用、能扩展”,让数据价值真正做到——抓得到、洗得净、用得上。

喵手的博客 299

从源码解析-Android系统启动流程概述 init进程zygote进程SystemServer进程启动流程

Android系统启动流程 启动流程 Loader Kernel Native Framework Application init进程 启动 rc文件规则 Actions Commands Service Options init.rc文件 init进程的功能 Zygote进程 main AndroidRuntime::start AndroidRuntime::startVm AndroidRuntime::sta

美丽心情 1215

交通仿真软件:SUMO_(13).Python脚本与TraCI接口

通过TraCI接口,用户可以实现与SUMO仿真器的实时交互,获取和控制交通数据。本文档详细介绍了如何使用Python脚本与TraCI接口进行交互,包括基本的设置、数据获取和控制方法。此外,还展示了几个高级应用示例,如交通流优化、多代理系统、车辆路径规划和车辆行为模拟。这些示例可以帮助用户更好地理解如何利用TraCI接口进行复杂的交通仿真和分析。希望这些内容对您有所帮助,如果您有任何问题或需要进一步的帮助,请参考SUMO的官方文档或社区支持。

kkchenjj的博客 1032

【RT-DETR多模态融合改进】在前期、中期、中后期、后期多模态融合中添加P6大目标检测层,完整步骤及代码

主题: RT-DETR的多模态融合改进中增加P6大目标检测层方式: 分别在前期融合、中期融合、中-后期融合、后期融合中增加P6多模态融合检测层。内容: 包含融合方式详解以及完整配置步骤,开箱即用,一键运行。原始模型结构如下: 二、有效特征层对应的检测头类别 2.1 P3/8 - small检测头 原始模型中的对应的检测头主要用于检测相对较小的目标。其特征图大小相对较大,空间分辨率较高。 适合检测尺寸大概在到像素左右的目标。 2.2 P4/16 - medium检测头 这个检测头对应的经过了更多的下采样

Limiiiing的博客 402

深入Android S(12.0)探索Framework之SystemServer进程的启动详解

SystemServer 作为 Zygote 进程 fork 出的第一个进程,其进程名为:system_server。其承载着整个 Framework 的核心服务,如创建并启动 ActivityManagerService、PackageManagerService、WindowManagerService、InputManagerService 等 90 多个核心系统服务。接下来一起深入学习一下 SystemServer 进程的创建与启动流程。

neuHenry 1255

刷机维修进阶教程-----oppo机型 一加 7pro 修复基带 更改参数 实例详细步骤解析

通过课程基本可以了解 1-----oppo机型与一加7机型更改参数的具体步骤解析 2------了解oppo机型开启diag端口的具体步骤 3-----了解一加7这些机型改参数常识 。可以借鉴参考用于其他机型 4-----了解手机基带分区与基带辅助分区的作用 5-----机型参数能读取到 但不能写这种情况如何操作

小马哥的专栏 1489

三分钟带你了解Android 系统启动流程详解

Android的运行机制(AMS)是Android系统中的一项核心服务,它主要负责管理系统的四大组件的、切换和调度,以及应用进程的整个生命周期的管理和调度。与操作系统中的进程管理和调度模.,AMS也是一个Binder的实现类,它允许应用进程通过Binder机制与系统服务进行交互。通常情况下,每个Android应用都在其自身的进程中运行。当应用需要执行某些代码时,系统会为该应用创建一个新的进程,并使其保持运行状态,直到不再需要该应用并且系统需要回收其内存以供其他应用使用。

huahaiyi的博客 193

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

Android 系统框架

init 进程启动过程Android系统启动流程Zygote启动流程及源码分析 APP启动流程init进程是Android用户空间第一个进程,主要做以下3件事情:Android 的AMS是什么 Android AMS源码解析Activity创建到View的显示过程PMS(PackageManagerService) 原理PMS处理APK的复制

朱建伟的专栏 526

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Android源码解析系列

知乎上看了一篇非常不错的博文:<a href="http://zhuanlan.zhihu.com/kaede/20563936">有没有必要阅读ANDROID源码</a> 看完之后痛定思过,平时所学往往是知其然然不知其所以然,所以为了更好的深入android体系,决定学习android framework层源码。这篇文章就是源码学习的汇总篇,包含学习源码的流程,文章列表等等,会根据学习的进度不定时更新。

一片枫叶的专栏 3万+

Android中init与受精卵Zygote分析,以及添加自己开机启动的系统服务

最近在做云游戏的项目,由于需要购买硬件,系统提供Android源码需要自己编译。所以特地学习一下记录一下Android的启动(我的是AOSP7.1然后+厂商系统补丁) 先上图: 目录 一. 启动流程硬件开机上电后,系统硬件内会有一个小程序就是bootloader 二.bootloader会引导内核启动加载 三.内核就会去挂载 文件以及初始化设备。最重要的是启动Init进程(这个就是重点相关的) 1.init.rc 脚本是在 init.cpp 入口方法 main 中开始解析的 2 紧接...

陈柱的博客 781

Android学习之路(15) AMS与PMS详解

我们从系统启动 init 进程开始分析到 Zygote 进程创建、SystemServer 进程创建,大致的将整体流程进行了梳理。init 进程是系统内核启动后第二个启动的进程,该进程在用户空间。init 进程启动后会解析 init.rc 执行脚本启动 Zygote 进程和 ServiceManager 进程。Zygote 因为是由 init 进程启动的,所以它也是在用户空间。Zygote 的业务处理横跨 native 层和 java 层。startVm() 创建虚拟机。

qq_32907491的博客 3378

深入了解Android卷 学习笔记 03

第三章 深入理解 init 3.1 概述

Chen_Yumin的博客 917

Android framework系列2 - Init进程

入口在init.cpp下,这个阶段主要来解析init.rc并执行此文件下的命令。可以看到这个文件下有很多命令,这里查找下我们关注的内容。主入口共三步,如流程图所示,我们主要看下最后一步。这里我们看到在解析init.rc,文件内容如下。serviceManager 服务进程。这里我们看到了循环执行,空闲则等待。解析后放到了am、sm下。

ytuglt的博客 195
上一篇: 【Android开发技巧】Android系统编译out目录结构
下一篇: 【Android系统启动】 Android SystemServer 进程启动流程源码解析
LokiCode
博客等级 码龄3年 840粉丝 18原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LokiCode

谢谢您买的奶茶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址