Outline ○进程描逑符 o Linux的进程描速待:task struct 。进程的栈和thread info最据结拘 。进程相关的几个链表 。proc文件系就简介 进程的等待和唤醒 进程切提 。进程上下文 a上下文切换 ●进程的创建和则 。进程的创建 o Linux的进程创建 。内核线程及其创建 。进程树及其开焰 。近程的终止和周除 进调度 。进程的分美 aLix中的调度菜略和调度算法 o Linux-2.6.26中的调度相关数据结均和代码 0Ln山x2.6.26中的优先级及其设置 行小结和作 4口4四1是14P刀00 陈香兰(xlanchenoustc,edu,en)(升异数应Lnux操作系统分折Chapter 0注在管理 Decenber23,201415/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Outline . 1. 进程描述符 Linux的进程描述符:task_struct 进程的栈和thread_info数据结构 进程相关的几个链表 proc文件系统简介 .2 进程的等待和唤醒 .3 进程切换 进程上下文 上下文切换 4. 进程的创建和删除 进程的创建 Linux的进程创建 内核线程及其创建 进程树及其开始 进程的终止和删除 5. 进程调度 进程的分类 Linux中的调度策略和调度算法 Linux-2.6.26中的调度相关数据结构和代码 Linux2.6.26中的优先级及其设置 .6 小结和作业 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 15 / 95
进程相关的几个链表 ·为了对给定属性的进程(比如所有在可运行状态下的进程)进行 有效的搜索,内核维护了几个进程链表 ,所有进程链表 ·TASK RUNNING状态的进程组织:运行队列(参见进程调度)】 ~pidhash表及链接表 ·进程之间的亲属关系 ·等待队列 ●Liux内核为自己提供专门的双向链表结构和相关操作, 参见include/1inux/1ist.h和lib/1 ist_debug.c 1口+4四1是4P在刀0C 陈香兰(xlanchenouste,edu,cn)(升界教应Lnux掉作系统分行Chapter 9过在管理 Decenber23,201416/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 进程相关的几个链表 为了对给定属性的进程(比如所有在可运行状态下的进程)进行 有效的搜索,内核维护了几个进程链表 ▶ 所有进程链表 ▶ TASK_RUNNING状态的进程组织:运行队列(参见进程调度) ▶ pidhash表及链接表 ▶ 进程之间的亲属关系 ▶ 等待队列 Linux内核为自己提供专门的双向链表结构和相关操作, 参见include/linux/list.h和lib/list_debug.c 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 16 / 95
1、所有进程链表 ●所有进程链表以init task为链表头,将所有进程通过 task struct::tasks链接在一起 prev_task next_task prev_task next_task prev_task next init_task ·新创建的进程插入链表尾部, 参见kernel/fork.c::copy_process() list_add_tail_rcu(&p->tasks,&init_task.tasks); ●for_each_process宏扫描整个进程链表 include/1inux/sched.h #define next_task(p)list_entry(rcu_dereference((p)->tasks.next),struct task_struct, tasks) #define for_each_process(p)\ for (p =&init_task (p next_task(p))!=&init_task 东香兰(x1 anchenoustc,ed加,cn)(升界款应Lnux操作系统分行Chapter 0注任管理 Decenber 23.2014 17/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1、所有进程链表 所有进程链表以init_task为链表头,将所有进程通过 task_struct::tasks链接在一起 init_task prev_task next_task prev_task next_task prev_task next_task 新创建的进程插入链表尾部, 参见kernel/fork.c::copy_process() . . ... list_add_tail_rcu(&p->tasks, &init_task.tasks); ... for_each_process宏扫描整个进程链表 . include/linux/sched.h . . #define next_task(p) list_entry(rcu_dereference((p)->tasks.next), struct task_struct, tasks) #define for_each_process(p) \ for (p = &init_task ; (p = next_task(p)) != &init_task ; ) 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 17 / 95
2、pidhash表及链接表 ●在一些情况下,内核必须能从进程的PID得出对应的进程描述 符指针。例如ki11系统调用 ●为了加速查找,引入了pid hash散列表 参见kernel/pid.c #define pid_hashfn(nr,ns)\ hash_long((unsigned long)nr+(unsigned long)ns,pidhash_shift) static struct hlist head pid hash; static int pidhash_shift: 0 voidinit pidhash_init(void) PID PID 199 199 26799 PID 216 26800 1023 4口4四是4P在刀0C 陈香兰(x1 anchenoustc,edu,cn)(计异轧应Linux操作系统分折Chapter0连狂管理 Decenber23,201418/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2、pidhash表及链接表 在一些情况下,内核必须能从进程的PID得出对应的进程描述 符指针。例如kill系统调用 为了加速查找,引入了pid_hash散列表 . 参见kernel/pid.c . . #define pid_hashfn(nr, ns) \ hash_long((unsigned long)nr + (unsigned long)ns, pidhash_shift) static struct hlist_head *pid_hash; static int pidhash_shift; ... void __init pidhash_init(void){ ... } 0 199 216 1023 ... ... ... PID 199 PID 26799 PID 26800 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 18 / 95
3、进程之间的亲属关系 ·程序创建的进程具有父子关系,在编程时往往需要引用这样的 父子关系。进程描述符中有几个域用来表示这样的关系 task struct中表示亲属关系的几个域 / pointers to (original)parent process,youngest child,younger sibling. older sibling,respectively.(p->father can be replaced with ·p->parent-->pid) struct task_struct *realparent;/real parent process (when being debugged)*/ struct task_struct *parent;parent process/ struct list_head children;/.list of my children struct list_head sibling:/.linkage in my parent's children list struct task_struct 'group_leader:threadgroup leader/ 4口4四1是14P刀00 陈香兰(xlanchenoustc,edu,cn)(升界丸应Linux掉作系统分行Chapter 9注任管理 Decenber23,201419/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3、进程之间的亲属关系 程序创建的进程具有父子关系,在编程时往往需要引用这样的 父子关系。进程描述符中有几个域用来表示这样的关系 . task_struct中表示亲属关系的几个域 . . /* * pointers to (original) parent process, youngest child, younger sibling, * older sibling, respectively. (p->father can be replaced with * p->parent->pid) */ struct task_struct *real_parent; /* real parent process (when being debugged) */ struct task_struct *parent; /* parent process */ ... struct list_head children; /* list of my children */ struct list_head sibling; /* linkage in my parent’s children list */ struct task_struct *group_leader; /* threadgroup leader */ 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 19 / 95