进程的概念:
OS:程序的一个执行实例。正在执行的程序,能分配处理器并由处理器执行的实体。
内核:但当分配系统资源的实体。
进程的两个基本元素是程序代码和代码相连的数据集。进程是一种动态描述,但并不代表所有进程都在运行。
进程描述:
每个进程在内核中都有⼀一个进程控制块(PCB)来维护进程相关的信息,Linux内核的
进程控制块是task_struct结构体。现在我们全⾯面了解⼀一下其中都有哪些信息。
task_struct
{
标示符 : 描述本进程的唯⼀一标⽰示符,⽤用来区别其他进程。
状态 :任务状态,退出代码,退出信号等。
优先级 :相对于其他进程的优先级。
程序计数器:程序中即将被执⾏行的下⼀一条指令的地址。
内存指针:包括程序代码和进程相关数据的指针,还有和其他进程共享的内存块的指针。
上下⽂文数据:进程执⾏行时处理器的寄存器中的数据。
I/O状态信息:包括显⽰示的I/O请求,分配给进程的I/O设备和被进程使⽤用的⽂文件列表。
记账信息:可能包括处理器时间总和,使⽤用的时钟数总和,时间限制,记账号等。
}
以结构体形式展示,并且保存在include/linux/sched.h中,可以用vim打开进行阅读,分析。所有运行在系统的进程都以task_struct链表形式存在内核里。
进程的信息可以通过/pro系统文件查看,要获得PID为400的进程信息,则为/proc/400,当然大多数进程可以使用top和ps这些工具获得。
如何找到tack_struct:
在超级用户下,输入:
vim /usr/src/kernels/2.6.32-431.el6.i686/include/linux/sched.h
就可以打开sched.h文件
内容还是比较多的,不过都有注释进行讲解,下面我将库中的描述附于下方:
struct task_struct {
/*调度数据成员---------------------------------------------------------------------*/
volatile long state; //进程运行时状态/* -1 就绪, 0 运行, >0 停止 */
void *stack;
atomic_t usage; //有几个进程正在使用此结构
unsigned int flags; //每个进程的标志,在下面定义
unsigned int ptrace;
int 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
//有限占有链表
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;
//进程的内存描述符,链表和红黑树