Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215414
  • 博文数量: 127
  • 博客积分: 1998
  • 博客等级: 上尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 16:41
文章分类

全部博文(127)

文章存档

2014年(41)

2013年(1)

2012年(85)

分类: C/C++

2012-03-27 16:52:33

linux-headers-2.6.32-40-generic 里PCB结构体的定义

点击(此处)折叠或打开

  1. struct task_struct {
  2.     volatile long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
  3.     void *stack;
  4.     atomic_t usage;
  5.     unsigned int flags;    /* per process flags, defined below */
  6.     unsigned int ptrace;

  7.     int lock_depth;        /* BKL lock depth */

  8. #ifdef CONFIG_SMP
  9. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  10.     int oncpu;
  11. #endif
  12. #endif

  13.     int prio, static_prio, normal_prio;
  14.     unsigned int rt_priority;
  15.     const struct sched_class *sched_class;
  16.     struct sched_entity se;
  17.     struct sched_rt_entity rt;

  18. #ifdef CONFIG_PREEMPT_NOTIFIERS
  19.     /* list of struct preempt_notifier: */
  20.     struct hlist_head preempt_notifiers;
  21. #endif

  22.     /*
  23.      * fpu_counter contains the number of consecutive context switches
  24.      * that the FPU is used. If this is over a threshold, the lazy fpu
  25.      * saving becomes unlazy to save the trap. This is an unsigned char
  26.      * so that after 256 times the counter wraps and the behavior turns
  27.      * lazy again; this to deal with bursty apps that only use FPU for
  28.      * a short time
  29.      */
  30.     unsigned char fpu_counter;
  31. #ifdef CONFIG_BLK_DEV_IO_TRACE
  32.     unsigned int btrace_seq;
  33. #endif

  34.     unsigned int policy;
  35.     cpumask_t cpus_allowed;

  36. #ifdef CONFIG_TREE_PREEMPT_RCU
  37.     int rcu_read_lock_nesting;
  38.     char rcu_read_unlock_special;
  39.     struct rcu_node *rcu_blocked_node;
  40.     struct list_head rcu_node_entry;
  41. #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */

  42. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  43.     struct sched_info sched_info;
  44. #endif

  45.     struct list_head tasks;
  46.     struct plist_node pushable_tasks;

  47.     struct mm_struct *mm, *active_mm;

  48. /* task state */
  49.     int exit_state;
  50.     int exit_code, exit_signal;
  51.     int pdeath_signal; /* The signal sent when the parent dies */
  52.     /* ??? */
  53.     unsigned int personality;
  54.     unsigned did_exec:1;
  55.     unsigned in_execve:1;    /* Tell the LSMs that the process is doing an
  56.                  * execve */
  57.     unsigned in_iowait:1;


  58.     /* Revert to default priority/policy when forking */
  59.     unsigned sched_reset_on_fork:1;

  60.     pid_t pid;
  61.     pid_t tgid;

  62. #ifdef CONFIG_CC_STACKPROTECTOR
  63.     /* Canary value for the -fstack-protector gcc feature */
  64.     unsigned long stack_canary;
  65. #endif

  66.     /*
  67.      * pointers to (original) parent process, youngest child, younger sibling,
  68.      * older sibling, respectively. (p->father can be replaced with
  69.      * p->real_parent->pid)
  70.      */
  71.     struct task_struct *real_parent; /* real parent process */
  72.     struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
  73.     /*
  74.      * children/sibling forms the list of my natural children
  75.      */
  76.     struct list_head children;    /* list of my children */
  77.     struct list_head sibling;    /* linkage in my parent's children list */
  78.     struct task_struct *group_leader;    /* threadgroup leader */

  79.     /*
  80.      * ptraced is the list of tasks this task is using ptrace on.
  81.      * This includes both natural children and PTRACE_ATTACH targets.
  82.      * p->ptrace_entry is p's link on the p->parent->ptraced list.
  83.      */
  84.     struct list_head ptraced;
  85.     struct list_head ptrace_entry;

  86.     /*
  87.      * This is the tracer handle for the ptrace BTS extension.
  88.      * This field actually belongs to the ptracer task.
  89.      */
  90.     struct bts_context *bts;

  91.     /* PID/PID hash table linkage. */
  92.     struct pid_link pids[PIDTYPE_MAX];
  93.     struct list_head thread_group;

  94.     struct completion *vfork_done;        /* for vfork() */
  95.     int __user *set_child_tid;        /* CLONE_CHILD_SETTID */
  96.     int __user *clear_child_tid;        /* CLONE_CHILD_CLEARTID */

  97.     cputime_t utime, stime, utimescaled, stimescaled;
  98.     cputime_t gtime;
  99.     cputime_t prev_utime, prev_stime;
  100.     unsigned long nvcsw, nivcsw; /* context switch counts */
  101.     struct timespec start_time;         /* monotonic time */
  102.     struct timespec real_start_time;    /* boot based time */
  103. /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
  104.     unsigned long min_flt, maj_flt;

  105.     struct task_cputime cputime_expires;
  106.     struct list_head cpu_timers[3];

  107. /* process credentials */
  108.     const struct cred *real_cred;    /* objective and real subjective task
  109.                      * credentials (COW) */
  110.     const struct cred *cred;    /* effective (overridable) subjective task
  111.                      * credentials (COW) */
  112.     struct mutex cred_guard_mutex;    /* guard against foreign influences on
  113.                      * credential calculations
  114.                      * (notably. ptrace) */
  115.     struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */

  116.     char comm[TASK_COMM_LEN]; /* executable name excluding path
  117.                  - access with [gs]et_task_comm (which lock
  118.                  it with task_lock())
  119.                  - initialized normally by setup_new_exec */
  120. /* file system info */
  121.     int link_count, total_link_count;
  122. #ifdef CONFIG_SYSVIPC
  123. /* ipc stuff */
  124.     struct sysv_sem sysvsem;
  125. #endif
  126. #ifdef CONFIG_DETECT_HUNG_TASK
  127. /* hung task detection */
  128.     unsigned long last_switch_count;
  129. #endif
  130. /* CPU-specific state of this task */
  131.     struct thread_struct thread;
  132. /* filesystem information */
  133.     struct fs_struct *fs;
  134. /* open file information */
  135.     struct files_struct *files;
  136. /* namespaces */
  137.     struct nsproxy *nsproxy;
  138. /* signal handlers */
  139.     struct signal_struct *signal;
  140.     struct sighand_struct *sighand;

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

  144.     unsigned long sas_ss_sp;
  145.     size_t sas_ss_size;
  146.     int (*notifier)(void *priv);
  147.     void *notifier_data;
  148.     sigset_t *notifier_mask;
  149.     struct audit_context *audit_context;
  150. #ifdef CONFIG_AUDITSYSCALL
  151.     uid_t loginuid;
  152.     unsigned int sessionid;
  153. #endif
  154.     seccomp_t seccomp;

  155. /* Thread group tracking */
  156.        u32 parent_exec_id;
  157.        u32 self_exec_id;
  158. /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
  159.  * mempolicy */
  160.     spinlock_t alloc_lock;

  161. #ifdef CONFIG_GENERIC_HARDIRQS
  162.     /* IRQ handler threads */
  163.     struct irqaction *irqaction;
  164. #endif

  165.     /* Protection of the PI data structures: */
  166.     spinlock_t pi_lock;

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

  173. #ifdef CONFIG_DEBUG_MUTEXES
  174.     /* mutex deadlock detection */
  175.     struct mutex_waiter *blocked_on;
  176. #endif
  177. #ifdef CONFIG_TRACE_IRQFLAGS
  178.     unsigned int irq_events;
  179.     int hardirqs_enabled;
  180.     unsigned long hardirq_enable_ip;
  181.     unsigned int hardirq_enable_event;
  182.     unsigned long hardirq_disable_ip;
  183.     unsigned int hardirq_disable_event;
  184.     int softirqs_enabled;
  185.     unsigned long softirq_disable_ip;
  186.     unsigned int softirq_disable_event;
  187.     unsigned long softirq_enable_ip;
  188.     unsigned int softirq_enable_event;
  189.     int hardirq_context;
  190.     int softirq_context;
  191. #endif
  192. #ifdef CONFIG_LOCKDEP
  193. # define MAX_LOCK_DEPTH 48UL
  194.     u64 curr_chain_key;
  195.     int lockdep_depth;
  196.     unsigned int lockdep_recursion;
  197.     struct held_lock held_locks[MAX_LOCK_DEPTH];
  198.     gfp_t lockdep_reclaim_gfp;
  199. #endif

  200. /* journalling filesystem info */
  201.     void *journal_info;

  202. /* stacked block device info */
  203.     struct bio *bio_list, **bio_tail;

  204. /* VM state */
  205.     struct reclaim_state *reclaim_state;

  206.     struct backing_dev_info *backing_dev_info;

  207.     struct io_context *io_context;

  208.     unsigned long ptrace_message;
  209.     siginfo_t *last_siginfo; /* For ptrace use. */
  210.     struct task_io_accounting ioac;
  211. #if defined(CONFIG_TASK_XACCT)
  212.     u64 acct_rss_mem1;    /* accumulated rss usage */
  213.     u64 acct_vm_mem1;    /* accumulated virtual memory usage */
  214.     cputime_t acct_timexpd;    /* stime + utime since last update */
  215. #endif
  216. #ifdef CONFIG_CPUSETS
  217.     nodemask_t mems_allowed;    /* Protected by alloc_lock */
  218.     int cpuset_mem_spread_rotor;
  219. #endif
  220. #ifdef CONFIG_CGROUPS
  221.     /* Control Group info protected by css_set_lock */
  222.     struct css_set *cgroups;
  223.     /* cg_list protected by css_set_lock and tsk->alloc_lock */
  224.     struct list_head cg_list;
  225. #endif
  226. #ifdef CONFIG_FUTEX
  227.     struct robust_list_head __user *robust_list;
  228. #ifdef CONFIG_COMPAT
  229.     struct compat_robust_list_head __user *compat_robust_list;
  230. #endif
  231.     struct list_head pi_state_list;
  232.     struct futex_pi_state *pi_state_cache;
  233. #endif
  234. #ifdef CONFIG_PERF_EVENTS
  235.     struct perf_event_context *perf_event_ctxp;
  236.     struct mutex perf_event_mutex;
  237.     struct list_head perf_event_list;
  238. #endif
  239. #ifdef CONFIG_NUMA
  240.     struct mempolicy *mempolicy;    /* Protected by alloc_lock */
  241.     short il_next;
  242. #endif
  243.     atomic_t fs_excl;    /* holding fs exclusive resources */
  244.     struct rcu_head rcu;

  245.     /*
  246.      * cache last used pipe for splice
  247.      */
  248.     struct pipe_inode_info *splice_pipe;
  249. #ifdef    CONFIG_TASK_DELAY_ACCT
  250.     struct task_delay_info *delays;
  251. #endif
  252. #ifdef CONFIG_FAULT_INJECTION
  253.     int make_it_fail;
  254. #endif
  255.     struct prop_local_single dirties;
  256. #ifdef CONFIG_LATENCYTOP
  257.     int latency_record_count;
  258.     struct latency_record latency_record[LT_SAVECOUNT];
  259. #endif
  260.     /*
  261.      * time slack values; these are used to round up poll() and
  262.      * select() etc timeout values. These are in nanoseconds.
  263.      */
  264.     unsigned long timer_slack_ns;
  265.     unsigned long default_timer_slack_ns;

  266.     struct list_head    *scm_work_list;
  267. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  268.     /* Index of current stored adress in ret_stack */
  269.     int curr_ret_stack;
  270.     /* Stack of return addresses for return function tracing */
  271.     struct ftrace_ret_stack    *ret_stack;
  272.     /* time stamp for last schedule */
  273.     unsigned long long ftrace_timestamp;
  274.     /*
  275.      * Number of functions that haven't been traced
  276.      * because of depth overrun.
  277.      */
  278.     atomic_t trace_overrun;
  279.     /* Pause for the tracing */
  280.     atomic_t tracing_graph_pause;
  281. #endif
  282. #ifdef CONFIG_TRACING
  283.     /* state flags for use by tracers */
  284.     unsigned long trace;
  285.     /* bitmask of trace recursion */
  286.     unsigned long trace_recursion;
  287. #endif /* CONFIG_TRACING */
  288. };

1.

    Linux的进程控制块为一个由结构task_struct所定义的数据结构,task_struct存
/include/ linux/sched.h 中,其中包括管理进程所需的各种信息。Linux系统的所有进程控制块组织成结构数组形式。早期的Linux版本是多可同时运行进程的个数由NR_TASK(缺省值为512)规定,NR_TASK即为PCB结果数组的长度。近期版本中的PCB组成一个环形结构,系统中实际存在的进程数由其定义的全局变量nr_task来动态记录。结构数组:struct task_struct *task[NR_TASK]={&init_task}来记录指向各PCB的指针,该指针数组定义于/kernel/sched.c中。


    在创建一个新进程时,系统在内存中申请一个空的task_struct区,即空闲PCB块,并填入所需信息。同时将指向该结构的指针填入到task[]数组中。当前处于运行状态进程的PCB用指针数组current_set[]来指出。这是因为Linux支持多处理机系统,系统内可能存在多个同时运行的进程,故current_set定义成指针数组。
    Linux系统的PCB包括很多参数,每个PCB约占1KB多的内存空间。用于表示PCB的结构task_struct简要描述如下:
struct task_struct{
...
unsigned short uid;
int pid;
int processor;
...
volatile long state;
long prority;
unsighed long rt_prority;
long counter;
unsigned long flags;
unsigned long policy;
...
Struct task_struct *next_task, *prev_task;
Struct task_struct *next_run,*prev_run;
Struct task_struct *p_opptr,*p_pptr,*p_cptr,*pysptr,*p_ptr;
...
};
下面对部分数据成员进行说明:
(1)unsigned short pid 为用户标识
(2)int pid 为进程标识
(3)int processor标识用户正在使用的CPU,以支持对称多处理机方式;
(4)volatile long state 标识进程的状态,可为下列六种状态之一:
可运行状态(TASK-RUNING);
可中断阻塞状态(TASK-UBERRUPTIBLE)
不可中断阻塞状态(TASK-UNINTERRUPTIBLE)
僵死状态(TASK-ZOMBLE)
暂停态(TASK_STOPPED)
交换态(TASK_SWAPPING)
(5)long prority表示进程的优先级
(6)unsigned long rt_prority 表示实时进程的优先级,对于普通进程无效
(7)long counter 为进程动态优先级计数器,用于进程轮转调度算法
(8)unsigned long policy 表示进程调度策略,其值为下列三种情况之一:
SCHED_OTHER(值为0)对应普通进程优先级轮转法(round robin)
SCHED_FIFO(值为1)对应实时进程先来先服务算法;
SCHED_RR(值为2)对应实时进程优先级轮转法
(9)struct task_struct *next_task,*prev_task为进程PCB双向链表的前后项指针
(10)struct task_struct *next_run,*prev_run为就绪队列双向链表的前后项指针
(11)struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_ptr指明进程家族间的关系,分别为指向祖父进程、父进程、子进程以及新老进程的指针。

一个进程在其生存期内,可处于一组不同的状态下,称为进程状态。见下图2-6所示。进程状态保存在进程任务结构的state字段中。当进程正在等待系统中的资源而处于等待状态时,则称奇处于睡眠等待状态。在Linux系统中,睡眠等待状态被分为可中断的和不可中断的等待状态。

运行状态(TASK_RUNNING)

  当进程正在被CPU执行,或已经准备就绪随时可以由调度程序执行,则称该进程为处于运行状态(running)。进程可以在内核态运行,也可以在用户态运行。当系统资源已经可用时,进程就被唤醒而进入准备运行状态,该状态称为就绪态。这些状态在内核中表示方法相同,都被称为处于TASK_RUNNING状态。

  可中断睡眠状态(TASK_INTERRUPTIBLE)

  当进程处于可中断等待状态时,系统不会调度该进程执行。当系统产生一个中断或者释放了进程正在等待的资源,或者进程收到一个信号,都可以唤醒进程转换到就绪状态(运行状态)。

  不可中断睡眠状态(TASK_UNINTERRUPTIBLE)

  与可中断睡眠状态类似。但处于该状态的进程只有被使用wake_up()函数明确唤醒时才能被转换到可运行就绪状态。

  暂停状态(TASK_STOPPED)

  当进程收到信号SIGSTOP,SIGTSTP,SIGTTIN或SIGTTOU时就会进入暂停状态。可向其发送SIGCONT信号让进程转换到可运行状态。在Linux0.11中,还为实现对该状态的转换处理。处于该状态的进程将被作为进程终止来处理。

  僵死状态(TASK_ZOMBIE)

  当进程已停止运行,但其父进程还没有询问其状态时,则称该进城处于僵死状态。

当一个进程的运行时间片用完,系统就会使用调度程序强制切换到其他的进程去执行。另外,如果进程在内核态执行时需要等待系统的某个资源,此时该进城就会调用sleep_on()或者sleep_on_interruptible()自愿放弃CPU使用权,而让调度程序去执行其他程序。进程则进入睡眠状态(TASK_UNINTERRUPTIBLE或TASK_INTERRUPTIBLE)。

  只有当进程从"内核运行态"转移到"睡眠状态"时,内核才会进行进城切换操作。在内核态下运行的进程不能被其他进程抢占,而且一个进程不能改变另一个进程的状态。为了避免进程切换时造成内核数据错误,内核在执行临街区代码时禁止一切中断。

2.Unix(教科书10.2节有详细介绍)

在 UNIX 系统Ⅴ中, 把进程控制块分为四部分:
   进程表项

      进程标识符(PID)

      用户标识符(UID)

      进程状态

      事件描述符

      进程和U区在内存或外存的地址

      软中断信息

      计时域

      进程的大小

      偏置值nice

      P-Link指针

      指向U区进程正文、数据及栈在内存区域的指针

 

    U 区  

      进程表项指针

      真正用户标识符u-ruid(real user ID)

      有效用户标识符u-euid(effective user ID)

      用户文件描述符表

      当前目录和当前根

      计时器

      内部I/O参数

      限制字段

      差错字段

      返回值

      信号处理数组

 
     进程区表

      区的类型和大小

      区的状态

      区在物理存储器中的位置

      引用计数

      指向文件索引结点的指针

 
   系统区表 

      进程区表项、系统区表项和区的关系
 
      进程的数据结构
                                                     
进程状态与进程映像
   进程状态
阅读(1157) | 评论(0) | 转发(0) |
0

上一篇:RGB 配色表

下一篇:Linux下的多线程编程

给主人留下些什么吧!~~