Linux进程管理之task_struct结构体(下)

来源:互联网 发布:php final 编辑:程序博客网 时间:2024/06/05 18:31

9、进程地址空间 

[cpp] view plain copy
  1.     struct mm_struct *mm, *active_mm;  
  2. #ifdef CONFIG_COMPAT_BRK  
  3.     unsigned brk_randomized:1;  
  4. #endif  
  5. #if defined(SPLIT_RSS_COUNTING)  
  6.     struct task_rss_stat    rss_stat;  
  7. #endif  

    mm指向进程所拥有的内存描述符,而active_mm指向进程运行时所使用的内存描述符。对于普通进程而言,这两个指针变量的值相同。但是,内核线程不拥有任何内存描述符,所以它们的mm成员总是为NULL。当内核线程得以运行时,它的active_mm成员被初始化为前一个运行进程的active_mm值。

    brk_randomized的用法在http://lkml.indiana.edu/hypermail/Linux/kernel/1104.1/00196.html上有介绍,用来确定对随机堆内存的探测。

    rss_stat用来记录缓冲信息。 

    10、判断标志 

[cpp] view plain copy
  1. int exit_code, exit_signal;  
  2. int pdeath_signal;  /*  The signal sent when the parent dies  */  
  3. /* ??? */  
  4. unsigned int personality;  
  5. unsigned did_exec:1;  
  6. unsigned in_execve:1;   /* Tell the LSMs that the process is doing an 
  7.              * execve */  
  8. unsigned in_iowait:1;  
  9.   
  10.   
  11. /* Revert to default priority/policy when forking */  
  12. unsigned sched_reset_on_fork:1;  

    exit_code用于设置进程的终止代号,这个值要么是_exit()或exit_group()系统调用参数(正常终止),要么是由内核提供的一个错误代号(异常终止)。

    exit_signal被置为-1时表示是某个线程组中的一员。只有当线程组的最后一个成员终止时,才会产生一个信号,以通知线程组的领头进程的父进程。

    pdeath_signal用于判断父进程终止时发送信号。

    personality用于处理不同的ABI,它的可能取值如下: 

[cpp] view plain copy
  1. enum {  
  2.     PER_LINUX =     0x0000,  
  3.     PER_LINUX_32BIT =   0x0000 | ADDR_LIMIT_32BIT,  
  4.     PER_LINUX_FDPIC =   0x0000 | FDPIC_FUNCPTRS,  
  5.     PER_SVR4 =      0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,  
  6.     PER_SVR3 =      0x0002 | STICKY_TIMEOUTS | SHORT_INODE,  
  7.     PER_SCOSVR3 =       0x0003 | STICKY_TIMEOUTS |  
  8.                      WHOLE_SECONDS | SHORT_INODE,  
  9.     PER_OSR5 =      0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,  
  10.     PER_WYSEV386 =      0x0004 | STICKY_TIMEOUTS | SHORT_INODE,  
  11.     PER_ISCR4 =     0x0005 | STICKY_TIMEOUTS,  
  12.     PER_BSD =       0x0006,  
  13.     PER_SUNOS =     0x0006 | STICKY_TIMEOUTS,  
  14.     PER_XENIX =     0x0007 | STICKY_TIMEOUTS | SHORT_INODE,  
  15.     PER_LINUX32 =       0x0008,  
  16.     PER_LINUX32_3GB =   0x0008 | ADDR_LIMIT_3GB,  
  17.     PER_IRIX32 =        0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */  
  18.     PER_IRIXN32 =       0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */  
  19.     PER_IRIX64 =        0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */  
  20.     PER_RISCOS =        0x000c,  
  21.     PER_SOLARIS =       0x000d | STICKY_TIMEOUTS,  
  22.     PER_UW7 =       0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,  
  23.     PER_OSF4 =      0x000f,          /* OSF/1 v4 */  
  24.     PER_HPUX =      0x0010,  
  25.     PER_MASK =      0x00ff,  
  26. };  

    did_exec用于记录进程代码是否被execve()函数所执行。

    in_execve用于通知LSM是否被do_execve()函数所调用。详见补丁说明:http://lkml.indiana.edu/hypermail/linux/kernel/0901.1/00014.html。

    in_iowait用于判断是否进行iowait计数。

    sched_reset_on_fork用于判断是否恢复默认的优先级或调度策略。

    11、时间 

[cpp] view plain copy
  1.     cputime_t utime, stime, utimescaled, stimescaled;  
  2.     cputime_t gtime;  
  3. #ifndef CONFIG_VIRT_CPU_ACCOUNTING  
  4.     cputime_t prev_utime, prev_stime;  
  5. #endif  
  6.     unsigned long nvcsw, nivcsw; /* context switch counts */  
  7.     struct timespec start_time;         /* monotonic time */  
  8.     struct timespec real_start_time;    /* boot based time */  
  9.     struct task_cputime cputime_expires;  
  10.     struct list_head cpu_timers[3];  
  11. #ifdef CONFIG_DETECT_HUNG_TASK  
  12. /* hung task detection */  
  13.     unsigned long last_switch_count;  
  14. #endif  

    utime/stime用于记录进程在用户态/内核态下所经过的节拍数(定时器)。prev_utime/prev_stime是先前的运行时间,请参考补丁说明http://lkml.indiana.edu/hypermail/linux/kernel/1003.3/02431.html。

    utimescaled/stimescaled也是用于记录进程在用户态/内核态的运行时间,但它们以处理器的频率为刻度。

    gtime是以节拍计数的虚拟机运行时间(guest time)。

    nvcsw/nivcsw是自愿(voluntary)/非自愿(involuntary)上下文切换计数。last_switch_count是nvcsw和nivcsw的总和。

    start_time和real_start_time都是进程创建时间,real_start_time还包含了进程睡眠时间,常用于/proc/pid/stat,补丁说明请参考http://lkml.indiana.edu/hypermail/linux/kernel/0705.0/2094.html。

    cputime_expires用来统计进程或进程组被跟踪的处理器时间,其中的三个成员对应着cpu_timers[3]的三个链表。

    12、信号处理 

[cpp] view plain copy
  1. /* signal handlers */  
  2.     struct signal_struct *signal;  
  3.     struct sighand_struct *sighand;  
  4.   
  5.     sigset_t blocked, real_blocked;  
  6.     sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */  
  7.     struct sigpending pending;  
  8.   
  9.     unsigned long sas_ss_sp;  
  10.     size_t sas_ss_size;  
  11.     int (*notifier)(void *priv);  
  12.     void *notifier_data;  
  13.     sigset_t *notifier_mask;  

    signal指向进程的信号描述符。

    sighand指向进程的信号处理程序描述符。

    blocked表示被阻塞信号的掩码,real_blocked表示临时掩码。

    pending存放私有挂起信号的数据结构。

    sas_ss_sp是信号处理程序备用堆栈的地址,sas_ss_size表示堆栈的大小。

    设备驱动程序常用notifier指向的函数来阻塞进程的某些信号(notifier_mask是这些信号的位掩码),notifier_data指的是notifier所指向的函数可能使用的数据。

    13、其他

    (1)、用于保护资源分配或释放的自旋锁 

[cpp] view plain copy
  1. /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed, 
  2.  * mempolicy */  
  3.     spinlock_t alloc_lock;  

    (2)、进程描述符使用计数,被置为2时,表示进程描述符正在被使用而且其相应的进程处于活动状态。 

[cpp] view plain copy
  1. atomic_t usage;  

    (3)、用于表示获取大内核锁的次数,如果进程未获得过锁,则置为-1。 

[cpp] view plain copy
  1. int lock_depth;     /* BKL lock depth */  

    (4)、在SMP上帮助实现无加锁的进程切换(unlocked context switches) 

[cpp] view plain copy
  1. #ifdef CONFIG_SMP  
  2. #ifdef __ARCH_WANT_UNLOCKED_CTXSW  
  3.     int oncpu;  
  4. #endif  
  5. #endif  

    (5)、preempt_notifier结构体链表 

[cpp] view plain copy
  1. #ifdef CONFIG_PREEMPT_NOTIFIERS  
  2.     /* list of struct preempt_notifier: */  
  3.     struct hlist_head preempt_notifiers;  
  4. #endif  

    (6)、FPU使用计数 

[cpp] view plain copy
  1. unsigned char fpu_counter;  

    (7)、blktrace是一个针对Linux内核中块设备I/O层的跟踪工具。 

[cpp] view plain copy
  1. #ifdef CONFIG_BLK_DEV_IO_TRACE  
  2.     unsigned int btrace_seq;  
  3. #endif  

    (8)、RCU同步原语 

[cpp] view plain copy
  1. #ifdef CONFIG_PREEMPT_RCU  
  2.     int rcu_read_lock_nesting;  
  3.     char rcu_read_unlock_special;  
  4.     struct list_head rcu_node_entry;  
  5. #endif /* #ifdef CONFIG_PREEMPT_RCU */  
  6. #ifdef CONFIG_TREE_PREEMPT_RCU  
  7.     struct rcu_node *rcu_blocked_node;  
  8. #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */  
  9. #ifdef CONFIG_RCU_BOOST  
  10.     struct rt_mutex *rcu_boost_mutex;  
  11. #endif /* #ifdef CONFIG_RCU_BOOST */  

    (9)、用于调度器统计进程的运行信息 

[cpp] view plain copy
  1. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)  
  2.     struct sched_info sched_info;  
  3. #endif  

    (10)、用于构建进程链表 

[cpp] view plain copy
  1. struct list_head tasks;  

    (11)、to limit pushing to one attempt 

[cpp] view plain copy
  1. #ifdef CONFIG_SMP  
  2.     struct plist_node pushable_tasks;  
  3. #endif  

    补丁说明请参考:http://lkml.indiana.edu/hypermail/linux/kernel/0808.3/0503.html

    (12)、防止内核堆栈溢出 

[cpp] view plain copy
  1. #ifdef CONFIG_CC_STACKPROTECTOR  
  2.     /* Canary value for the -fstack-protector gcc feature */  
  3.     unsigned long stack_canary;  
  4. #endif  

    在GCC编译内核时,需要加上-fstack-protector选项。

    (13)、PID散列表和链表 

[cpp] view plain copy
  1. /* PID/PID hash table linkage. */  
  2. struct pid_link pids[PIDTYPE_MAX];  
  3. struct list_head thread_group; //线程组中所有进程的链表  

    (14)、do_fork函数 

[cpp] view plain copy
  1. struct completion *vfork_done;      /* for vfork() */  
  2. int __user *set_child_tid;      /* CLONE_CHILD_SETTID */  
  3. int __user *clear_child_tid;        /* CLONE_CHILD_CLEARTID */  

    在执行do_fork()时,如果给定特别标志,则vfork_done会指向一个特殊地址。

    如果copy_process函数的clone_flags参数的值被置为CLONE_CHILD_SETTID或CLONE_CHILD_CLEARTID,则会把child_tidptr参数的值分别复制到set_child_tid和clear_child_tid成员。这些标志说明必须改变子进程用户态地址空间的child_tidptr所指向的变量的值。

    (15)、缺页统计 

[cpp] view plain copy
  1. /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */  
  2.     unsigned long min_flt, maj_flt;  

    (16)、进程权能 

[cpp] view plain copy
  1. const struct cred __rcu *real_cred; /* objective and real subjective task 
  2.                  * credentials (COW) */  
  3. const struct cred __rcu *cred;  /* effective (overridable) subjective task 
  4.                  * credentials (COW) */  
  5. struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */  

    (17)、相应的程序名 

[cpp] view plain copy
  1. char comm[TASK_COMM_LEN];  

    (18)、文件 

[cpp] view plain copy
  1. /* file system info */  
  2.     int link_count, total_link_count;  
  3. /* filesystem information */  
  4.     struct fs_struct *fs;  
  5. /* open file information */  
  6.     struct files_struct *files;  

    fs用来表示进程与文件系统的联系,包括当前目录和根目录。

    files表示进程当前打开的文件。

    (19)、进程通信(SYSVIPC) 

[cpp] view plain copy
  1. #ifdef CONFIG_SYSVIPC  
  2. /* ipc stuff */  
  3.     struct sysv_sem sysvsem;  
  4. #endif  

    (20)、处理器特有数据 

[cpp] view plain copy
  1. /* CPU-specific state of this task */  
  2.     struct thread_struct thread;  

    (21)、命名空间 

[cpp] view plain copy
  1. /* namespaces */  
  2.     struct nsproxy *nsproxy;  

    (22)、进程审计 

[cpp] view plain copy
  1.     struct audit_context *audit_context;  
  2. #ifdef CONFIG_AUDITSYSCALL  
  3.     uid_t loginuid;  
  4.     unsigned int sessionid;  
  5. #endif  

    (23)、secure computing 

[cpp] view plain copy
  1. seccomp_t seccomp;  

    (24)、用于copy_process函数使用CLONE_PARENT 标记时 

[cpp] view plain copy
  1. /* Thread group tracking */  
  2.     u32 parent_exec_id;  
  3.     u32 self_exec_id;  

    (25)、中断 

[cpp] view plain copy
  1. #ifdef CONFIG_GENERIC_HARDIRQS  
  2.     /* IRQ handler threads */  
  3.     struct irqaction *irqaction;  
  4. #endif  
  5. #ifdef CONFIG_TRACE_IRQFLAGS  
  6.     unsigned int irq_events;  
  7.     unsigned long hardirq_enable_ip;  
  8.     unsigned long hardirq_disable_ip;  
  9.     unsigned int hardirq_enable_event;  
  10.     unsigned int hardirq_disable_event;  
  11.     int hardirqs_enabled;  
  12.     int hardirq_context;  
  13.     unsigned long softirq_disable_ip;  
  14.     unsigned long softirq_enable_ip;  
  15.     unsigned int softirq_disable_event;  
  16.     unsigned int softirq_enable_event;  
  17.     int softirqs_enabled;  
  18.     int softirq_context;  
  19. #endif  

    (26)、task_rq_lock函数所使用的锁 

[cpp] view plain copy
  1. /* Protection of the PI data structures: */  
  2. raw_spinlock_t pi_lock;  

    (27)、基于PI协议的等待互斥锁,其中PI指的是priority inheritance(优先级继承) 

[cpp] view plain copy
  1. #ifdef CONFIG_RT_MUTEXES  
  2.     /* PI waiters blocked on a rt_mutex held by this task */  
  3.     struct plist_head pi_waiters;  
  4.     /* Deadlock detection and priority inheritance handling */  
  5.     struct rt_mutex_waiter *pi_blocked_on;  
  6. #endif  

    (28)、死锁检测 

[cpp] view plain copy
  1. #ifdef CONFIG_DEBUG_MUTEXES  
  2.     /* mutex deadlock detection */  
  3.     struct mutex_waiter *blocked_on;  
  4. #endif  

    (29)、lockdep,参见内核说明文档linux-2.6.38.8/Documentation/lockdep-design.txt 

[cpp] view plain copy
  1. #ifdef CONFIG_LOCKDEP  
  2. # define MAX_LOCK_DEPTH 48UL  
  3.     u64 curr_chain_key;  
  4.     int lockdep_depth;  
  5.     unsigned int lockdep_recursion;  
  6.     struct held_lock held_locks[MAX_LOCK_DEPTH];  
  7.     gfp_t lockdep_reclaim_gfp;  
  8. #endif  

    (30)、JFS文件系统 

[cpp] view plain copy
  1. /* journalling filesystem info */  
  2.     void *journal_info;  

    (31)、块设备链表 

[cpp] view plain copy
  1. /* stacked block device info */  
  2.     struct bio_list *bio_list;  

    (32)、内存回收 

[cpp] view plain copy
  1. struct reclaim_state *reclaim_state;  

    (33)、存放块设备I/O数据流量信息

[cpp] view plain copy
  1. struct backing_dev_info *backing_dev_info;  
    (34)、I/O调度器所使用的信息 
[cpp] view plain copy
  1. struct io_context *io_context;  

    (35)、记录进程的I/O计数 

[cpp] view plain copy
  1. struct task_io_accounting ioac;  
  2. if defined(CONFIG_TASK_XACCT)  
  3. u64 acct_rss_mem1;  /* accumulated rss usage */  
  4. u64 acct_vm_mem1;   /* accumulated virtual memory usage */  
  5. cputime_t acct_timexpd; /* stime + utime since last update */  
  6. endif  

    在Ubuntu 11.04上,执行cat获得进程1的I/O计数如下: 

[cpp] view plain copy
  1. $ sudo cat /proc/1/io  
[cpp] view plain copy
  1. rchar: 164258906  
  2. wchar: 455212837  
  3. syscr: 388847  
  4. syscw: 92563  
  5. read_bytes: 439251968  
  6. write_bytes: 14143488  
  7. cancelled_write_bytes: 2134016  

    输出的数据项刚好是task_io_accounting结构体的所有成员。

    (36)、CPUSET功能 

[cpp] view plain copy
  1. #ifdef CONFIG_CPUSETS  
  2.     nodemask_t mems_allowed;    /* Protected by alloc_lock */  
  3.     int mems_allowed_change_disable;  
  4.     int cpuset_mem_spread_rotor;  
  5.     int cpuset_slab_spread_rotor;  
  6. #endif  

    (37)、Control Groups 

[cpp] view plain copy
  1. #ifdef CONFIG_CGROUPS  
  2.     /* Control Group info protected by css_set_lock */  
  3.     struct css_set __rcu *cgroups;  
  4.     /* cg_list protected by css_set_lock and tsk->alloc_lock */  
  5.     struct list_head cg_list;  
  6. #endif  
  7. #ifdef CONFIG_CGROUP_MEM_RES_CTLR /* memcg uses this to do batch job */  
  8.     struct memcg_batch_info {  
  9.         int do_batch;   /* incremented when batch uncharge started */  
  10.         struct mem_cgroup *memcg; /* target memcg of uncharge */  
  11.         unsigned long bytes;        /* uncharged usage */  
  12.         unsigned long memsw_bytes; /* uncharged mem+swap usage */  
  13.     } memcg_batch;  
  14. #endif  

    (38)、futex同步机制 

[cpp] view plain copy
  1. #ifdef CONFIG_FUTEX  
  2.     struct robust_list_head __user *robust_list;  
  3. #ifdef CONFIG_COMPAT  
  4.     struct compat_robust_list_head __user *compat_robust_list;  
  5. #endif  
  6.     struct list_head pi_state_list;  
  7.     struct futex_pi_state *pi_state_cache;  
  8. #endif  

    (39)、非一致内存访问(NUMA  Non-Uniform Memory Access) 

[cpp] view plain copy
  1. #ifdef CONFIG_NUMA  
  2.     struct mempolicy *mempolicy;    /* Protected by alloc_lock */  
  3.     short il_next;  
  4. #endif  

    (40)、文件系统互斥资源 

[cpp] view plain copy
  1. atomic_t fs_excl;   /* holding fs exclusive resources */  

    (41)、RCU链表 

[cpp] view plain copy
  1. struct rcu_head rcu;  

    (42)、管道 

[cpp] view plain copy
  1. struct pipe_inode_info *splice_pipe;  

    (43)、延迟计数 

[cpp] view plain copy
  1. #ifdef  CONFIG_TASK_DELAY_ACCT  
  2.     struct task_delay_info *delays;  
  3. #endif  

    (44)、fault injection,参考内核说明文件linux-2.6.38.8/Documentation/fault-injection/fault-injection.txt 

[cpp] view plain copy
  1. #ifdef CONFIG_FAULT_INJECTION  
  2.     int make_it_fail;  
  3. #endif  

    (45)、FLoating proportions 

[cpp] view plain copy
  1. struct prop_local_single dirties;  

    (46)、Infrastructure for displayinglatency 

[cpp] view plain copy
  1. #ifdef CONFIG_LATENCYTOP  
  2.     int latency_record_count;  
  3.     struct latency_record latency_record[LT_SAVECOUNT];  
  4. #endif  

    (47)、time slack values,常用于poll和select函数 

[cpp] view plain copy
  1. unsigned long timer_slack_ns;  
  2. unsigned long default_timer_slack_ns;  

    (48)、socket控制消息(control message) 

[cpp] view plain copy
  1. struct list_head    *scm_work_list;  

    (49)、ftrace跟踪器 

[cpp] view plain copy
  1. #ifdef CONFIG_FUNCTION_GRAPH_TRACER  
  2.     /* Index of current stored address in ret_stack */  
  3.     int curr_ret_stack;  
  4.     /* Stack of return addresses for return function tracing */  
  5.     struct ftrace_ret_stack *ret_stack;  
  6.     /* time stamp for last schedule */  
  7.     unsigned long long ftrace_timestamp;  
  8.     /* 
  9.      * Number of functions that haven't been traced 
  10.      * because of depth overrun. 
  11.      */  
  12.     atomic_t trace_overrun;  
  13.     /* Pause for the tracing */  
  14.     atomic_t tracing_graph_pause;  
  15. #endif  
  16. #ifdef CONFIG_TRACING  
  17.     /* state flags for use by tracers */  
  18.     unsigned long trace;  
  19.     /* bitmask of trace recursion */  
  20.     unsigned long trace_recursion;  
  21. #endif /* CONFIG_TRACING */  
原创粉丝点击