Linux进程之task_struct结构体

进程是处于执行期的程序以及它所管理的资源(如打开的文件、挂起的信号、进程状态、地址空间等等)的总称。注意,程序并不是进程,实际上两个或多个进程不仅有可能执行同一程序,而且还有可能共享地址空间等资源。

Linux内核通过一个被称为进程描述符的task_struct结构体来管理进程,这个结构体包含了一个进程所需的所有信息。它定义在linux-2.6.38.8/include/linux/sched.h文件中。

本文将尽力就task_struct结构体所有成员的用法进行简要说明。

1、进程状态 
volatile long state;
int exit_state;
state成员的可能取值如下:

#define TASK_RUNNING        0
#define TASK_INTERRUPTIBLE  1
#define TASK_UNINTERRUPTIBLE    2
#define __TASK_STOPPED      4
#define __TASK_TRACED       8
/* in tsk->exit_state */
#define EXIT_ZOMBIE     16
#define EXIT_DEAD       32
/* in tsk->state again */
#define TASK_DEAD       64
#define TASK_WAKEKILL       128
#define TASK_WAKING     256

系统中的每个进程都必然处于以上所列进程状态中的一种。

TASK_RUNNING表示进程要么正在执行,要么正要准备执行。

TASK_INTERRUPTIBLE表示进程被阻塞(睡眠),直到某个条件变为真。条件一旦达成,进程的状态就被设置为TASK_RUNNING。

TASK_UNINTERRUPTIBLE的意义与TASK_INTERRUPTIBLE类似,除了不能通过接受一个信号来唤醒以外。

__TASK_STOPPED表示进程被停止执行。

__TASK_TRACED表示进程被debugger等进程监视。

EXIT_ZOMBIE表示进程的执行被终止,但是其父进程还没有使用wait()等系统调用来获知它的终止信息。

EXIT_DEAD表示进程的最终状态。

EXIT_ZOMBIE和EXIT_DEAD也可以存放在exit_state成员中。

2、进程标识符(PID)

pid_t pid;
pid_t tgid;

在CONFIG_BASE_SMALL配置为0的情况下,PID的取值范围是0到32767,即系统中的进程数最大为32768个。

/* linux-2.6.38.8/include/linux/threads.h */
#define PID_MAX_DEFAULT (CONFIG_BASE_SMALL ? 0x1000 : 0x8000)

在Linux系统中,一个线程组中的所有线程使用和该线程组的领头线程(该组中的第一个轻量级进程)相同的PID,并被存放在tgid成员中。只有线程组的领头线程的pid成员才会被设置为与tgid相同的值。注意,getpid()系统调用返回的是当前进程的tgid值而不是pid值。

3、进程内核栈

void *stack;

进程通过alloc_thread_info函数分配它的内核栈,通过free_thread_info函数释放所分配的内核栈。


/* linux-2.6.38.8/kernel/fork.c */ 
static inline struct thread_info *alloc_thread_info(struct task_struct *tsk)
{
#ifdef CONFIG_DEBUG_STACK_USAGE
    gfp_t mask = GFP_KERNEL | __GFP_ZERO;
#else
    gfp_t mask = GFP_KERNEL;
#endif
    return (struct thread_info *)__get_free_pages(mask, THREAD_SIZE_ORDER);
}
static inline void free_thread_info(struct thread_info *ti)
{
    free_pages((unsigned long)ti, THREAD_SIZE_ORDER);
}

其中,THREAD_SIZE_ORDER宏在linux-2.6.38.8/arch/arm/include/asm/thread_info.h文件中被定义为1,也就是说alloc_thread_info函数通过调用__get_free_pages函数分配2个页的内存(它的首地址是8192字节对齐的)。

Linux内核通过thread_union联合体来表示进程的内核栈,其中THREAD_SIZE宏的大小为8192。

union thread_union {
struct thread_info thread_info;
unsigned long stack[THREAD_SIZE/sizeof(long)];
};
当进程从用户态切换到内核态时,进程的内核栈总是空的,所以ARM的sp寄存器指向这个栈的顶端。因此,内核能够轻易地通过sp寄存器获得当前正在CPU上运行的进程。


/* linux-2.6.38.8/arch/arm/include/asm/current.h */
static inline struct task_struct *get_current(void)
{
    return current_thread_info()->task;
}

#define current (get_current())

/* linux-2.6.38.8/arch/arm/include/asm/thread_info.h */ 
static inline struct thread_info *current_thread_info(void)
{
    register unsigned long sp asm ("sp");
    return (struct thread_info *)(sp & ~(THREAD_SIZE - 1));
}

进程内核栈与进程描述符的关系如下图:

4、标记

unsigned int flags; /* per process flags, defined below */
flags成员的可能取值如下:


#define PF_KSOFTIRQD    0x00000001  /* I am ksoftirqd */  
#define PF_STARTING 0x00000002  /* being created */  
#define PF_EXITING  0x00000004  /* getting shut down */  
#define PF_EXITPIDONE   0x00000008  /* pi exit done on shut down */  
#define PF_VCPU     0x00000010  /* I'm a virtual CPU */  
#define PF_WQ_WORKER    0x00000020  /* I'm a workqueue worker */  
#define PF_FORKNOEXEC   0x00000040  /* forked but didn't exec */  
#define PF_MCE_PROCESS  0x00000080      /* process policy on mce errors */  
#define PF_SUPERPRIV    0x00000100  /* used super-user privileges */  
#define PF_DUMPCORE 0x00000200  /* dumped core */  
#define PF_SIGNALED 0x00000400  /* killed by a signal */  
#define PF_MEMALLOC 0x00000800  /* Allocating memory */  
#define PF_USED_MATH    0x00002000  /* if unset the fpu must be initialized before use */  
#define PF_FREEZING 0x00004000  /* freeze in progress. do not account to load */  
#define PF_NOFREEZE 0x00008000  /* this thread should not be frozen */  
#define PF_FROZEN   0x00010000  /* frozen for system suspend */  
#define PF_FSTRANS  0x00020000  /* inside a filesystem transaction */  
#define PF_KSWAPD   0x00040000  /* I am kswapd */  
#define PF_OOM_ORIGIN   0x00080000  /* Allocating much memory to others */  
#define PF_LESS_THROTTLE 0x00100000 /* Throttle me less: I clean memory */  
#define PF_KTHREAD  0x00200000  /* I am a kernel thread */  
#define PF_RANDOMIZE    0x00400000  /* randomize virtual address space */  
#define PF_SWAPWRITE    0x00800000  /* Allowed to write to swap */  
#define PF_SPREAD_PAGE  0x01000000  /* Spread page cache over cpuset */  
#define PF_SPREAD_SLAB  0x02000000  /* Spread some slab caches over cpuset */  
#define PF_THREAD_BOUND 0x04000000  /* Thread bound to specific cpu */  
#define PF_MCE_EARLY    0x08000000      /* Early kill for mce process policy */  
#define PF_MEMPOLICY    0x10000000  /* Non-default NUMA mempolicy */  
#define PF_MUTEX_TESTER 0x20000000  /* Thread belongs to the rt mutex tester */  
#define PF_FREEZER_SKIP 0x40000000  /* Freezer should not count it as freezable */  
#define PF_FREEZER_NOSIG 0x80000000 /* Freezer won't send signals to it */  
5、表示进程亲属关系的成员 

struct task_struct real_parent; / real parent process */
struct task_struct parent; / recipient of SIGCHLD, wait4() reports */
struct list_head children; /* list of my children */
struct list_head sibling; /* linkage in my parent’s children list */
struct task_struct group_leader; / threadgroup leader */
在Linux系统中,所有进程之间都有着直接或间接地联系,每个进程都有其父进程,也可能有零个或多个子进程。拥有同一父进程的所有进程具有兄弟关系。

real_parent指向其父进程,如果创建它的父进程不再存在,则指向PID为1的init进程。
parent指向其父进程,当它终止时,必须向它的父进程发送信号。它的值通常与real_parent相同。
children表示链表的头部,链表中的所有元素都是它的子进程。
sibling用于把当前进程插入到兄弟链表中。
group_leader指向其所在进程组的领头进程。

6、ptrace系统调用

unsigned int ptrace;
struct list_head ptraced;
struct list_head ptrace_entry;
unsigned long ptrace_message;
siginfo_t last_siginfo; / For ptrace use. */
ifdef CONFIG_HAVE_HW_BREAKPOINT
atomic_t ptrace_bp_refcnt;
endif
成员ptrace被设置为0时表示不需要被跟踪,它的可能取值如下:

/* linux-2.6.38.8/include/linux/ptrace.h */

#define PT_PTRACED  0x00000001  
#define PT_DTRACE   0x00000002  /* delayed trace (used on m68k, i386) */  
#define PT_TRACESYSGOOD 0x00000004  
#define PT_PTRACE_CAP   0x00000008  /* ptracer can follow suid-exec */  
#define PT_TRACE_FORK   0x00000010  
#define PT_TRACE_VFORK  0x00000020  
#define PT_TRACE_CLONE  0x00000040  
#define PT_TRACE_EXEC   0x00000080  
#define PT_TRACE_VFORK_DONE 0x00000100  
#define PT_TRACE_EXIT   0x00000200

7、Performance Event


#ifdef CONFIG_PERF_EVENTS  
    struct perf_event_context *perf_event_ctxp[perf_nr_task_contexts];  
    struct mutex perf_event_mutex;  
    struct list_head perf_event_list;  
#endif  

Performance Event是一款随 Linux 内核代码一同发布和维护的性能诊断工具。这些成员用于帮助PerformanceEvent分析进程的性能问题。

关于Performance Event工具的介绍可参考文章http://www.ibm.com/developerworks/cn/linux/l-cn-perf1/index.html?ca=drs-#major1http://www.ibm.com/developerworks/cn/linux/l-cn-perf2/index.html?ca=drs-#major1

8、进程调度

int prio, static_prio, normal_prio;
unsigned int rt_priority;
const struct sched_class *sched_class;
struct sched_entity se;
struct sched_rt_entity rt;
unsigned int policy;
cpumask_t cpus_allowed;
实时优先级范围是0到MAX_RT_PRIO-1(即99),而普通进程的静态优先级范围是从MAX_RT_PRIO到MAX_PRIO-1(即100到139)。值越大静态优先级越低。


/* linux-2.6.38.8/include/linux/sched.h */  
#define MAX_USER_RT_PRIO    100  
#define MAX_RT_PRIO     MAX_USER_RT_PRIO  

#define MAX_PRIO        (MAX_RT_PRIO + 40)  
#define DEFAULT_PRIO        (MAX_RT_PRIO + 20)  
static_prio用于保存静态优先级,可以通过nice系统调用来进行修改。

rt_priority用于保存实时优先级。

normal_prio的值取决于静态优先级和调度策略。

prio用于保存动态优先级。

policy表示进程的调度策略,目前主要有以下五种: 

#define SCHED_NORMAL        0  
#define SCHED_FIFO      1  
#define SCHED_RR        2  
#define SCHED_BATCH     3  
/* SCHED_ISO: reserved but not implemented yet */  
#define SCHED_IDLE      5  
SCHED_NORMAL用于普通进程,通过CFS调度器实现。SCHED_BATCH用于非交互的处理器消耗型进程。SCHED_IDLE是在系统负载很低时使用。

SCHED_FIFO(先入先出调度算法)和SCHED_RR(轮流调度算法)都是实时调度策略。

sched_class结构体表示调度类,目前内核中有实现以下四种: 

/* linux-2.6.38.8/kernel/sched_fair.c */
static const struct sched_class fair_sched_class;
/* linux-2.6.38.8/kernel/sched_rt.c */
static const struct sched_class rt_sched_class;
/* linux-2.6.38.8/kernel/sched_idletask.c */
static const struct sched_class idle_sched_class;
/* linux-2.6.38.8/kernel/sched_stoptask.c */
static const struct sched_class stop_sched_class;
se和rt都是调用实体,一个用于普通进程,一个用于实时进程,每个进程都有其中之一的实体。

cpus_allowed用于控制进程可以在哪里处理器上运行。

9、进程地址空间


struct mm_struct *mm, *active_mm;  
#ifdef CONFIG_COMPAT_BRK  
    unsigned brk_randomized:1;  
#endif  
#if defined(SPLIT_RSS_COUNTING)  
    struct task_rss_stat    rss_stat;  
#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、判断标志

int exit_code, exit_signal;
int pdeath_signal; /* The signal sent when the parent dies */
/* ??? */
unsigned int personality;
unsigned did_exec:1;
unsigned in_execve:1; /* Tell the LSMs that the process is doing an
* execve */
unsigned in_iowait:1;

/* Revert to default priority/policy when forking */
unsigned sched_reset_on_fork:1;
exit_code用于设置进程的终止代号,这个值要么是_exit()或exit_group()系统调用参数(正常终止),要么是由内核提供的一个错误代号(异常终止)。

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

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

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

enum {
PER_LINUX = 0x0000,
PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
WHOLE_SECONDS | SHORT_INODE,
PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
PER_BSD = 0x0006,
PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
PER_LINUX32 = 0x0008,
PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
PER_RISCOS = 0x000c,
PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
PER_OSF4 = 0x000f, /* OSF/1 v4 */
PER_HPUX = 0x0010,
PER_MASK = 0x00ff,
};
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、时间

    cputime_t utime, stime, utimescaled, stimescaled;  
    cputime_t gtime;  
#ifndef CONFIG_VIRT_CPU_ACCOUNTING  
    cputime_t prev_utime, prev_stime;  
#endif  
    unsigned long nvcsw, nivcsw; /* context switch counts */  
    struct timespec start_time;         /* monotonic time */  
    struct timespec real_start_time;    /* boot based time */  
    struct task_cputime cputime_expires;  
    struct list_head cpu_timers[3];  
#ifdef CONFIG_DETECT_HUNG_TASK  
/* hung task detection */  
    unsigned long last_switch_count;  
#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、信号处理

/* signal handlers */
struct signal_struct *signal;
struct sighand_struct *sighand;

sigset_t blocked, real_blocked;  
sigset_t saved_sigmask; /* restored if set_restore_sigmask() was used */  
struct sigpending pending;  

unsigned long sas_ss_sp;  
size_t sas_ss_size;  
int (*notifier)(void *priv);  
void *notifier_data;  
sigset_t *notifier_mask;  
signal指向进程的信号描述符。

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

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

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

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

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

13、其他

(1)、用于保护资源分配或释放的自旋锁
/* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
* mempolicy */
spinlock_t alloc_lock;
(2)、进程描述符使用计数,被置为2时,表示进程描述符正在被使用而且其相应的进程处于活动状态。

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

int lock_depth; /* BKL lock depth */
(4)、在SMP上帮助实现无加锁的进程切换(unlocked context switches)

#ifdef CONFIG_SMP  
#ifdef __ARCH_WANT_UNLOCKED_CTXSW  
    int oncpu;  
#endif  
#endif  
(5)、preempt_notifier结构体链表 
#ifdef CONFIG_PREEMPT_NOTIFIERS  
    /* list of struct preempt_notifier: */  
    struct hlist_head preempt_notifiers;  
#endif  

(6)、FPU使用计数

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

#ifdef CONFIG_BLK_DEV_IO_TRACE  
    unsigned int btrace_seq;  
#endif  

(8)、RCU同步原语

#ifdef CONFIG_PREEMPT_RCU  
    int rcu_read_lock_nesting;  
    char rcu_read_unlock_special;  
    struct list_head rcu_node_entry;  
#endif /* #ifdef CONFIG_PREEMPT_RCU */  
#ifdef CONFIG_TREE_PREEMPT_RCU  
    struct rcu_node *rcu_blocked_node;  
#endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */  
#ifdef CONFIG_RCU_BOOST  
    struct rt_mutex *rcu_boost_mutex;  
#endif /* #ifdef CONFIG_RCU_BOOST */  

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

#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)  
    struct sched_info sched_info;  
#endif 

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

struct list_head tasks;
(11)、to limit pushing to one attempt

#ifdef CONFIG_SMP  
    struct plist_node pushable_tasks;  
#endif 

补丁说明请参考:http://lkml.indiana.edu/hypermail/linux/kernel/0808.3/0503.html
(12)、防止内核堆栈溢出

#ifdef CONFIG_CC_STACKPROTECTOR  
    /* Canary value for the -fstack-protector gcc feature */  
    unsigned long stack_canary;  
#endif  
在GCC编译内核时,需要加上-fstack-protector选项。

(13)、PID散列表和链表

/* PID/PID hash table linkage. */
struct pid_link pids[PIDTYPE_MAX];
struct list_head thread_group; //线程组中所有进程的链表
(14)、do_fork函数

struct completion vfork_done; / for vfork() */
int __user set_child_tid; / CLONE_CHILD_SETTID */
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)、缺页统计
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
unsigned long min_flt, maj_flt;
(16)、进程权能
const struct cred __rcu real_cred; / objective and real subjective task
* credentials (COW) */
const struct cred __rcu cred; / effective (overridable) subjective task
* credentials (COW) */
struct cred replacement_session_keyring; / for KEYCTL_SESSION_TO_PARENT */
(17)、相应的程序名

char comm[TASK_COMM_LEN];
(18)、文件

/* file system info */
int link_count, total_link_count;
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
fs用来表示进程与文件系统的联系,包括当前目录和根目录。

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

(19)、进程通信(SYSVIPC)

#ifdef CONFIG_SYSVIPC  
/* ipc stuff */  
    struct sysv_sem sysvsem;  
#endif 

(20)、处理器特有数据

/* CPU-specific state of this task */
struct thread_struct thread;
(21)、命名空间

[cpp] view plain copy
/* namespaces */
struct nsproxy *nsproxy;
(22)、进程审计

    struct audit_context *audit_context;  
#ifdef CONFIG_AUDITSYSCALL  
    uid_t loginuid;  
    unsigned int sessionid;  
#endif 

(23)、secure computing
seccomp_t seccomp;
(24)、用于copy_process函数使用CLONE_PARENT 标记时
/* Thread group tracking */
u32 parent_exec_id;
u32 self_exec_id;
(25)、中断


#ifdef CONFIG_GENERIC_HARDIRQS  
    /* IRQ handler threads */  
    struct irqaction *irqaction;  
#endif  
#ifdef CONFIG_TRACE_IRQFLAGS  
    unsigned int irq_events;  
    unsigned long hardirq_enable_ip;  
    unsigned long hardirq_disable_ip;  
    unsigned int hardirq_enable_event;  
    unsigned int hardirq_disable_event;  
    int hardirqs_enabled;  
    int hardirq_context;  
    unsigned long softirq_disable_ip;  
    unsigned long softirq_enable_ip;  
    unsigned int softirq_disable_event;  
    unsigned int softirq_enable_event;  
    int softirqs_enabled;  
    int softirq_context;  
#endif 

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

/* Protection of the PI data structures: */
raw_spinlock_t pi_lock;
(27)、基于PI协议的等待互斥锁,其中PI指的是priority inheritance(优先级继承)

#ifdef CONFIG_RT_MUTEXES  
    /* PI waiters blocked on a rt_mutex held by this task */  
    struct plist_head pi_waiters;  
    /* Deadlock detection and priority inheritance handling */  
    struct rt_mutex_waiter *pi_blocked_on;  
#endif 

(28)、死锁检测

#ifdef CONFIG_DEBUG_MUTEXES  
    /* mutex deadlock detection */  
    struct mutex_waiter *blocked_on;  
#endif

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

#ifdef CONFIG_LOCKDEP  
# define MAX_LOCK_DEPTH 48UL  
    u64 curr_chain_key;  
    int lockdep_depth;  
    unsigned int lockdep_recursion;  
    struct held_lock held_locks[MAX_LOCK_DEPTH];  
    gfp_t lockdep_reclaim_gfp;  
#endif  

(30)、JFS文件系统
/* journalling filesystem info */
void *journal_info;
(31)、块设备链表
/* stacked block device info */
struct bio_list *bio_list;
(32)、内存回收
struct reclaim_state *reclaim_state;
(33)、存放块设备I/O数据流量信息
struct backing_dev_info *backing_dev_info;
(34)、I/O调度器所使用的信息
struct io_context *io_context;
(35)、记录进程的I/O计数
struct task_io_accounting ioac;
if defined(CONFIG_TASK_XACCT)
u64 acct_rss_mem1; /* accumulated rss usage */
u64 acct_vm_mem1; /* accumulated virtual memory usage */
cputime_t acct_timexpd; /* stime + utime since last update */
endif
在Ubuntu 11.04上,执行cat获得进程1的I/O计数如下:

$ sudo cat /proc/1/io

rchar: 164258906
wchar: 455212837
syscr: 388847
syscw: 92563
read_bytes: 439251968
write_bytes: 14143488
cancelled_write_bytes: 2134016
输出的数据项刚好是task_io_accounting结构体的所有成员。

(36)、CPUSET功能

#ifdef CONFIG_CPUSETS  
    nodemask_t mems_allowed;    /* Protected by alloc_lock */  
    int mems_allowed_change_disable;  
    int cpuset_mem_spread_rotor;  
    int cpuset_slab_spread_rotor;  
#endif  
(37)、Control Groups 
#ifdef CONFIG_CGROUPS  
    /* Control Group info protected by css_set_lock */  
    struct css_set __rcu *cgroups;  
    /* cg_list protected by css_set_lock and tsk->alloc_lock */  
    struct list_head cg_list;  
#endif  
#ifdef CONFIG_CGROUP_MEM_RES_CTLR /* memcg uses this to do batch job */  
    struct memcg_batch_info {  
        int do_batch;   /* incremented when batch uncharge started */  
        struct mem_cgroup *memcg; /* target memcg of uncharge */  
        unsigned long bytes;        /* uncharged usage */  
        unsigned long memsw_bytes; /* uncharged mem+swap usage */  
    } memcg_batch;  
#endif 

(38)、futex同步机制

#ifdef CONFIG_FUTEX  
    struct robust_list_head __user *robust_list;  
#ifdef CONFIG_COMPAT  
    struct compat_robust_list_head __user *compat_robust_list;  
#endif  
    struct list_head pi_state_list;  
    struct futex_pi_state *pi_state_cache;  
#endif  39)、非一致内存访问(NUMA  Non-Uniform Memory Access) 

#ifdef CONFIG_NUMA  
    struct mempolicy *mempolicy;    /* Protected by alloc_lock */  
    short il_next;  
#endif  40)、文件系统互斥资源 
atomic_t fs_excl;   /* holding fs exclusive resources */41)、RCU链表 
struct rcu_head rcu;  
(42)、管道 
struct pipe_inode_info *splice_pipe;  
(43)、延迟计数 
#ifdef  CONFIG_TASK_DELAY_ACCT  
    struct task_delay_info *delays;  
#endif  44)、fault injection,参考内核说明文件linux-2.6.38.8/Documentation/fault-injection/fault-injection.txt 
#ifdef CONFIG_FAULT_INJECTION  
    int make_it_fail;  
#endif  45)、FLoating proportions 
struct prop_local_single dirties;  
(46)、Infrastructure for displayinglatency 
#ifdef CONFIG_LATENCYTOP  
    int latency_record_count;  
    struct latency_record latency_record[LT_SAVECOUNT];  
#endif  47)、time slack values,常用于poll和select函数 
unsigned long timer_slack_ns;  
unsigned long default_timer_slack_ns;  
(48)、socket控制消息(control message) 
struct list_head    *scm_work_list;  
 (49)、ftrace跟踪器 
#ifdef CONFIG_FUNCTION_GRAPH_TRACER  
    /* Index of current stored address in ret_stack */  
    int curr_ret_stack;  
    /* Stack of return addresses for return function tracing */  
    struct ftrace_ret_stack *ret_stack;  
    /* time stamp for last schedule */  
    unsigned long long ftrace_timestamp;  
    /* 
     * Number of functions that haven't been traced 
     * because of depth overrun. 
     */  
    atomic_t trace_overrun;  
    /* Pause for the tracing */  
    atomic_t tracing_graph_pause;  
#endif  
#ifdef CONFIG_TRACING  
    /* state flags for use by tracers */  
    unsigned long trace;  
    /* bitmask of trace recursion */  
    unsigned long trace_recursion;  
#endif /* CONFIG_TRACING */

1、进程状态 ,将纪录进程在等待,运行,或死锁
2、调度信息, 由哪个调度函数调度,怎样调度等
3、进程的通讯状况
4、因为要插入进程树,必须有联系父子兄弟的指针, 当然是task_struct型
5、时间信息, 比如计算好执行的时间, 以便cpu 分配
6、标号 ,决定改进程归属
7、可以读写打开的一些文件信息
8、 进程上下文和内核上下文
9、处理器上下文
10、内存信息


struct task_struct {
volatile long state;  //说明了该进程是否可以执行,还是可中断等信息
unsigned long flags;  //Flage 是进程号,在调用fork()时给出
int sigpending;    //进程上是否有待处理的信号
mm_segment_t addr_limit; //进程地址空间,区分内核进程与普通进程在内存存放的位置不同
    //0-0xBFFFFFFF for user-thead
   //0-0xFFFFFFFF for kernel-thread
//调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度
volatile long need_resched;
int lock_depth;  //锁深度
long nice;       //进程的基本时间片
//进程的调度策略,有三种,实时进程:SCHED_FIFO,SCHED_RR, 分时进程:SCHED_OTHER
unsigned long policy;
struct mm_struct *mm; //进程内存管理信息
int processor;
//若进程不在任何CPU上运行, cpus_runnable 的值是0,否则是1 这个值在运行队列被锁时更新
unsigned long cpus_runnable, cpus_allowed;
struct list_head run_list; //指向运行队列的指针
unsigned long sleep_time;  //进程的睡眠时间
//用于将系统中所有的进程连成一个双向循环链表, 其根是init_task
struct task_struct *next_task, *prev_task;
struct mm_struct *active_mm;
struct list_head local_pages;       //指向本地页面      
unsigned int allocation_order, nr_local_pages;
struct linux_binfmt *binfmt;  //进程所运行的可执行文件的格式
int exit_code, exit_signal;
int pdeath_signal;     //父进程终止是向子进程发送的信号
unsigned long personality;
//Linux可以运行由其他UNIX操作系统生成的符合iBCS2标准的程序
int did_exec:1; 
pid_t pid;    //进程标识符,用来代表一个进程
pid_t pgrp;   //进程组标识,表示进程所属的进程组
pid_t tty_old_pgrp;  //进程控制终端所在的组标识
pid_t session;  //进程的会话标识
pid_t tgid;
int leader;     //表示进程是否为会话主管
struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr;
struct list_head thread_group;   //线程链表
struct task_struct *pidhash_next; //用于将进程链入HASH表
struct task_struct **pidhash_pprev;
wait_queue_head_t wait_chldexit;  //供wait4()使用
struct completion *vfork_done;  //供vfork() 使用
unsigned long rt_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重置初值。
unsigned long it_real_value, it_prof_value, it_virt_value;
unsigned long it_real_incr, it_prof_incr, it_virt_value;
struct timer_list real_timer;   //指向实时定时器的指针
struct tms times;      //记录进程消耗的时间
unsigned long start_time;  //进程创建的时间
//记录进程在每个CPU上所消耗的用户态时间和核心态时间
long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; 
//内存缺页和交换信息:
//min_flt, maj_flt累计进程的次缺页数(Copy on Write页和匿名页)和主缺页数(从映射文件或交换
//设备读入的页面数); nswap记录进程累计换出的页面数,即写到交换设备上的页面数。
//cmin_flt, cmaj_flt, cnswap记录本进程为祖先的所有子孙进程的累计次缺页数,主缺页数和换出页面数。
//在父进程回收终止的子进程时,父进程会将子进程的这些信息累计到自己结构的这些域中
unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
int swappable:1; //表示进程的虚拟地址空间是否允许换出
//进程认证信息
//uid,gid为运行该进程的用户的用户标识符和组标识符,通常是进程创建者的uid,gid
//euid,egid为有效uid,gid
//fsuid,fsgid为文件系统uid,gid,这两个ID号通常与有效uid,gid相等,在检查对于文件
//系统的访问权限时使用他们。
//suid,sgid为备份uid,gid
uid_t uid,euid,suid,fsuid;
gid_t gid,egid,sgid,fsgid;
int ngroups; //记录进程在多少个用户组中
gid_t groups[NGROUPS]; //记录进程所在的组
//进程的权能,分别是有效位集合,继承位集合,允许位集合
kernel_cap_t cap_effective, cap_inheritable, cap_permitted;
int keep_capabilities:1;
struct user_struct *user;
struct rlimit rlim[RLIM_NLIMITS];  //与进程相关的资源限制信息
unsigned short used_math;   //是否使用FPU
char comm[16];   //进程正在运行的可执行文件名
 //文件系统信息
int link_count, total_link_count;
//NULL if no tty 进程所在的控制终端,如果不需要控制终端,则该指针为空
struct tty_struct *tty;
unsigned int locks;
//进程间通信信息
struct sem_undo *semundo;  //进程在信号灯上的所有undo操作
struct sem_queue *semsleeping; //当进程因为信号灯操作而挂起时,他在该队列中记录等待的操作
//进程的CPU状态,切换时,要保存到停止进程的task_struct中
struct thread_struct thread;
  //文件系统信息
struct fs_struct *fs;
  //打开文件信息
struct files_struct *files;
  //信号处理函数
spinlock_t sigmask_lock;
struct signal_struct *sig; //信号处理函数
sigset_t blocked;  //进程当前要阻塞的信号,每个信号对应一位
struct sigpending pending;  //进程上是否有待处理的信号
unsigned long sas_ss_sp;
size_t sas_ss_size;
int (*notifier)(void *priv);
void *notifier_data;
sigset_t *notifier_mask;
u32 parent_exec_id;
u32 self_exec_id;

spinlock_t alloc_lock;
void *journal_info;
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值