收藏 分销(赏)

Linux内核设计与实现.doc

上传人:pc****0 文档编号:8729083 上传时间:2025-02-28 格式:DOC 页数:36 大小:188.50KB
下载 相关 举报
Linux内核设计与实现.doc_第1页
第1页 / 共36页
Linux内核设计与实现.doc_第2页
第2页 / 共36页
点击查看更多>>
资源描述
进程管理 一、FORK()函数的两次返回的具体情况 对于fork来说,父子进程共享同一段代码空间,所以给人的感觉好像是有两次返回,其实对于调用 fork的父进程来说,如果fork出来的子进程没有得到调度,那么父进程从fork系统调用返回,同时分析sys_fork知道,fork返回的是子进 程的id。再看fork出来的子进程,由copy_process函数可以看出,子进程的返回地址为ret_from_fork(和父进程在同一个代码点 上返回),返回值直接置为0。所以当子进程得到调度的时候,也从fork返回,返回值为0。 关键注意两点:1.fork返回后,父进程或子进程的执行位置。(首先会将当前进程eax的值做为返回值)2.两次返回的pid存放的位置。(eax中) 进程调用copy_process得到lastpid的值(放入eax中,fork正常返回后,父进程中返回的就是lastpid) 子进程任务状态段tss的eax被设置成0, fork.c 中 p->tss.eax=0;(如果子进程要执行就需要进程切换,当发生切换时,子进程tss中的eax值就调入eax寄存器,子进程执行时首先会将eax的内容做为返回值) 当子进程开始执行时,copy_process返回eax的值。 fork()后,就是两个任务同时进行,父进程用他的tss,子进程用自己的tss,在切换时,各用各的eax中的值. 所以,“一次调用两次返回”是2个不同的进程! 例子: int main() { pid_t pid; pid=fork(); if ( pid < 0 ) { fprintf( stderr, "Fork Failed" ); exit( -1 ); } else if ( pid == 0 ) { printf( "child process\\n"); } else { printf( "parent process\\n" ); } return 0; } 这个程序执行为什么总是显示:child process parent process 而不会先是parent 后是child呢? 答:看这一句:pid=fork() 当 执行这一句时,当前进程进入fork()运行,此时,fork()内会用一段嵌入式汇编进行系统调用:int 0x80(具体代码可参见内核版本0.11的unistd.h文件的133行_syscall0函数)。这时进入内核根据此前写入eax的系统调用功能号 便会运行sys_fork系统调用。接着,sys_fork中首先会调用C函数find_empty_process产生一个新的进程,然后会调用C函数 copy_process将父进程的内容复制给子进程,但是子进程tss中的eax值赋值为0(这也是为什么子进程中返回0的原因),当赋值完成后, copy_process会返回新进程(该子进程)的linux进程描述符—task_struct结构 为了管理进程,操作系统必须对每个进程所做的事情进行清楚地描述,为此,操作系统使用数据结构来代表处理不同的实体,这个数据结构就是通常所说的进程描述符或进程控制块,在linux系统中,这就是task_struct结构,在include\linux\sched.h文件中定义。每个进程都会被分配一个task_struct结构,它包含了这个进程的所有信息,在任何时候操作系统都能跟踪这个结构的信息,这个结构是linux内核汇总最重要的数据结构,下面我们会详细的介绍。这个结构的源代码及其注释如下,之后对其进行了分类解释。 //进程描述符task_struct struct task_struct { /* * offsets of these are hardcoded elsewhere - touch with care */ volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ //-1 不能运行 0 运行 >0 停止 unsigned long flags; /* per process flags, defined below *///进程标志,在下面定义 int sigpending; //进程上是否有待处理的信号 mm_segment_t addr_limit; /* thread address space:进程地址空间 0-0xBFFFFFFF for user-thead 0-0xFFFFFFFF for kernel-thread */ volatile long need_resched; //调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度 int lock_depth; /* Lock depth *///锁深度 /* * offset 32 begins here on 32-bit platforms. We keep * all fields in a single cacheline that are needed for * the goodness() loop in schedule(). */ long counter; //进程可运行的时间量 long nice; //进程的基本时间片 unsigned long policy; //进程的调度策略,有三种,实时进程:SCHED_FIFO,SCHED_RR;分时进程:SCHED_OTHER; struct mm_struct *mm; //进程内存管理信息 int processor; /* * cpus_runnable is ~0 if the process is not running on any * CPU. It's (1 << cpu) if it's running on a CPU. This mask * is updated under the runqueue lock. * * To determine whether a process might run on a CPU, this * mask is AND-ed with cpus_allowed. * 若进程不在任何CPU上运行,cpus_runnable 的值是0,否则是1。这个值在运行 *队列被锁时更新;*/ unsigned long cpus_runnable, cpus_allowed; /* * (only the 'next' pointer fits into the cacheline, but * that's just fine.) */ struct list_head run_list; //指向运行队列的指针 unsigned long sleep_time; //进程的睡眠时间 struct task_struct *next_task, *prev_task; //用于将系统中所有的进程连成一个双向循环链表,其根是init_task. struct mm_struct *active_mm; struct list_head local_pages; //指向本地页面 unsigned int allocation_order, nr_local_pages; /* task state */ struct linux_binfmt *binfmt; //进程所运行的可执行文件的格式 int exit_code, exit_signal; int pdeath_signal; /* The signal sent when the parent dies *///父进程终止是向子进程发送的信号 /* ??? */ unsigned long personality; //Linux可以运行由其他UNIX操作系统生成的符合iBCS2标准的程序 int did_exec:1; //按POSIX要求设计的布尔量,区分进程正在执行从父进程中继承的代码,还是执行由execve装入的新程序代码 pid_t pid; //进程标识符,用来代表一个进程 pid_t pgrp; //进程组标识,表示进程所属的进程组 pid_t tty_old_pgrp; //进程控制终端所在的组标识 pid_t session; //进程的会话标识 pid_t tgid; /* boolean value for session group leader */ int leader; //标志,表示进程是否为会话主管 /* * pointers to (original) parent process, youngest child, younger sibling, * older sibling, respectively. (p->father can be replaced with * p->p_pptr->pid) *///指针指向(原始的)父进程,孩子进程,比自己年轻的兄弟进程,比自己年长的兄弟进程 //(p->father能被p->p_pptr->pid代替) struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr; struct list_head thread_group; //线程链表 /* PID hash table linkage. *///进程散列表指针 struct task_struct *pidhash_next; //用于将进程链入HASH表pidhash struct task_struct **pidhash_pprev; wait_queue_head_t wait_chldexit; /* for wait4() *///wait4()使用 struct completion *vfork_done; /* for vfork() */// vfork() 使用 unsigned long rt_priority; //实时优先级,用它计算实时进程调度时的weight值 //it_real_value,it_real_incr用于REAL定时器,单位为jiffies。系统根据it_real_value //设置定时器的第一个终止时间。在定时器到期时,向进程发送SIGALRM信号,同时根据it_real_incr重置终止时间 //it_prof_value,it_prof_incr用于Profile定时器,单位为jiffies。当进程运行时,不管在何种状态下,每个tick都使 //it_prof_value值减一,当减到0时,向进程发送信号SIGPROF,并根据it_prof_incr重置时间 //it_virt_value,it_virt_value用于Virtual定时器,单位为jiffies。当进程运行时,不管在何种状态下,每个tick都使 //it_virt_value值减一,当减到0时,向进程发送信号SIGVTALRM,根据it_virt_incr重置初值。 //Real定时器根据系统时间实时更新,不管进程是否在运行 //Virtual定时器只在进程运行时,根据进程在用户态消耗的时间更新 //Profile定时器在进程运行时,根据进程消耗的时(不管在用户态还是内核态)更新 unsigned long it_real_value, it_prof_value, it_virt_value; unsigned long it_real_incr, it_prof_incr, it_virt_value; struct timer_list real_timer;//指向实时定时器的指针 struct tms times; //记录进程消耗的时间, unsigned long start_time;//进程创建的时间 long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; //记录进程在每个CPU上所消耗的用户态时间和核心态时间 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ //内存缺页和交换信息: //min_flt, maj_flt累计进程的次缺页数(Copy on Write页和匿名页)和主缺页数(从映射文件或交换设备读入的页面数); //nswap记录进程累计换出的页面数,即写到交换设备上的页面数。 //cmin_flt, cmaj_flt, cnswap记录本进程为祖先的所有子孙进程的累计次缺页数,主缺页数和换出页面数。在父进程 //回收终止的子进程时,父进程会将子进程的这些信息累计到自己结构的这些域中 unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; int swappable:1; //表示进程的虚拟地址空间是否允许换出 /* process credentials *////进程认证信息 //uid,gid为运行该进程的用户的用户标识符和组标识符,通常是进程创建者的uid,gid //euid,egid为有效uid,gid //fsuid,fsgid为文件系统uid,gid,这两个ID号通常与有效uid,gid相等,在检查对于文件系统的访问权限时使用他们。 //suid,sgid为备份uid,gid uid_t uid,euid,suid,fsuid; gid_t gid,egid,sgid,fsgid; int ngroups; //记录进程在多少个用户组中 gid_t groups[NGROUPS]; //记录进程所在的组 kernel_cap_t cap_effective, cap_inheritable, cap_permitted;//进程的权能,分别是有效位集合,继承位集合,允许位集合 int keep_capabilities:1; struct user_struct *user; /* limits */ struct rlimit rlim[RLIM_NLIMITS]; //与进程相关的资源限制信息 unsigned short used_math; //是否使用FPU char comm[16]; //进程正在运行的可执行文件名 /* file system info *///文件系统信息 int link_count, total_link_count; struct tty_struct *tty; /* NULL if no tty 进程所在的控制终端,如果不需要控制终端,则该指针为空*/ unsigned int locks; /* How many file locks are being held */ /* ipc stuff *///进程间通信信息 struct sem_undo *semundo; //进程在信号灯上的所有undo操作 struct sem_queue *semsleeping; //当进程因为信号灯操作而挂起时,他在该队列中记录等待的操作 /* CPU-specific state of this task *///进程的CPU状态,切换时,要保存到停止进程的 task_struct中 struct thread_struct thread; /* filesystem information文件系统信息*/ struct fs_struct *fs; /* open file information *///打开文件信息 struct files_struct *files; /* signal handlers *///信号处理函数 spinlock_t sigmask_lock; /* Protects signal and blocked */ struct signal_struct *sig; //信号处理函数, sigset_t blocked; //进程当前要阻塞的信号,每个信号对应一位 struct sigpending pending; //进程上是否有待处理的信号 unsigned long sas_ss_sp; size_t sas_ss_size; int (*notifier)(void *priv); void *notifier_data; sigset_t *notifier_mask; /* Thread group tracking */ u32 parent_exec_id; u32 self_exec_id; /* Protection of (de-)allocation: mm, files, fs, tty */ spinlock_t alloc_lock; /* journalling filesystem info */ void *journal_info; }; pid,这个值会被保存到eax中。这时子进程就产生了,此时子进程与父进程拥有相同的代码空间,程 序指针寄存器eip指向相同的下一条指令地址,当fork正常返回调用其的父进程后,因为eax中的值是新创建的子进程号,所以,fork()返回子进程 号,执行else(pid>0);当产生进程切换运行子进程时,首先会恢复子进程的运行环境即装入子进程的tss任务状态段,其中的eax值 (copy_process中置为0)也会被装入eax寄存器,所以,当子进程运行时,fork返回的是0执行if(pid==0)。 先显示child process应该和内核机制有关,当fork一个新的进程后都会进行进程的重新调度,此时总是子进程先运行(和进程优先级有关?)   二、进程描述符 linux进程描述符—task_struct结构 为了管理进程,操作系统必须对每个进程所做的事情进行清楚地描述,为此,操作系统使用数据结构来代表处理不同的实体,这个数据结构就是通常所说的进程描述符或进程控制块,在linux系统中,这就是task_struct结构,在include\linux\sched.h文件中定义。每个进程都会被分配一个task_struct结构,它包含了这个进程的所有信息,在任何时候操作系统都能跟踪这个结构的信息,这个结构是linux内核汇总最重要的数据结构,下面我们会详细的介绍。这个结构的源代码及其注释如下,之后对其进行了分类解释。 //进程描述符task_struct struct task_struct { /* * offsets of these are hardcoded elsewhere - touch with care */ volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ //-1 不能运行 0 运行 >0 停止 unsigned long flags; /* per process flags, defined below *///进程标志,在下面定义 int sigpending; //进程上是否有待处理的信号 mm_segment_t addr_limit; /* thread address space:进程地址空间 0-0xBFFFFFFF for user-thead 0-0xFFFFFFFF for kernel-thread */ volatile long need_resched; //调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度 int lock_depth; /* Lock depth *///锁深度 /* * offset 32 begins here on 32-bit platforms. We keep * all fields in a single cacheline that are needed for * the goodness() loop in schedule(). */ long counter; //进程可运行的时间量 long nice; //进程的基本时间片 unsigned long policy; //进程的调度策略,有三种,实时进程:SCHED_FIFO,SCHED_RR;分时进程:SCHED_OTHER; struct mm_struct *mm; //进程内存管理信息 int processor; /* * cpus_runnable is ~0 if the process is not running on any * CPU. It's (1 << cpu) if it's running on a CPU. This mask * is updated under the runqueue lock. * * To determine whether a process might run on a CPU, this * mask is AND-ed with cpus_allowed. * 若进程不在任何CPU上运行,cpus_runnable 的值是0,否则是1。这个值在运行 *队列被锁时更新;*/ unsigned long cpus_runnable, cpus_allowed; /* * (only the 'next' pointer fits into the cacheline, but * that's just fine.) */ struct list_head run_list; //指向运行队列的指针 unsigned long sleep_time; //进程的睡眠时间 struct task_struct *next_task, *prev_task; //用于将系统中所有的进程连成一个双向循环链表,其根是init_task. struct mm_struct *active_mm; struct list_head local_pages; //指向本地页面 unsigned int allocation_order, nr_local_pages; /* task state */ struct linux_binfmt *binfmt; //进程所运行的可执行文件的格式 int exit_code, exit_signal; int pdeath_signal; /* The signal sent when the parent dies *///父进程终止是向子进程发送的信号 /* ??? */ unsigned long personality; //Linux可以运行由其他UNIX操作系统生成的符合iBCS2标准的程序 int did_exec:1; //按POSIX要求设计的布尔量,区分进程正在执行从父进程中继承的代码,还是执行由execve装入的新程序代码 pid_t pid; //进程标识符,用来代表一个进程 pid_t pgrp; //进程组标识,表示进程所属的进程组 pid_t tty_old_pgrp; //进程控制终端所在的组标识 pid_t session; //进程的会话标识 pid_t tgid; /* boolean value for session group leader */ int leader; //标志,表示进程是否为会话主管 /* * pointers to (original) parent process, youngest child, younger sibling, * older sibling, respectively. (p->father can be replaced with * p->p_pptr->pid) *///指针指向(原始的)父进程,孩子进程,比自己年轻的兄弟进程,比自己年长的兄弟进程 //(p->father能被p->p_pptr->pid代替) struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr; struct list_head thread_group; //线程链表 /* PID hash table linkage. *///进程散列表指针 struct task_struct *pidhash_next; //用于将进程链入HASH表pidhash struct task_struct **pidhash_pprev; wait_queue_head_t wait_chldexit; /* for wait4() *///wait4()使用 struct completion *vfork_done; /* for vfork() */// vfork() 使用 unsigned long rt_priority; //实时优先级,用它计算实时进程调度时的weight值 //it_real_value,it_real_incr用于REAL定时器,单位为jiffies。系统根据it_real_value //设置定时器的第一个终止时间。在定时器到期时,向进程发送SIGALRM信号,同时根据it_real_incr重置终止时间 //it_prof_value,it_prof_incr用于Profile定时器,单位为jiffies。当进程运行时,不管在何种状态下,每个tick都使 //it_prof_value值减一,当减到0时,向进程发送信号SIGPROF,并根据it_prof_incr重置时间 //it_virt_value,it_virt_value用于Virtual定时器,单位为jiffies。当进程运行时,不管在何种状态下,每个tick都使 //it_virt_value值减一,当减到0时,向进程发送信号SIGVTALRM,根据it_virt_incr重置初值。 //Real定时器根据系统时间实时更新,不管进程是否在运行 //Virtual定时器只在进程运行时,根据进程在用户态消耗的时间更新 //Profile定时器在进程运行时,根据进程消耗的时(不管在用户态还是内核态)更新 unsigned long it_real_value, it_prof_value, it_virt_value; unsigned long it_real_incr, it_prof_incr, it_virt_value; struct timer_list real_timer;//指向实时定时器的指针 struct tms times; //记录进程消耗的时间, unsigned long start_time;//进程创建的时间 long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; //记录进程在每个CPU上所消耗的用户态时间和核心态时间 /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ //内存缺页和交换信息: //min_flt, maj_flt累计进程的次缺页数(Copy on Write页和匿名页)和主缺页数(从映射文件或交换设备读入的页面数); //nswap记录进程累计换出的页面数,即写到交换设备上的页面数。 //cmin_flt, cmaj_flt, cnswap记录本进程为祖先的所有子孙进程的累计次缺页数,主缺页数和换出页面数。在父进程 //回收终止的子进程时,父进程会将子进程的这些信息累计到自己结构的这些域中 unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap; int swappable:1; //表示进程的虚拟地址空间是否允许换出 /* process credentials *////进程认证信息 //uid,gid为运行该进程的用户的用户标识符和组标识符,通常是进程创建者的uid,gid //euid,egid为有效uid,gid //fsuid,fsgid为文件系统uid,gid,这两个ID号通常与有效uid,gid相等,在检查对于文件系统的访问权限时使用他们。 //suid,sgid为备份uid,gid uid_t uid,euid,suid,fsuid; gid_t gid,egid,sgid,fsgid; int ngroups; //记录进程在多少个用户组中 gid_t groups[NGROUPS]; //记录进程所在的组 kernel_cap_t cap_effective, cap_inheritable, cap_permitted;//进程的权能,分别是有效位集合,继承位集合,允许位集合 int keep_capabilities:1; struct user_struct *user; /* limits */ struct rlimit rlim[RLIM_NLIMITS]; //与进程相关的资源限制信息 unsigned short used_math; //是否使用FPU char comm[16]; //进程正在运行的可执行文件名 /* file system info *///文件系统信息 int link_count, total_link_count; struct tty_struct *tty; /* NULL if no tty 进程所在的控制终端,如果不需要控制终端,则该指针为空*/ unsigned int locks; /* How many file locks are being held */ /* ipc stuff *///进程间通信信息 struct sem_undo *semundo; //进程在信号灯上的所有undo操作 struct sem_queue *semsleeping; //当进程因为信号灯操作而挂起时,他在该队列中记录等待的操作 /* CPU-specific state of this task *///进程的CPU状态,切换时,要保存到停止进程的 task_struct中 struct thread_struct thread; /* filesystem information文件系统信息*/ struct fs_struct *fs; /* open file information *///打开文件信息 struct files_struct *files; /* signal handlers *///信号处理函数 spinlock_t sigmask_lock; /* Protects signal and blocked */ struct signal_struct *sig; //信号处理函数, sigset_t blocked; //进程当前要阻塞的信号,每个信号对应一位 struct sigpending pending; //进程上是否有待处理的信号 unsigned long sas_ss_sp; size_t sas_ss_size; int (*notifier)(void *priv); void *notifier_data; sigset_t *notifier_mask; /* Thread group tracking */ u32 parent_exec_id; u32 self_exec_id; /* Protection of (de-)allocation: mm, files, fs, tty */ spinlock_t alloc_lock; /* journalling filesystem info */ void *journal_info; }; 三、Linux内核2.6和2.4中内核堆栈的比较 在内核2.4中堆栈是这么定义的: union task_union {         struct task_struct task;         unsigned long stack[INIT_TASK_SIZE/sizeof(long)];     }; 而INIT_TASK_SIZE只能是8K。 内核为每个进程分配一个task_struct结构时,实际上分配两个连续的物理页面(8192字节),如图所示。底部用作task_struct结构(大小约为1K字节),结构的上面用作内核堆栈(大小约为7K字节)。访问进程自身的task_struct结构,使用宏操作current, 在2.4中定义如下: #define current get_current() static inline struct task_struct * get_current(void) {       struct task_struct *current;       __asm__("andl %%esp,%0; ":"=r" (current) : "" (~8191UL));       return current; }   ~8191UL表示最低13位为0, 其余位全为1。 %esp指向内核堆栈中,当屏蔽掉%esp的最低13后,就得到这个”两个连续的物理页面”的开头,而这个开头正好是task_struct的开始,从而得到了指向task_struct的指针。 在内核2.6中
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传
相似文档                                   自信AI助手自信AI助手

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4009-655-100  投诉/维权电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服