Welcome 微信登录

首页 / 操作系统 / Linux / Linux进程描述符task_struct详解

大家都知道进程,可是知道linux是怎么管理其进程的吗?每一个进程都有一个进程描述符,具体是task_struct结构体存储相关的信息,在linux/sched.h文件里定义,那么我们先看看linux内核3.0版本的task_struct结构体的定义吧(删除了不必要的字段,只保留了重要的字段)。同时欢迎大家转载和交流。struct task_struct { //这个是进程的运行时状态,-1代表不可运行,0代表可运行,>0代表已停止。
 volatile long state;  /*flags是进程当前的状态标志,具体的如:0x00000002表示进程正在被创建;0x00000004表示进程正准备退出;0x00000040 表示此进程被fork出,但是并没有执行exec;0x00000400表示此进程由于其他进程发送相关信号而被杀死 。*/ unsigned int flags;  //表示此进程的运行优先级 unsigned int rt_priority; //这里出现了list_head结构体,详情请参考 struct list_head tasks; //这里出现了mm_struct 结构体,该结构体记录了进程内存使用的相关情况,详情请参考 struct mm_struct *mm; /* 接下来是进程的一些状态参数*/
 int exit_state;
 int exit_code, exit_signal;

//这个是进程号 pid_t pid; //这个是进程组号
 pid_t tgid; //real_parent是该进程的”亲生父亲“,不管其是否被“寄养”。 struct task_struct *real_parent;  //parent是该进程现在的父进程,有可能是”继父“
 struct task_struct *parent;  //这里children指的是该进程孩子的链表,可以得到所有孩子的进程描述符,但是需使用list_for_each和list_entry,list_entry其实直接使用了container_of,详情请参考
 struct list_head children;  //同理,sibling该进程兄弟的链表,也就是其父亲的所有孩子的链表。用法与children相似。 struct list_head sibling;  //这个是主线程的进程描述符,也许你会奇怪,为什么线程用进程描述符表示,因为linux并没有单独实现线程的相关结构体,只是用一个进程来代替线程,然后对其做一些特殊的处理。 struct task_struct *group_leader;  //这个是该进程所有线程的链表。 struct list_head thread_group; //顾名思义,这个是该进程使用cpu时间的信息,utime是在用户态下执行的时间,stime是在内核态下执行的时间。 cputime_t utime, stime; //下面的是启动的时间,只是时间基准不一样。
 struct timespec start_time;    struct timespec real_start_time; //comm是保存该进程名字的字符数组,长度最长为15,因为TASK_COMM_LEN为16。 char comm[TASK_COMM_LEN];  /* 文件系统信息计数*/
 int link_count, total_link_count; /*该进程在特定CPU下的状态*/
 struct thread_struct thread;
/* 文件系统相关信息结构体*/
 struct fs_struct *fs;
/* 打开的文件相关信息结构体 */
 struct files_struct *files;  /* 信号相关信息的句柄*/
 struct signal_struct *signal;
 struct sighand_struct *sighand; 
 /*这些是松弛时间值,用来规定select()和poll()的超时时间,单位是纳秒nanoseconds  */
 unsigned long timer_slack_ns;
 unsigned long default_timer_slack_ns;};到这里,我们已经看到了很多的字段,但这些字段只是task_struct的冰山一角,希望更多的人可以为我们揭开task_struct的神秘面纱。至于内存描述符可参考 http://www.linuxidc.com/Linux/2012-02/53703.htmLinux内核源码中container_of详解Linux内存描述符mm_struct初窥相关资讯      Linux基础教程 
  • Linux基础教程:对文件打包压缩  (03月08日)
  • 基础教程:Linux 新手应该知道的   (09/06/2015 21:17:20)
  • Linux基础教程:find 与 xargs  (04/05/2015 10:20:11)
  • Linux基础教程:tar 命令使用介绍  (12/03/2015 13:19:47)
  • Linux基础教程(1)操作系统基础   (08/15/2015 20:44:01)
  • Linux基础教程:从源码安装软件  (04/05/2015 10:14:45)
本文评论 查看全部评论 (0)
表情: 姓名: 字数