美文网首页
APP冷启动加载进程线程内存分配流程代码

APP冷启动加载进程线程内存分配流程代码

作者: teddy8ear | 来源:发表于2019-07-26 11:39 被阅读0次

int __mac_execve(proc_t p, struct __mac_execve_args *uap, int32_t *retval)

{

    // 字段设置

    ...

    int is_64 = IS_64BIT_PROCESS(p);

    struct vfs_context context;

    struct uthread  *uthread; // 线程

    task_t new_task = NULL;  // Mach Task

    ...

   

    context.vc_thread = current_thread();

    context.vc_ucred = kauth_cred_proc_ref(p);

   

    // 分配大块内存,不用堆栈是因为 Mach-O 结构很大。

    MALLOC(bufp, char *, (sizeof(*imgp) + sizeof(*vap) + sizeof(*origvap)), M_TEMP, M_WAITOK | M_ZERO);

    imgp = (struct image_params *) bufp;

   

    // 初始化 imgp 结构里的公共数据

    ...

   

    uthread = get_bsdthread_info(current_thread());

    if (uthread->uu_flag & UT_VFORK) {

        imgp->ip_flags |= IMGPF_VFORK_EXEC;

        in_vfexec = TRUE;

    } else {

        // 程序如果是启动态,就需要 fork 新进程

        imgp->ip_flags |= IMGPF_EXEC;

        // fork 进程

        imgp->ip_new_thread = fork_create_child(current_task(),

                    NULL, p, FALSE, p->p_flag & P_LP64, TRUE);

        // 异常处理

        ...

        new_task = get_threadtask(imgp->ip_new_thread);

        context.vc_thread = imgp->ip_new_thread;

    }

   

    // 加载解析 Mach-O

    error = exec_activate_image(imgp);

   

    if (imgp->ip_new_thread != NULL) {

        new_task = get_threadtask(imgp->ip_new_thread);

    }

    if (!error && !in_vfexec) {

        p = proc_exec_switch_task(p, current_task(), new_task, imgp->ip_new_thread);

   

        should_release_proc_ref = TRUE;

    }

    kauth_cred_unref(&context.vc_ucred);

   

    if (!error) {

        task_bank_init(get_threadtask(imgp->ip_new_thread));

        proc_transend(p, 0);

        thread_affinity_exec(current_thread());

        // 继承进程处理

        if (!in_vfexec) {

            proc_inherit_task_role(get_threadtask(imgp->ip_new_thread), current_task());

        }

        // 设置进程的主线程

        thread_t main_thread = imgp->ip_new_thread;

        task_set_main_thread_qos(new_task, main_thread);

    }

    ...

}

相关文章

  • APP冷启动加载进程线程内存分配流程代码

    int __mac_execve(proc_t p, struct __mac_execve_args *uap,...

  • 多线程(GCD)

    进程和线程的概念 进程:程序是运行在内存中,进程负责分配内存线程:负责程序中代码的实际运行。分类:主线程(一条),...

  • 进程与线程、线程池

    进程和线程 进程间无法共享内存,可以通过tcp/ip端口交互等线程之间共享内存进程开销大,分配内存线程分配栈和PC...

  • 关于App启动速度优化

    冷启动、热启动 冷启动:App启动前,它的进程不在系统里,系统新创建一个进程分配给它启动;热启动:App冷启动后,...

  • 02-App 启动速度怎么做优化与监控?

    一、知识点 1.1 App启动分为冷启动和热启动 冷启动是指当前App的进程没有在系统中,需要系统重新分配进程给他...

  • js内存

    一、关于内存知识点 堆栈的三种含义内存管理内存机制 进程和线程 进程分配的有内存空间,一个进程有多个线程,每个线程...

  • iOS APP启动时间优化

    APP启动分为冷启动和热启动。 冷启动:App 点击启动前,它的进程不在系统里,需要系统新创建一个进程分配给它启动...

  • windows vs linux

    内核管理进程、线程,决定哪个进程、线程使用 CPU,也就是进程调度的能力;管理内存,决定内存的分配和回收,也就是内...

  • App启动速度的优化

    通常,App的启动分为冷启动和热启动。 冷启动是指,App启动之前,进程不在系统内,需要重新创建一个进程分配给Ap...

  • 进程和线程

    进程: 进程分配内存,且内存是隔离的。至少包括一个线程。 线程: (start /affinity 0x1 jav...

网友评论

      本文标题:APP冷启动加载进程线程内存分配流程代码

      本文链接:https://www.haomeiwen.com/subject/cperrctx.html