linux-headers-2.6.32-40-generic 里PCB结构体的定义
- struct task_struct {
- volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
- void *stack;
- atomic_t usage;
- unsigned int flags; /* per process flags, defined below */
- unsigned int ptrace;
- int lock_depth; /* BKL lock depth */
- #ifdef CONFIG_SMP
- #ifdef __ARCH_WANT_UNLOCKED_CTXSW
- int oncpu;
- #endif
- #endif
- 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;
- #ifdef CONFIG_PREEMPT_NOTIFIERS
- /* list of struct preempt_notifier: */
- struct hlist_head preempt_notifiers;
- #endif
- /*
- * fpu_counter contains the number of consecutive context switches
- * that the FPU is used. If this is over a threshold, the lazy fpu
- * saving becomes unlazy to save the trap. This is an unsigned char
- * so that after 256 times the counter wraps and the behavior turns
- * lazy again; this to deal with bursty apps that only use FPU for
- * a short time
- */
- unsigned char fpu_counter;
- #ifdef CONFIG_BLK_DEV_IO_TRACE
- unsigned int btrace_seq;
- #endif
- unsigned int policy;
- cpumask_t cpus_allowed;
- #ifdef CONFIG_TREE_PREEMPT_RCU
- int rcu_read_lock_nesting;
- char rcu_read_unlock_special;
- struct rcu_node *rcu_blocked_node;
- struct list_head rcu_node_entry;
- #endif /* #ifdef CONFIG_TREE_PREEMPT_RCU */
- #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
- struct sched_info sched_info;
- #endif
- struct list_head tasks;
- struct plist_node pushable_tasks;
- struct mm_struct *mm, *active_mm;
- /* task state */
- int exit_state;
- 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;
- pid_t pid;
- pid_t tgid;
- #ifdef CONFIG_CC_STACKPROTECTOR
- /* Canary value for the -fstack-protector gcc feature */
- unsigned long stack_canary;
- #endif
- /*
- * pointers to (original) parent process, youngest child, younger sibling,
- * older sibling, respectively. (p->father can be replaced with
- * p->real_parent->pid)
- */
- struct task_struct *real_parent; /* real parent process */
- struct task_struct *parent; /* recipient of SIGCHLD, wait4() reports */
- /*
- * children/sibling forms the list of my natural children
- */
- 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 */
- /*
- * ptraced is the list of tasks this task is using ptrace on.
- * This includes both natural children and PTRACE_ATTACH targets.
- * p->ptrace_entry is p's link on the p->parent->ptraced list.
- */
- struct list_head ptraced;
- struct list_head ptrace_entry;
- /*
- * This is the tracer handle for the ptrace BTS extension.
- * This field actually belongs to the ptracer task.
- */
- struct bts_context *bts;
- /* PID/PID hash table linkage. */
- struct pid_link pids[PIDTYPE_MAX];
- struct list_head thread_group;
- struct completion *vfork_done; /* for vfork() */
- int __user *set_child_tid; /* CLONE_CHILD_SETTID */
- int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
- cputime_t utime, stime, utimescaled, stimescaled;
- cputime_t gtime;
- cputime_t prev_utime, prev_stime;
- unsigned long nvcsw, nivcsw; /* context switch counts */
- struct timespec start_time; /* monotonic time */
- struct timespec real_start_time; /* boot based time */
- /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
- unsigned long min_flt, maj_flt;
- struct task_cputime cputime_expires;
- struct list_head cpu_timers[3];
- /* process credentials */
- const struct cred *real_cred; /* objective and real subjective task
- * credentials (COW) */
- const struct cred *cred; /* effective (overridable) subjective task
- * credentials (COW) */
- struct mutex cred_guard_mutex; /* guard against foreign influences on
- * credential calculations
- * (notably. ptrace) */
- struct cred *replacement_session_keyring; /* for KEYCTL_SESSION_TO_PARENT */
- char comm[TASK_COMM_LEN]; /* executable name excluding path
- - access with [gs]et_task_comm (which lock
- it with task_lock())
- - initialized normally by setup_new_exec */
- /* file system info */
- int link_count, total_link_count;
- #ifdef CONFIG_SYSVIPC
- /* ipc stuff */
- struct sysv_sem sysvsem;
- #endif
- #ifdef CONFIG_DETECT_HUNG_TASK
- /* hung task detection */
- unsigned long last_switch_count;
- #endif
- /* CPU-specific state of this task */
- struct thread_struct thread;
- /* filesystem information */
- struct fs_struct *fs;
- /* open file information */
- struct files_struct *files;
- /* namespaces */
- struct nsproxy *nsproxy;
- /* 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;
- struct audit_context *audit_context;
- #ifdef CONFIG_AUDITSYSCALL
- uid_t loginuid;
- unsigned int sessionid;
- #endif
- seccomp_t seccomp;
- /* Thread group tracking */
- u32 parent_exec_id;
- u32 self_exec_id;
- /* Protection of (de-)allocation: mm, files, fs, tty, keyrings, mems_allowed,
- * mempolicy */
- spinlock_t alloc_lock;
- #ifdef CONFIG_GENERIC_HARDIRQS
- /* IRQ handler threads */
- struct irqaction *irqaction;
- #endif
- /* Protection of the PI data structures: */
- spinlock_t pi_lock;
- #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
- #ifdef CONFIG_DEBUG_MUTEXES
- /* mutex deadlock detection */
- struct mutex_waiter *blocked_on;
- #endif
- #ifdef CONFIG_TRACE_IRQFLAGS
- unsigned int irq_events;
- int hardirqs_enabled;
- unsigned long hardirq_enable_ip;
- unsigned int hardirq_enable_event;
- unsigned long hardirq_disable_ip;
- unsigned int hardirq_disable_event;
- int softirqs_enabled;
- unsigned long softirq_disable_ip;
- unsigned int softirq_disable_event;
- unsigned long softirq_enable_ip;
- unsigned int softirq_enable_event;
- int hardirq_context;
- int softirq_context;
- #endif
- #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
- /* journalling filesystem info */
- void *journal_info;
- /* stacked block device info */
- struct bio *bio_list, **bio_tail;
- /* VM state */
- struct reclaim_state *reclaim_state;
- struct backing_dev_info *backing_dev_info;
- struct io_context *io_context;
- unsigned long ptrace_message;
- siginfo_t *last_siginfo; /* For ptrace use. */
- 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
- #ifdef CONFIG_CPUSETS
- nodemask_t mems_allowed; /* Protected by alloc_lock */
- int cpuset_mem_spread_rotor;
- #endif
- #ifdef CONFIG_CGROUPS
- /* Control Group info protected by css_set_lock */
- struct css_set *cgroups;
- /* cg_list protected by css_set_lock and tsk->alloc_lock */
- struct list_head cg_list;
- #endif
- #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
- #ifdef CONFIG_PERF_EVENTS
- struct perf_event_context *perf_event_ctxp;
- struct mutex perf_event_mutex;
- struct list_head perf_event_list;
- #endif
- #ifdef CONFIG_NUMA
- struct mempolicy *mempolicy; /* Protected by alloc_lock */
- short il_next;
- #endif
- atomic_t fs_excl; /* holding fs exclusive resources */
- struct rcu_head rcu;
- /*
- * cache last used pipe for splice
- */
- struct pipe_inode_info *splice_pipe;
- #ifdef CONFIG_TASK_DELAY_ACCT
- struct task_delay_info *delays;
- #endif
- #ifdef CONFIG_FAULT_INJECTION
- int make_it_fail;
- #endif
- struct prop_local_single dirties;
- #ifdef CONFIG_LATENCYTOP
- int latency_record_count;
- struct latency_record latency_record[LT_SAVECOUNT];
- #endif
- /*
- * time slack values; these are used to round up poll() and
- * select() etc timeout values. These are in nanoseconds.
- */
- unsigned long timer_slack_ns;
- unsigned long default_timer_slack_ns;
- struct list_head *scm_work_list;
- #ifdef CONFIG_FUNCTION_GRAPH_TRACER
- /* Index of current stored adress 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.
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参数 限制字段 差错字段 返回值 信号处理数组 |
|
进程区表 区的类型和大小 区的状态 区在物理存储器中的位置 引用计数 指向文件索引结点的指针 |
|
系统区表 |
进程区表项、系统区表项和区的关系 |
|
|
进程的数据结构 |
|
|
进程状态与进程映像 |
进程状态 |
|
阅读(1118) | 评论(0) | 转发(0) |