*isupdatedundertherunqueuelock.
**TodeterminewhetheraprocessmightrunonaCPU,this
*maskisAND-edwithcpus_allowed.
*若进程不在任何CPU上运行,cpus_runnable的值是0,否则是1。
这个值在运行*队列被锁时更新;*/
unsignedlongcpus_runnable,cpus_allowed;
/**(onlythe'next'pointerfitsintothecacheline,but
*that'sjustfine.)
*/
structlist_headrun_list;//指向运行队列的指针
unsignedlongsleep_time;//进程的睡眠时间
structtask_struct*next_task,*prev_task;//用于将系统中所有的进程连成一个双向循环链表,其根是init_task.
structmm_struct*active_mm;
structlist_headlocal_pages;//指向本地页面
unsignedintallocation_order,nr_local_pages;
/*taskstate*/
structlinux_binfmt*binfmt;//进程所运行的可执行文件的格式
intexit_code,exit_signal;
intpdeath_signal;/*Thesignalsentwhentheparentdies*///父进程终止是向子进程发送的信号
/*?
?
?
*/
unsignedlongpersonality;//Linux可以运行由其他UNIX操作系统生成的符合iBCS2标准的程序
intdid_exec:
1;//按POSIX要求设计的布尔量,区分进程正在执行从父进程中继承的代码,还是执行由execve装入的新程序代码
pid_tpid;//进程标识符,用来代表一个进程
pid_tpgrp;//进程组标识,表示进程所属的进程组
pid_ttty_old_pgrp;//进程控制终端所在的组标识
pid_tsession;//进程的会话标识
pid_ttgid;
/*booleanvalueforsessiongroupleader*/
intleader;//标志,表示进程是否为会话主管
/*
*pointersto(original)parentprocess,youngestchild,youngersibling,
*oldersibling,respectively.(p->fathercanbereplacedwith
*p->p_pptr->pid)
*///指针指向(原始的)父进程,孩子进程,比自己年轻的兄弟进程,比自己年长的兄弟进程
//(p->father能被p->p_pptr->pid代替)
structtask_struct*p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr;
structlist_headthread_group;//线程链表
/*PIDhashtablelinkage.*///进程散列表指针
structtask_struct*pidhash_next;//用于将进程链入HASH表pidhash
structtask_struct**pidhash_pprev;
wait_queue_head_twait_chldexit;/*forwait4()*///wait4()使用
structcompletion*vfork_done;/*forvfork()*///vfork()使用
unsignedlongrt_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定时器在进程运行时,根据进程消耗的时(不管在用户态还是内核态)更新
unsignedlongit_real_value,it_prof_value,it_virt_value;
unsignedlongit_real_incr,it_prof_incr,it_virt_value;
structtimer_listreal_timer;//指向实时定时器的指针
structtmstimes;//记录进程消耗的时间,
unsignedlongstart_time;//进程创建的时间
longper_cpu_utime[NR_CPUS],per_cpu_stime[NR_CPUS];//记录进程在每个CPU上所消耗的用户态时间和核心态时间
/*mmfaultandswapinfo:
thiscanarguablybeseenaseithermm-specificorthread-specific*/
//内存缺页和交换信息:
//min_flt,maj_flt累计进程的次缺页数(Copyon Write页和匿名页)和主缺页数(从映射文件或交换设备读入的页面数);
//nswap记录进程累计换出的页面数,即写到交换设备上的页面数。
//cmin_flt,cmaj_flt,cnswap记录本进程为祖先的所有子孙进程的累计次缺页数,主缺页数和换出页面数。
在父进程
//回收终止的子进程时,父进程会将子进程的这些信息累计到自己结构的这些域中
unsignedlongmin_flt,maj_flt,nswap,cmin_flt,cmaj_flt,cnswap;
intswappable:
1;//表示进程的虚拟地址空间是否允许换出
/*processcredentials*////进程认证信息
//uid,gid为运行该进程的用户的用户标识符和组标识符,通常是进程创建者的uid,gid//euid,egid为有效uid,gid
//fsuid,fsgid为文件系统uid,gid,这两个ID号通常与有效uid,gid相等,在检查对于文件系统的访问权限时使用他们。
//suid,sgid为备份uid,gid
uid_tuid,euid,suid,fsuid;
gid_tgid,egid,sgid,fsgid;
intngroups;//记录进程在多少个用户组中
gid_tgroups[NGROUPS];//记录进程所在的组
kernel_cap_tcap_effective,cap_inheritable,cap_permitted;//进程的权能,分别是有效位集合,继承位集合,允许位集合
intkeep_capabilities:
1;
structuser_struct*user;
/*limits*/
structrlimitrlim[RLIM_NLIMITS];//与进程相关的资源限制信息
unsignedshortused_math;//是否使用FPU
charcomm[16];//进程正在运行的可执行文件名
/*filesysteminfo*///文件系统信息
intlink_count,total_link_count;
structtty_struct*tty;/*NULLifnotty进程所在的控制终端,如果不需要控制终端,则该指针为空*/
unsignedintlocks;/*Howmanyfilelocksarebeingheld*/
/*ipcstuff*///进程间通信信息
structsem_undo*semundo;//进程在信号灯上的所有undo操作
structsem_queue*semsleeping;//当进程因为信号灯操作而挂起时,他在该队列中记录等待的操作
/*CPU-specificstateofthistask*///进程的CPU状态,切换时,要保存到停止进程的
task_struct中
structthread_structthread;
/*filesysteminformation文件系统信息*/
structfs_struct*fs;
/*openfileinformation*///打开文件信息
structfiles_struct*files;
/*signalhandlers*///信号处理函数
spinlock_tsigmask_lock;/*Protectssignalandblocked*/
structsignal_struct*sig;//信号处理函数,
sigset_tblocked;//进程当前要阻塞的信号,每个信号对应一位
structsigpendingpending;//进程上是否有待处理的信号
unsignedlongsas_ss_sp;
size_tsas_ss_size;
int(*notifier)(void*priv);
void*notifier_data;
sigset_t*notifier_mask;
/*Threadgrouptracking*/
u32parent_exec_id;
u32self_exec_id;
/*Protectionof(de-)allocation:
mm,files,fs,tty*/
spinlock_talloc_lock;
/*journallingfilesysteminfo*/
void*journal_info;
};
pid,这个值会被保存到eax中。
这时子进程就产生了,此时子进程与父进程拥有相同的代码空间,程序指针寄存器eip指向相同的下一条指令地址,当fork正常返回调用其的父进程后,因为eax中的值是新创建的子进程号,所以,fork()返回子进程号,执行else(pid>0);当产生进程切换运行子进程时,首先会恢复子进程的运行环境即装入子进程的tss任务状态段,其中的eax值(copy_process中置为0)也会被装入eax寄存器,所以,当子进程运行时,fork返回的是0执行if(pid==0)。
先显示childprocess应该和内核机制有关,当fork一个新的进程后都会进行进程的重新调度,此时总是子进程先运行(和进程优先级有关?
)
二、进程描述符
linux进程描述符—task_struct结构
为了管理进程,操作系统必须对每个进程所做的事情进行清楚地描述,为此,操作系统使用数据结构来代表处理不同的实体,这个数据结构就是通常所说的进程描述符或进程控制块,在linux系统中,这就是task_struct结构,在include\linux\sched.h文件中定义。
每个进程都会被分配一个task_struct结构,它包含了这个进程的所有信息,在任何时候操作系统都能跟踪这个结构的信息,这个结构是linux内核汇总最重要的数据结构,下面我们会详细的介绍。
这个结构的源代码及其注释如下,之后对其进行了分类解释。
//进程描述符task_struct
structtask_struct{
/**offsetsofthesearehardcodedelsewhere-touchwithcare
*/volatilelongstate;/*-1unrunnable,0runnable,>0stopped*///-1不能运行0运行>0停止
unsignedlongflags;/*perprocessflags,definedbelow*///进程标志,在下面定义
intsigpending;//进程上是否有待处理的信号
mm_segment_taddr_limit;/*threadaddressspace:
进程地址空间
0-0xBFFFFFFFforuser-thead
0-0xFFFFFFFFforkernel-thread
*/
volatilelongneed_resched;//调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度
intlock_depth;/*Lockdepth*///锁深度
/**offset32beginshereon32-bitplatforms.Wekeep
*allfieldsinasinglecachelinethatareneededfor
*thegoodness()loopinschedule().
*/longcounter;//进程可运行的时间量
longnice;//进程的基本时间片
unsignedlongpolicy;//进程的调度策略,有三种,实时进程:
SCHED_FIFO,SCHED_RR;分时进程:
SCHED_OTHER;
structmm_struct*mm;//进程内存管理信息
intprocessor;
/**cpus_runnableis~0iftheprocessisnotrunningonany
*CPU.It's(1<*isupdatedundertherunqueuelock.
**TodeterminewhetheraprocessmightrunonaCPU,this
*maskisAND-edwithcpus_allowed.
*若进程不在任何CPU上运行,cpus_runnable的值是0,否则是1。
这个值在运行*队列被锁时更新;*/
unsignedlongcpus_runnable,cpus_allowed;
/**(onlythe'next'pointerfitsintothecacheline,but
*that'sjustfine.)
*/
structlist_headrun_list;//指向运行队列的指针
unsignedlongsleep_time;//进程的睡眠时间
structtask_struct*next_task,*prev_task;//用于将系统中所有的进程连成一个双向循环链表,其根是init_task.
structmm_struct*active_mm;
structlist_headlocal_pages;//指向本地页面
unsignedintallocation_order,nr_local_pages;
/*taskstate*/
structlinux_binfmt*binfmt;//进程所运行的可执行文件的格