窺探 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. };  


上面只是一些簡單註釋,後面會重點介紹某些重要的結構體,和它們的相關操作和用途。


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