窥探 kernel,just for fun --- task_struct

本系列文章由张同浩编写,转载请注明出处:http://blog.csdn.net/muge0913/article/details/7477083

邮箱:[email protected]


程序和进程:

程序是存放在磁盘上的一系列代码和数据的可执行镜像,是一个静态的实体

进程是一个执行的程序,它是动态的实体,它除了包含指令段,数据段等静态数据外(数据是可以是动态变化的),还包括当前的状态信息,如临时数据堆栈信息,当前处理器的寄存器信息等动态信息。这些动态信息通常称为进程上下文。

从内核角度来看,进程是操作系统分配内存,cpu时间片等资源的最小单位。其中它用到的数据和信息大部分都是在动态变化的。在linux内核中进程上下文通常用task_struct来描述,进程切换负责保存当前进程的上下文,恢复合适进程的上下文到cpu和寄存器中。


进程和线程:

 随着计算机产业的发展,计算机的应用范围越来越广,计算机要解决的范围从处理器密集型的科学计算向IO密集型的用户交互式程序。为了解决日益复杂的问题。人们提出了分而治之(divide and comquer)的思想,也就是提出了进程。随着计算机的发展和对此技术的研究,人们发现,进程间的切换带来了相当大的系统开销(overload),人们又提出了线程的概念。线程是对进程的进一步抽象。一个进程有两部分组成:线程集合和资源集合。线程是进程中的一个动态对象,一组动态的指令流。进程中的所有线程将共享进程的中的资源,但每个线程又有独立的程序计数器,堆栈和寄存器。


linux中线程、进程都是用struct task_struct来描述。进程描述符task_struct用来刻画进程的状态属性,是内核操作和维护进程状态的唯一手段,其定义在linux 2.6.xx/include/linux/sched.h中。这个结构体相当的大




  1. truct task_struct {  
  2.     /*这个是进程的运行时状态,-1代表不可运行,0代表可运行,>0代表已停止*/  
  3.     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */  
  4.     void *stack;  
  5.     atomic_t usage;  
  6.     unsigned int flags; /* per process flags, defined below */  
  7.     unsigned int ptrace;  
  8.   
  9.     int lock_depth;     /* BKL lock depth */  
  10.   
  11. #ifdef CONFIG_SMP  
  12. #ifdef __ARCH_WANT_UNLOCKED_CTXSW  
  13.     int oncpu;  
  14. #endif  
  15. #endif  
  16.   
  17.     int prio, static_prio, normal_prio;  
  18.     /*表示此进程的运行优先级*/  
  19.     unsigned int rt_priority;  
  20.     const struct sched_class *sched_class;  
  21.     struct sched_entity se;  
  22.     struct sched_rt_entity rt;  
  23.   
  24. #ifdef CONFIG_PREEMPT_NOTIFIERS  
  25.     /* list of struct preempt_notifier: */  
  26.     struct hlist_head preempt_notifiers;  
  27. #endif  
  28.   
  29.     /* 
  30.      * fpu_counter contains the number of consecutive context switches 
  31.      * that the FPU is used. If this is over a threshold, the lazy fpu 
  32.      * saving becomes unlazy to save the trap. This is an unsigned char 
  33.      * so that after 256 times the counter wraps and the behavior turns 
  34.      * lazy again; this to deal with bursty apps that only use FPU for 
  35.      * a short time 
  36.      */  
  37.     unsigned char fpu_counter;  
  38. #ifdef CONFIG_BLK_DEV_IO_TRACE  
  39.     unsigned int btrace_seq;  
  40. #endif  
  41.   
  42.     unsigned int policy;  
  43.     cpumask_t cpus_allowed;  
  44.   
  45. #ifdef CONFIG_PREEMPT_RCU  
  46.     int rcu_read_lock_nesting;  
  47.     char rcu_read_unlock_special;  
  48.     struct list_head rcu_node_entry;  
  49. #endif /* #ifdef CONFIG_PREEMPT_RCU */  
  50. #ifdef CONFIG_TREE_PREEMPT_RCU  
  51.     struct rcu_node *rcu_blocked_node;  
  52. #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */  
  53.   
  54. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)  
  55.     struct sched_info sched_info;  
  56. #endif  
  57.   
  58.     struct list_head tasks;  
  59.     struct plist_node pushable_tasks;  
  60.     /*该结构体记录了进程内存使用的相关情况*/  
  61.     struct mm_struct *mm, *active_mm;  
  62. #if defined(SPLIT_RSS_COUNTING)  
  63.     struct task_rss_stat    rss_stat;  
  64. #endif  
  65. /* task state */  
  66.     /*进程退出时的状态*/  
  67.     int exit_state;  
  68.     int exit_code, exit_signal;  
  69.     int pdeath_signal;  /*  The signal sent when the parent dies  */  
  70.     /* ??? */  
  71.     unsigned int personality;  
  72.     unsigned did_exec:1;  
  73.     unsigned in_execve:1;   /* Tell the LSMs that the process is doing an 
  74.                  * execve */  
  75.     unsigned in_iowait:1;  
  76.   
  77.   
  78.     /* Revert to default priority/policy when forking */  
  79.     unsigned sched_reset_on_fork:1;  
  80.   
  81.     /*进程号*/  
  82.     pid_t pid;  
  83.     /*组进程号*/  
  84.     pid_t tgid;  
  85.   
  86. #ifdef CONFIG_CC_STACKPROTECTOR  
  87.     /* Canary value for the -fstack-protector gcc feature */  
  88.     unsigned long stack_canary;  
  89. #endif  
  90.   
  91.     /*  
  92.      * pointers to (original) parent process, youngest child, younger sibling, 
  93.      * older sibling, respectively.  (p->father can be replaced with  
  94.      * p->real_parent->pid) 
  95.      */  
  96.      /*创建该进程的父进程*/  
  97.     struct task_struct *real_parent; /* real parent process */  
  98.     /*parent是该进程现在的父进程,有可能是”继父“*/  
  99.     struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */  
  100.     /* 
  101.      * children/sibling forms the list of my natural children 
  102.      */  
  103.      /*这里children指的是该进程孩子的链表,可以得到所有孩子的进程描述符*/  
  104.     struct list_head children;  /* list of my children */  
  105.     /*sibling该进程兄弟的链表,也就是其父亲的所有孩子的链表*/  
  106.     struct list_head sibling;   /* linkage in my parent's children list */  
  107.     /*这个是主线程的进程描述符,linux并没有单独实现线程的相关结构体,只是用一个进程来代替线程,然后对其做一些特殊的处理*/  
  108.     struct task_struct *group_leader;   /* threadgroup leader */  
  109.   
  110.     /* 
  111.      * ptraced is the list of tasks this task is using ptrace on. 
  112.      * This includes both natural children and PTRACE_ATTACH targets. 
  113.      * p->ptrace_entry is p's link on the p->parent->ptraced list. 
  114.      */  
  115.     struct list_head ptraced;  
  116.     struct list_head ptrace_entry;  
  117.   
  118.     /* PID/PID hash table linkage. */  
  119.     struct pid_link pids[PIDTYPE_MAX];  
  120.     /*该进程所有线程的链表*/  
  121.     struct list_head thread_group;  
  122.   
  123.     struct completion *vfork_done;      /* for vfork() */  
  124.     int __user *set_child_tid;      /* CLONE_CHILD_SETTID */  
  125.     int __user *clear_child_tid;        /* CLONE_CHILD_CLEARTID */  
  126.   
  127.     /*这个是该进程使用cpu时间的信息,utime是在用户态下执行的时间,stime是在内核态下执行的时间*/  
  128.     cputime_t utime, stime, utimescaled, stimescaled;  
  129.     cputime_t gtime;  
  130. #ifndef CONFIG_VIRT_CPU_ACCOUNTING  
  131.     cputime_t prev_utime, prev_stime;  
  132. #endif  
  133.     unsigned long nvcsw, nivcsw; /* context switch counts */  
  134.     /*启动的时间,只是时间基准不一样*/  
  135.     struct timespec start_time;         /* monotonic time */  
  136.     struct timespec real_start_time;    /* boot based time */  
  137. /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */  
  138.     unsigned long min_flt, maj_flt;  
  139.   
  140.     struct task_cputime cputime_expires;  
  141.     struct list_head cpu_timers[3];  
  142.   
  143. /* process credentials */  
  144.     const struct cred __rcu *real_cred; /* objective and real subjective task 
  145.                      * credentials (COW) */  
  146.     const struct cred __rcu *cred;  /* effective (overridable) subjective task 
  147.                      * credentials (COW) */  
  148.     struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */  
  149.   
  150.     /*保存该进程名字的字符数组*/  
  151.     char comm[TASK_COMM_LEN]; /* executable name excluding path 
  152.                      - access with [gs]et_task_comm (which lock 
  153.                        it with task_lock()) 
  154.                      - initialized normally by setup_new_exec */  
  155. /* file system info */  
  156. /* 文件系统信息计数*/  
  157.     int link_count, total_link_count;  
  158. #ifdef CONFIG_SYSVIPC  
  159. /* ipc stuff */  
  160.     struct sysv_sem sysvsem;  
  161. #endif  
  162. #ifdef CONFIG_DETECT_HUNG_TASK  
  163. /* hung task detection */  
  164.     unsigned long last_switch_count;  
  165. #endif  
  166. /* CPU-specific state of this task */  
  167. /*该进程在特定CPU下的状态*/  
  168.     struct thread_struct thread;  
  169. /* filesystem information */  
  170. /* 文件系统相关信息结构体*/  
  171.     struct fs_struct *fs;  
  172. /* open file information */  
  173. /* 打开的文件相关信息结构体,对驱动开发者来说此结构会常见到*/  
  174.     struct files_struct *files;  
  175. /* namespaces */  
  176.     struct nsproxy *nsproxy;  
  177. /* signal handlers */  
  178. /* 信号相关信息的句柄*/  
  179.     struct signal_struct *signal;  
  180.     struct sighand_struct *sighand;  
  181.   
  182.     sigset_t blocked, real_blocked;  
  183.     sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */  
  184.     struct sigpending pending;  
  185.   
  186.     unsigned long sas_ss_sp;  
  187.     size_t sas_ss_size;  
  188.     int (*notifier)(void *priv);  
  189.     void *notifier_data;  
  190.     sigset_t *notifier_mask;  
  191.     struct audit_context *audit_context;  
  192. #ifdef CONFIG_AUDITSYSCALL  
  193.     uid_t loginuid;  
  194.     unsigned int sessionid;  
  195. #endif  
  196.     seccomp_t seccomp;  
  197.   
  198. /* Thread group tracking */  
  199.     u32 parent_exec_id;  
  200.     u32 self_exec_id;  
  201. /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, 
  202.  * mempolicy */  
  203.     spinlock_t alloc_lock;  
  204.   
  205. #ifdef CONFIG_GENERIC_HARDIRQS  
  206.     /* IRQ handler threads */  
  207.     struct irqaction *irqaction;  
  208. #endif  
  209.   
  210.     /* Protection of the PI data structures: */  
  211.     raw_spinlock_t pi_lock;  
  212.   
  213. #ifdef CONFIG_RT_MUTEXES  
  214.     /* PI waiters blocked on a rt_mutex held by this task */  
  215.     struct plist_head pi_waiters;  
  216.     /* Deadlock detection and priority inheritance handling */  
  217.     struct rt_mutex_waiter *pi_blocked_on;  
  218. #endif  
  219.   
  220. #ifdef CONFIG_DEBUG_MUTEXES  
  221.     /* mutex deadlock detection */  
  222.     struct mutex_waiter *blocked_on;  
  223. #endif  
  224. #ifdef CONFIG_TRACE_IRQFLAGS  
  225.     unsigned int irq_events;  
  226.     unsigned long hardirq_enable_ip;  
  227.     unsigned long hardirq_disable_ip;  
  228.     unsigned int hardirq_enable_event;  
  229.     unsigned int hardirq_disable_event;  
  230.     int hardirqs_enabled;  
  231.     int hardirq_context;  
  232.     unsigned long softirq_disable_ip;  
  233.     unsigned long softirq_enable_ip;  
  234.     unsigned int softirq_disable_event;  
  235.     unsigned int softirq_enable_event;  
  236.     int softirqs_enabled;  
  237.     int softirq_context;  
  238. #endif  
  239. #ifdef CONFIG_LOCKDEP  
  240. # define MAX_LOCK_DEPTH 48UL  
  241.     u64 curr_chain_key;  
  242.     int lockdep_depth;  
  243.     unsigned int lockdep_recursion;  
  244.     struct held_lock held_locks[MAX_LOCK_DEPTH];  
  245.     gfp_t lockdep_reclaim_gfp;  
  246. #endif  
  247.   
  248. /* journalling filesystem info */  
  249.     void *journal_info;  
  250.   
  251. /* stacked block device info */  
  252.     struct bio_list *bio_list;  
  253.   
  254. /* VM state */  
  255.     struct reclaim_state *reclaim_state;  
  256.   
  257.     struct backing_dev_info *backing_dev_info;  
  258.   
  259.     struct io_context *io_context;  
  260.   
  261.     unsigned long ptrace_message;  
  262.     siginfo_t *last_siginfo; /* For ptrace use.  */  
  263.     struct task_io_accounting ioac;  
  264. #if defined(CONFIG_TASK_XACCT)  
  265.     u64 acct_rss_mem1;  /* accumulated rss usage */  
  266.     u64 acct_vm_mem1;   /* accumulated virtual memory usage */  
  267.     cputime_t acct_timexpd; /* stime + utime since last update */  
  268. #endif  
  269. #ifdef CONFIG_CPUSETS  
  270.     nodemask_t mems_allowed;    /* Protected by alloc_lock */  
  271.     int mems_allowed_change_disable;  
  272.     int cpuset_mem_spread_rotor;  
  273.     int cpuset_slab_spread_rotor;  
  274. #endif  
  275. #ifdef CONFIG_CGROUPS  
  276.     /* Control Group info protected by css_set_lock */  
  277.     struct css_set __rcu *cgroups;  
  278.     /* cg_list protected by css_set_lock and tsk->alloc_lock */  
  279.     struct list_head cg_list;  
  280. #endif  
  281. #ifdef CONFIG_FUTEX  
  282.     struct robust_list_head __user *robust_list;  
  283. #ifdef CONFIG_COMPAT  
  284.     struct compat_robust_list_head __user *compat_robust_list;  
  285. #endif  
  286.     struct list_head pi_state_list;  
  287.     struct futex_pi_state *pi_state_cache;  
  288. #endif  
  289. #ifdef CONFIG_PERF_EVENTS  
  290.     struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];  
  291.     struct mutex perf_event_mutex;  
  292.     struct list_head perf_event_list;  
  293. #endif  
  294. #ifdef CONFIG_NUMA  
  295.     struct mempolicy *mempolicy;    /* Protected by alloc_lock */  
  296.     short il_next;  
  297. #endif  
  298.     atomic_t fs_excl;   /* holding fs exclusive resources */  
  299.     struct rcu_head rcu;  
  300.   
  301.     /* 
  302.      * cache last used pipe for splice 
  303.      */  
  304.     struct pipe_inode_info *splice_pipe;  
  305. #ifdef  CONFIG_TASK_DELAY_ACCT  
  306.     struct task_delay_info *delays;  
  307. #endif  
  308. #ifdef CONFIG_FAULT_INJECTION  
  309.     int make_it_fail;  
  310. #endif  
  311.     struct prop_local_single dirties;  
  312. #ifdef CONFIG_LATENCYTOP  
  313.     int latency_record_count;  
  314.     struct latency_record latency_record[LT_SAVECOUNT];  
  315. #endif  
  316.     /* 
  317.      * time slack values; these are used to round up poll() and 
  318.      * select() etc timeout values. These are in nanoseconds. 
  319.      */  
  320.      /*这些是松弛时间值,用来规定select()和poll()的超时时间,单位是纳秒nanoseconds  */  
  321.     unsigned long timer_slack_ns;  
  322.     unsigned long default_timer_slack_ns;  
  323.   
  324.     struct list_head    *scm_work_list;  
  325. #ifdef CONFIG_FUNCTION_GRAPH_TRACER  
  326.     /* Index of current stored address in ret_stack */  
  327.     int curr_ret_stack;  
  328.     /* Stack of return addresses for return function tracing */  
  329.     struct ftrace_ret_stack *ret_stack;  
  330.     /* time stamp for last schedule */  
  331.     unsigned long long ftrace_timestamp;  
  332.     /* 
  333.      * Number of functions that haven't been traced 
  334.      * because of depth overrun. 
  335.      */  
  336.     atomic_t trace_overrun;  
  337.     /* Pause for the tracing */  
  338.     atomic_t tracing_graph_pause;  
  339. #endif  
  340. #ifdef CONFIG_TRACING  
  341.     /* state flags for use by tracers */  
  342.     unsigned long trace;  
  343.     /* bitmask of trace recursion */  
  344.     unsigned long trace_recursion;  
  345. #endif /* CONFIG_TRACING */  
  346. #ifdef CONFIG_CGROUP_MEM_RES_CTLR /* memcg uses this to do batch job */  
  347.     struct memcg_batch_info {  
  348.         int do_batch;   /* incremented when batch uncharge started */  
  349.         struct mem_cgroup *memcg; /* target memcg of uncharge */  
  350.         unsigned long bytes;        /* uncharged usage */  
  351.         unsigned long memsw_bytes; /* uncharged mem+swap usage */  
  352.     } memcg_batch;  
  353. #endif  
  354. };  


上面只是一些简单注释,后面会重点介绍某些重要的结构体,和它们的相关操作和用途。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章