当前位置: 首页 > 图文教程 > 操作系统 > Unix/Linux > 读核日记(三)

Unix/Linux
Ubuntu Linux有多流行?数据告诉你
轻松制作Linux系统启动盘的四种方法
忘记Linux密码后的另类解决方法
Linux中安装Realplayer的方法
Linux桌面环境GNOME和KDE的切换
在Linux操作系统下安装与运行GAMIT软件
学习Linux系统的九条忠告
Linux系统发展空间探索
Linux与Windows Vista系统的比较
Linux刷新主板BIOS的好方法
Linux下学C语言开发要学些什么
Ubuntu 8.04 Alpha 3新功能初体验
Ubuntu Linux下用Firefox来安装软件
客户对Linux电脑的需求的确不高
带你一起认识下linux的各种版本
手把手教你学Ubuntu
双系统卸载linux系统的方法
linux系统下的U盘病毒清除法
Linux系统22端口修改的方法
理解和掌握Linux 体验Linux的方式

Unix/Linux 中的 读核日记(三)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 32 ::
收藏到网摘: n/a

 

在linux 中每一个进程都由task_struct 数据结构来定义. task_struct就是我们通常所说的PCB.

她是对进程控制的唯一手段也是最有效的手段.

当我们调用fork() 时, 系统会为我们产生一个task_struct结构.然后从父进程,那里继承一些数据,

并把新的进程插入到进程树中, 以待进行进程管理.因此了解task_struct的结构对于我们理解任务

调度(在linux 中任务和进程是同一概念)的关键.在进行剖析task_struct的定义之前. 我们先按照

我们的理论推一下它的结构.

1, 进程状态 ,将纪录进程在等待,运行,或死锁

2, 调度信息, 由哪个调度函数调度,怎样调度等

3, 进程的通讯状况

4,因为要插入进程树,必须有联系父子兄弟的指针, 当然是task_struct型

5,时间信息, 比如计算好执行的时间, 以便cpu 分配

6,标号 ,决定改进程归属

7,可以读写打开的一些文件信息

8, 进程上下文和内核上下文

9,处理器上下文

10,内存信息

因为每一个PCB都是这样的, 只有这些结构, 才能满足一个进程的所有要求.

打开/include/linux/sched.h 找到task_struct 的定义

struct task_struct {

/* these are hardcoded - don't touch */

这里是一些硬件设置对程序原来说是透明的. 其中state 说明了该进程是否可以执行,

还是可中断等信息. Flage 是进程号, 在调用 fork() 时给出,addr_limit 是区分内核进程

与普通进程在内存存放的位置不同

volatile long state; /* -1 unrunnable, 0 runnable, >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;

long need_resched;

 

/* various fields */

count 是 计数器 priorrity 是优先级

long counter;

long priority;

cycles_t avg_slice;

/* SMP and runqueue state */

为多处理机定义的变量.

int has_cpu;

int processor;

int last_processor;

int lock_depth;

/* Lock depth. We can context switch in and out of holding a syscall kernel lock... */

为了在进程树中排序, 定义的父子,兄弟指针

struct task_struct *next_task, *prev_task;

struct tas74k_struct *next_run, *prev_run;

 

/* task state */

定义可 task 运行的状态, 以及信号

struct linux_binfmt *binfmt;

int exit_code, exit_signal;

int pdeath_signal; /* The signal sent when the parent dies */

/* 定义可进程的用户号,用户组以及进程组*/

unsigned long personality;

int dumpable:1;

int did_exec:1;

pid_t pid;

pid_t pgrp;

pid_t tty_old_pgrp;

pid_t session;

/* boolean value for session group leader */

是不是进程组的头文件

int leader;

/*

* pointers to (original) parent process, youngest child, younger sibling,

* older sibling, respectively. (p->father can be replaced with

* p->p_pptr->pid)

*/

父子进程的一些指针

struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;

 

/* PID hash table linkage. */

在调度中用的一些hash 表