进程控制块——PCB

PCB进程控制块的结构:

  进程控制块(PCB)是系统为了管理进程设置的一个专门的数据结构。系统用它来记录进程的外部特征,描述进程的运动变化过程。同时,系统可以利用PCB来管理和控制进程,所以说,PCB是系统感知进程存在的唯一标识。

  Linux系统下的PCB包含了很多参数,每个PCB约占1KB的内存空间大小。

主要包含的内容:

1 进程标识符(唯一)
2 进程的当前状态,通常同一个状态的进程会被放到同一个队列中(五种状态)
3 进程的程序和数据地址
4 进程的资源清单。列出所拥有的除了CPU外的资源记录
5 进程优先级,反映进程的紧迫程度
6 CPU现场保护。记录中断时的CPU状态
7 进程队列中的PCB的链接字
8 进程的其他相关信息。记账用的,如占CPU多长时间等。
9  

用于表示PCB的结构task_struck简要描述如下:

​include/linux/sched.h

struct task_struct {
    volatile long state;
    struct thread_info *thread_info;
    atomic_t usage;
    unsigned long flags; 
    unsigned long ptrace;
 
    int lock_depth;
 
    int prio, static_prio;
    struct list_head run_list;
    prio_array_t *array;
 
    unsigned long sleep_avg;
    long interactive_credit;
    unsigned long long timestamp;
    int activated;
 
    unsigned long policy;
    cpumask_t cpus_allowed;
    unsigned int time_slice, first_time_slice;
         
    struct list_head tasks;
    struct list_head ptrace_children;
    struct list_head ptrace_list;
 
    struct mm_struct *mm, *active_mm;
    ...
    struct linux_binfmt *binfmt;
    int exit_code, exit_signal;
    int pdeath_signal;
    ...
    pid_t pid;
    pid_t tgid;    
    ...
    struct task_struct *real_parent;
    struct task_struct *parent;
    struct list_head children;
    struct list_head sibling;
    struct task_struct *group_leader;
    ...
    struct pid_link pids[PIDTYPE_MAX];
 
    wait_queue_head_t wait_chldexit;
    struct completion *vfork_done;
    int __user *set_child_tid;
    int __user *clear_child_tid;
 
    unsigned long rt_priority;
    unsigned long it_real_value, it_prof_value, it_virt_value;
    unsigned long it_real_incr, it_prof_incr, it_virt_incr;
    struct timer_list real_timer;
    unsigned long utime, stime, cutime, cstime;
    unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw;
    u64 start_time;
    ...
    uid_t uid,euid,suid,fsuid;
    gid_t gid,egid,sgid,fsgid;
    struct group_info *group_info;
    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;
    char comm[16];
    ...
    int link_count, total_link_count;
    ...
    struct fs_struct *fs;
    ...
    struct files_struct *files;
    ...
    unsigned long ptrace_message;
    siginfo_t *last_siginfo;
    ...
 };​
调度数据成员:

1》volatile long state:表示进程当前的运行状态①TASK RUNNING(可运行)②TASK INTERRUPT IBLE(可中断的等待状态)③TASK UNINTERRUPTIBLE(不可中断的等待状态)④TASK ZOMBIE(僵死状态)⑤TASK STOPPED(暂停状态)等五种。

2》long priority进程优先级,priority的值给出了每个进程每次获得CPU后,可使用的时间片长度(单位jiffies)。

3》unsigned long rt_priority rt_priority的值给出了实时进程的优先级。

4》long counter在轮转法调度是counter表示当前进程还可以运行多久。在进程刚开始时其被赋值为priority,以后每个一个时钟中断递减1,减到0引起新一轮调度。

5》unsigned long policy表示该进程的进程调度策略。有:SCKED_OTHER 0,非实时进程,用基于优先权的轮转法;SCHED_FIFO 1实时进程,用先进先出算法;SCHED-RR 2实时进程,用基于优先权的轮转法。

进程队列指针:

1》struct task_struct* next_task,*prev_task 在Linux中所有进程(以PCB的形式)组成一个双向链表。

2》struct task_struct* p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr分别指向该进程的原始父进程,父进程,子进程和新老兄弟进程的指针。

3》struct task_struct* pidhash_next;struct task_struct** pidhash_pprev用于链入进程hash表的前后指针。系统进程除了链入双向链表外,还被加到了hash表中。

进程标识:

1》uid  gid 分别为运行进程的用户标识和用户组标识。

2》pid pgrp分别是运行进程的进程标识号和进程组标识号。

3》时间数据成员:long per_cpu_utime[NR_CPUS]是用户进程运行时间,per_cpu_stime[NR_CPUS]内核进程运行的时间,进程创建时间unsigned long start_time

文件系统结构成员:

1》struct fs_struct* fs;fs保存了进程本身与VFS(虚拟文件系统)的关系信息。

struct fs-struct

{

    atom ic_t count;

    rw lock_lock;

    int umask;

    struct dentry* root,*pwd,*altroot;

    struct vfsm ount* rootmnt,*pwdmnt,*altrootmnt;

}

内部数据成员:

1》struct mm_stuct* mm;在Linux中,采用按需分页的策略解决进程的内存要求。mm是指向关于储存管理的mm_truct结构。

2》struct mm_struct* active_mm;指向活动地址空间。

3》mm_segm ent_t addr_lim it表示线程空间地址。用户线程空间地址:0...0xBFFFFFFF。内核线程空间地址:0...0xFFFFFFFF.

4》spinlock_t alloc_lock;用于申请空间时用的自旋锁。自旋锁的主要功能是临界区保护。

页面管理:

1》int swappable:1 进程占用的页面是否可换出。swappable 为1表示可换出。

2》unsigned long min_flt,maj_flt;该进程累计minor缺页次数和major缺页次数。

3》unsigned long nswap该进程累计换出页面数

4》unsigned long swap_cnt下一次循环最多可换出的页数。

猜你喜欢

转载自blog.csdn.net/genzld/article/details/84473037