linux进程控制块

论坛 期权论坛     
匿名小用户   2019-10-20 17:50   72   0
<p>进程在操作系统中都有一个户口,用于表示这个进程。这个户口操作系统被称为PCB(进程控制块),在linux中具体实现是 task_struct数据结构,它记录了一下几个类型的信息:</p>
<p>1.状态信息,例如这个进程处于可执行状态,休眠,挂起等。</p>
<p>2.性质,由于unix有很多变种,进程有自己独特的性质。</p>
<p>3.资源,资源的链接比如内存,还有资源的限制和权限等。</p>
<p>4.组织,例如按照家族关系建立起来的树(父进程,子进程等)。</p>
<p>下面是这一个数据结构</p>
<pre class="blockcode"><code class="language-cpp">struct task_struct {
    /*
     * offsets of these are hardcoded elsewhere - touch with care
     */
    volatile long state;    /* -1 unrunnable, 0 runnable, &gt;0 stopped */ //进程当前的状态
    unsigned long flags;    /* per process flags, defined below */    //反应进程状态的信息,但不是运行状态,定义见下
    int sigpending; //进程收到了信号,但尚未处理
    mm_segment_t addr_limit;    /* thread address space: //虚存地址上限
                         0-0xBFFFFFFF for user-thead
                        0-0xFFFFFFFF for kernel-thread
                     */
    struct exec_domain *exec_domain;
    volatile long need_resched;    //与进程调度有关表示用户从系统空间按返回用户空间要执行的一次调度
    unsigned long ptrace;

    int lock_depth;        /* Lock depth */

/*
* offset 32 begins here on 32-bit platforms. We keep
* all fields in a single cacheline that are needed for
* the goodness() loop in schedule().
*/
    long counter; //与进程调度相关
    long nice;
    unsigned long policy;    //实用于本进程的调度政策
    struct mm_struct *mm;
    int processor;
    /*
     * cpus_runnable is ~0 if the process is not running on any
     * CPU. It's (1 &lt;&lt; cpu) if it's running on a CPU. This mask
     * is updated under the runqueue lock.
     *
     * To determine whether a process might run on a CPU, this
     * mask is AND-ed with cpus_allowed.
     */
    unsigned long cpus_runnable, cpus_allowed;
    /*
     * (only the 'next' pointer fits into the cacheline, but
     * that's just fine.)
     */
    struct list_head run_list;
    unsigned long sleep_time;

    struct task_struct *next_task, *prev_task; //内核会对每一个进程做点什么事情的时候,常常需要将其连成一个队列,这2个指针用于这个目的
    struct mm_struct *active_mm;
    struct list_head local_pages;
    unsigned int allocation_order, nr_local_pages;

/* task state */
    struct linux_binfmt *binfmt;//应用文件格式
    int exit_code, exit_signal;
    int pdeath_signal;  /*  The signal sent when the parent dies  */
    /* ??? */
    unsigned long personality; //进程的个性化信息,详细见下
    int did_exec:1;
    unsigned task_dumpable:1;
    pid_t pid; //进程号
    pid_t pgrp;
    pid_t tty_old_pgrp;
    pid_t session;
    pid_t tgid;
    /* boolean value for session group leader */
    int leader;
    /*
     * pointers to (original) parent process, youngest child, younger sibling,
     * older sibling, respectively.  (p-&gt;father can be replaced with
     * p-&gt;p_pptr-&gt;pid)
     */
    struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr; //用于族谱信息的,例如p_opptr指向父进程
    struct list_head thread_group;

    /* PID hash table linkage. */
    struct task_struct *pidhash_next;
    struct task_struct **pidhash_pprev; //pid是随机分配的,我们常常使用kill pid想进程发送信号(大部分人认为是杀死进程,其实这是个发送信号的指令,默认的参数为杀死。如果想暂停某进程,只需kill STOP 进程的PID),这里可以看到根据pid寻找进程的操作是经常被使用的,而pid又是随机分配,于是这里边用这2个指针指向一个杂凑数组,数组是按照杂凑的算法,以pid为关键字建立,方便根据pid来寻找task_struct

    wait_queue_head_t wait_chldexit;    /* for wait4() */
    struct completion *vfork_done;        /* for vfork() */
    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;
    struct tms times; //运行时间的总汇
    unsigned long start_time;
    long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; //在多个处理器上运行于系统空间和用户空间的时间
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
    unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;//发生页面异常的次数和换入换出的次数
    int swappable:1;
/* process credentials */
    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;    //指向该进程拥有的用户
/* limits */
    struct rlimit rlim[RLIM_NLIMITS]; //进程对各种资源使用数量的限制,详细见下
    unsigned short used_math;
    char comm[16];
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:50
帖子:1079
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP