为了管理进程,操作系统必须对每个进程所做的事情有一个清楚地描述,为此,操作系统使用数据结构来代表处理的不同实体,这 个数据结构就是所说的进程描述符或者进程控制块。在linux中,把每一个进程的基本信息抽象成一个结构体,这就是task_struct结构体,在include\linux\sched.h文件中定义。
一说到进程,就要想到task_struct结构体。每个进程都会被分配一个task_struct结构,它包含了这个进程的所有信息。在任何时候,操作系统都能跟踪这个结构的信息。
PCB(进程控制块)----task_struct:
struct task_struct{
volatile long state;
//进程的运行状态,-1 不能运行 0 运行 >0 停止
unsigned long flags;
unsigned long flags;
//进程标志
unsigned int rt_priority;
//表示此进程的运行优先级
int sigpending;
int sigpending;
//进程上是否有待处理的信号
mm_segment_t addr_limit;
mm_segment_t addr_limit;
//进程地址空间
//0-0xBFFFFFFF for user-thead
//0-0xFFFFFFFF for kernel-thread
volatile long need_resched;
//0-0xBFFFFFFF for user-thead
//0-0xFFFFFFFF for kernel-thread
volatile long need_resched;
//调度标志,表示该进程是否需要重新调度,若非0,则当从内核态返回到用户态,会发生调度
int lock_depth;
int lock_depth;
//锁深度
long counter;
long counter;
//进程可运行的时间量
long nice;
long nice;
//进程的基本时间片
unsigned long policy;
unsigned long policy;
//进程的调度策略,有三种,实时进程:SCHED_FIFO,SCHED_RR;分时进程:SCHED_OTHER;
struct mm_struct *mm;
struct mm_struct *mm;
//进程内存管理信息
unsigned long cpus_runnable, cpus_allowed;
// 若进程不在任何CPU上运行,cpus_runnable 的值是0,否则是1。这个值在运行 *队列被锁时更新
struct list_head run_list;
struct list_head run_list;
//指向运行队列的指针
unsigned long sleep_time;
unsigned long sleep_time;
//进程的睡眠时间
struct task_struct *next_task, *prev_task;
struct task_struct *next_task, *prev_task;
//用于将系统中所有的进程连成一个双向循环链表,其根是init_task.
struct list_head local_pages;
struct list_head local_pages;
//指向本地页面
struct linux_binfmt *binfmt;
struct linux_binfmt *binfmt;
//进程所运行的可执行文件的格式
int pdeath_signal;
int pdeath_signal;
//父进程终止时向子进程发送的信号
unsigned long personality;
unsigned long personality;
//Linux可以运行由其他UNIX操作系统生成的符合iBCS2标准的程序
pid_t pid;
pid_t pid;
//进程标识符,用来代表一个进程
pid_t pgrp;
pid_t pgrp;
//进程组标识,表示进程所属的进程组
pid_t tty_old_pgrp;
pid_t tty_old_pgrp;
//进程控制终端所在的组标识
pid_t session;
pid_t session;
//进程的会话标识
int leader;
int leader;
//标志,表示进程是否为会话主管
struct list_head thread_group;
struct list_head thread_group;
//线程链表
unsigned long rt_priority;
unsigned long rt_priority;
//实时优先级,用它计算实时进程调度时的weight值
struct timer_list real_timer;
struct timer_list real_timer;
//指向实时定时器的指针
struct tms times;
struct tms times;
//记录进程消耗的时间,
unsigned long start_time;
unsigned long start_time;
//进程创建的时间
long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
//记录进程在每个CPU上所消耗的用户态时间和核心态时间
int swappable:1;
int swappable:1;
//表示进程的虚拟地址空间是否允许换出
int ngroups;
int ngroups;
//记录进程在多少个用户组中
task_struct中
struct thread_struct thread;
/* filesystem information文件系统信息*/
struct fs_struct *fs;
/* open file information *///打开文件信息
struct files_struct *files;
signal handlers ;//信号处理函数
struct signal_struct *sig; //信号处理函数,
sigset_t blocked; //进程当前要阻塞的信号,每个信号对应一位
struct sigpending pending; //进程上是否有待处理的信号
};