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 东奇兰(x1 anchenoustc,ed加,cn)(计异款应Linux操作系统分折Chapter 9注任管理 Decenber23,201412/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 12 / 95
进程的栈 ●一个普通的用户进程有2个栈 ·用户态的栈 ★用户态代码的运行使用用户态的栈 ★用户态的栈在用户地址空间中 内核态的栈 ★内核代码(内核控制路径)的运行使用内核态的栈 ★内核态的栈由内核进行管理和分配 4口4四是4巴,在月0C 陈香兰(xlanchenoustc,edu,cn)(升异丸应Linux操作系统分折Chapter 0注在管理 Decenber23,201413/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 进程的栈 一个普通的用户进程有2个栈 ▶ 用户态的栈 ⋆ 用户态代码的运行使用用户态的栈 ⋆ 用户态的栈在用户地址空间中 ▶ 内核态的栈 ⋆ 内核代码(内核控制路径)的运行使用内核态的栈 ⋆ 内核态的栈由内核进行管理和分配 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 13 / 95
进程的栈 。内核态的栈及其大小 Ox015f bfff Linux.为每个进程分配一个8KB大小的 STACK 内存区域,用于存放该进程 两个不同的数据结构: O thread_infoo 0x015fb000 include/asm-arm/thread info.h ©进程的内核堆栈 不同于用户态堆栈,由于内核控制 0x015fa878 路径所用的推栈很少,因此对栈和 thread info来说,8KB足够了 thread_info ·C语言允许用如下的一个union结构 0x015fa000 来方便的表示这样的一个混合体 include/linux/sched.h union thread_union{ struct thread_info thread_info; unsigned long stack[THREAD_SIZE/sizeof(1ong)]; }: 陈香兰(x1 anchenoustc,ed加.cm)(计异丸应Lnux掉作系统分折Chapter 0注任管理 Decenber 23.2014 13/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 进程的栈 内核态的栈及其大小 ▶ Linux为每个进程分配一个8KB大小的 内存区域,用于存放该进程 两个不同的数据结构: 1. thread_info@ include/asm-arm/thread_info.h 2. 进程的内核堆栈 ▶ 不同于用户态堆栈,由于内核控制 路径所用的堆栈很少,因此对栈和 thread_info 来说,8KB足够了 ▶ C语言允许用如下的一个union结构 来方便的表示这样的一个混合体 sp → t → thread_info STACK 0x015f bfff 0x015f b000 0x015f a878 0x015f a000 . include/linux/sched.h . . union thread_union { struct thread_info thread_info; unsigned long stack[THREAD_SIZE/sizeof(long)]; }; 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 13 / 95
thread info数据结构 ●thread info数据结构,参见include/asm-arm/thread info.h 。Thread info的分配/回收使用页面级分配器 (注:比较进程描述符的分配和回收) 参见include/asm-arm/thread_info.h /thread information allocation #ifdef CONFIG DEBUG_STACK_USAGE #define alloc_thread_info(tsk)\ ((struct thread_info )_get_free_pages(GFP_KERNEL I_GFP_ZERO,\ THREAD_SIZE_ORDER)) felse #define alloc_thread_info(tsk)\ ((struct thread_info )_get_free_pages(GFP_KERNEL,THREAD_SIZE_ORDER)) #endif #define free_thread_info(info)\ free_pages((unsigned 1ong)info,THREAD_SIZE_ORDER): 4口4四1是14P刀00 香兰(xlanchenoustc,edu.cn) (升界机应L1nux操作系统分行Chapter 9注任管理 Decenber23,201414/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . thread_info数据结构 thread_info数据结构,参见include/asm-arm/thread_info.h Thread_info的分配/回收使用页面级分配器 (注:比较进程描述符的分配和回收) . 参见include/asm − arm/thread_info.h . . /* thread information allocation */ #ifdef CONFIG_DEBUG_STACK_USAGE #define alloc_thread_info(tsk) \ ((struct thread_info *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, \ THREAD_SIZE_ORDER)) #else #define alloc_thread_info(tsk) \ ((struct thread_info *)__get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER)) #endif #define free_thread_info(info) \ free_pages((unsigned long)info, THREAD_SIZE_ORDER); 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 14 / 95
thread info数据结构 ●current thread info,从内核堆栈获得当前进程的thread info 从刚才看到的thread_info和内核态堆栈之间的配对,内核可以很 容易的从sp寄存器的值获得当前在CPU上运行的进程的thread info 起始地址 因为这个内存区是8KB=213B大小,并且起始地址是8KB对齐的, 内核只需让sp低l3位为0,即可获得thread info的基地址 include/asm-arm/thread info.h / how to get the thread information struct from C */ static inline struct thread_info'current_thread_info(void)_attribute_const_: static inline struct thread_info *current_thread_info(void) register unsigned long sp asm(”sp”)月 return (struct thread_info )(sp (THREAD_SIZE -1)); 4口4四1是142刀00 练香兰(xlanchenoustc,ed加,cn)(计并丸应Lnux掉作系统分行Chapter 9过花管理 Decenber23,201414/95
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . thread_info数据结构 current_thread_info,从内核堆栈获得当前进程的thread_info ▶ 从刚才看到的thread_info和内核态堆栈之间的配对,内核可以很 容易的从sp寄存器的值获得当前在CPU上运行的进程的thread_info 起始地址 ▶ 因为这个内存区是8KB = 213B大小,并且起始地址是8KB对齐的, 内核只需让sp低13位为0,即可获得thread_info的基地址 . include/asm-arm/thread_info.h . . /* * how to get the thread information struct from C */ static inline struct thread_info *current_thread_info(void) __attribute_const__; static inline struct thread_info *current_thread_info(void) { register unsigned long sp asm (”sp”); return (struct thread_info *)(sp & ~(THREAD_SIZE - 1)); } 陈香兰(xlanchen@ustc.edu.cn) (计算机应用教研室 Linux操作系统分析Chapter 9 进程管理 @计算机学院嵌入式系统实验室@苏州研究院中国科学技术大学Fall 2014) December 23, 2014 14 / 95