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

Unix/Linux
Linux crontab定时执行任务 命令格式与详细例子
linux 查看用户及用户组的方法
让Linux系统有效防御ARP攻击的实用技巧
Linux 常用软件列表
linux wget 一个强大的下载命令
linux 常用脚本、命令
linux 磁盘配额 简单介绍
Linux服务器架设笔记 Squid服务器配置
ubuntu intel 集成显卡安装
ubuntu 9.04 X3100 显卡开启3D特效
Ubuntu 8.10 Server Ruby 的安装方法
Ubuntu root帐户密码修改
ubuntu下apt-get 命令参数
Ubuntu Linux下实现QQ的三种方式
Ubuntu 8.04中建立PHP+MySQL环境
Ubuntu常用软件大全
Ubuntu系统下安装Aircrack-ng
Ubuntu实现FTP功能
ubuntu 字体美化实现方法
ubuntu下netbeans汉字显示残缺问题

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 56 ::
收藏到网摘: 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 表