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

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);

    }

    ...

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 最近的状态嘚吧嘚 最近工作有些忙碌,不停追赶着迭代,修复问题填坑…已经不记得上次自己写文章是什么时候了,导致之前的...
    Mac_guan阅读 4,500评论 0 1
  • 如果你看完书中的所有例子,你很可能已经做完你的实验和在已经越狱的iPhone上的研究。因为和许多人一样,几乎所有的...
    fishmai0阅读 17,215评论 2 42
  • 又来到了一个老生常谈的问题,应用层软件开发的程序员要不要了解和深入学习操作系统呢? 今天就这个问题开始,来谈谈操...
    tangsl阅读 9,682评论 0 23
  • 文/tangsl(简书作者) 原文链接:http://www.jianshu.com/p/2b993a4b913e...
    西葫芦炒胖子阅读 9,248评论 0 5
  • 看了中央电视台第十科教频道《人物》栏目纪念专辑《先生 南怀瑾》,了解到一代大师不为人知的点点滴滴, 更为南老师花费...
    所谓珈人阅读 3,593评论 8 1

友情链接更多精彩内容