在linux 中每一個(gè)進(jìn)程都由task_struct 數(shù)據(jù)結(jié)構(gòu)來(lái)定義. task_struct就是我們通常所說(shuō)的PCB.
她是對(duì)進(jìn)程控制的唯一手段也是最有效的手段.
當(dāng)我們調(diào)用fork() 時(shí), 系統(tǒng)會(huì)為我們產(chǎn)生一個(gè)task_struct結(jié)構(gòu).然后從父進(jìn)程,那里繼承一些數(shù)據(jù),
并把新的進(jìn)程插入到進(jìn)程樹(shù)中, 以待進(jìn)行進(jìn)程管理.因此了解task_struct的結(jié)構(gòu)對(duì)于我們理解任務(wù)
調(diào)度(在linux 中任務(wù)和進(jìn)程是同一概念)的關(guān)鍵.在進(jìn)行剖析task_struct的定義之前. 我們先按照
我們的理論推一下它的結(jié)構(gòu).
1, 進(jìn)程狀態(tài) ,將紀(jì)錄進(jìn)程在等待,運(yùn)行,或死鎖
2, 調(diào)度信息, 由哪個(gè)調(diào)度函數(shù)調(diào)度,怎樣調(diào)度等
3, 進(jìn)程的通訊狀況
4,因?yàn)橐迦脒M(jìn)程樹(shù),必須有聯(lián)系父子兄弟的指針, 當(dāng)然是task_struct型
5,時(shí)間信息, 比如計(jì)算好執(zhí)行的時(shí)間, 以便cpu 分配
6,標(biāo)號(hào) ,決定改進(jìn)程歸屬
7,可以讀寫打開(kāi)的一些文件信息
8, 進(jìn)程上下文和內(nèi)核上下文
9,處理器上下文
10,內(nèi)存信息
因?yàn)槊恳粋€(gè)PCB都是這樣的, 只有這些結(jié)構(gòu), 才能滿足一個(gè)進(jìn)程的所有要求.
打開(kāi)/include/linux/sched.h 找到task_struct 的定義
struct task_struct {
/* these are hardcoded - don't touch */
這里是一些硬件設(shè)置對(duì)程序原來(lái)說(shuō)是透明的. 其中state 說(shuō)明了該進(jìn)程是否可以執(zhí)行,
還是可中斷等信息. Flage 是進(jìn)程號(hào), 在調(diào)用 fork() 時(shí)給出,addr_limit 是區(qū)分內(nèi)核進(jìn)程
與普通進(jìn)程在內(nèi)存存放的位置不同
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 是 計(jì)數(shù)器 priorrity 是優(yōu)先級(jí)
long counter;
long priority;
cycles_t avg_slice;
/* SMP and runqueue state */
為多處理機(jī)定義的變量.
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... */
為了在進(jìn)程樹(shù)中排序, 定義的父子,兄弟指針
struct task_struct *next_task, *prev_task;
struct tas74k_struct *next_run, *prev_run;
/* task state */
定義可 task 運(yùn)行的狀態(tài), 以及信號(hào)
struct linux_binfmt *binfmt;
int exit_code, exit_signal;
int pdeath_signal; /* The signal sent when the parent dies */
/* 定義可進(jìn)程的用戶號(hào),用戶組以及進(jìn)程組*/
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 */
是不是進(jìn)程組的頭文件
int leader;
/*
* pointers to (original) parent process, youngest child, younger sibling,
* older sibling, respectively. (p->father can be replaced with
* p->p_PPTr->pid)
*/
父子進(jìn)程的一些指針
struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
/* PID hash table linkage. */
在調(diào)度中用的一些hash 表
struct task_struct *pidhash_next;
struct task_struct **pidhash_pprev;
/* Pointer to task[] array linkage. */
struct task_struct **tarray_ptr;
struct wait_queue *wait_chldexit; /* for wait4() 等待隊(duì)列 */
struct semaphore *vfork_sem; /* for vfork() */
unsigned long policy, rt_priority;
unsigned long it_real_value, it_prof_value, it_virt_value;
進(jìn)程的性質(zhì)因?yàn)閷?shí)時(shí)進(jìn)程與普通進(jìn)程的調(diào)度算法不一樣所以應(yīng)有變量區(qū)分
下面是進(jìn)程的一些時(shí)間信息
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];定義了時(shí)間片的大小
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
內(nèi)存信息
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;
struct user_struct *user;
以下英文注釋很清楚
/* limits */
struct rlimit rlim[RLIM_NLIMITS];
unsigned short used_math;
char comm[16];
/* file system info */
int link_count;
struct tty_struct *tty; /* NULL if no tty */
/* ipc stuff */
struct sem_undo *semundo;
struct sem_queue *semsleeping;
/* tss for this task */
struct thread_struct tss;
/* filesystem information */
struct fs_struct *fs;
/* open file information */
struct files_struct *files;
/* memory management info */
struct mm_struct *mm;
/* signal handlers */
spinlock_t sigmask_lock; /* Protects signal and blocked */
struct signal_struct *sig;
sigset_t signal, blocked;
struct signal_queue *sigqueue, **sigqueue_tail;
unsigned long sas_ss_sp;
size_t sas_ss_size;
};
在分析完 這個(gè)結(jié)構(gòu)之后, 還有很多問(wèn)題要想, 也許不能讀 但框架要搞好.需要向的問(wèn)題有以下幾個(gè)
1,在task_struct 中用的常量在那里定義呢, 如最大進(jìn)程個(gè)數(shù), 最多支持的cpu 個(gè)數(shù),等等
2,在調(diào)用fork() 時(shí), 系統(tǒng)是分配一塊內(nèi)存 會(huì)是這樣么
malloc(1,sizeof(struct task_struck))
拷貝一些變量,還是和服進(jìn)程公用一部分內(nèi)存.malloc 函數(shù)怎么實(shí)現(xiàn)(在內(nèi)存管理那一部分,但此處我認(rèn)為不能不想)
3,.對(duì)于線程來(lái)說(shuō), 又如何實(shí)現(xiàn)呢?
4, 調(diào)度策略函數(shù) schedul()
有幾種形勢(shì), 時(shí)間片輪轉(zhuǎn), 搶占式,優(yōu)先級(jí)搶占式, 多級(jí)反饋制.除了時(shí)間片輪轉(zhuǎn)外都要對(duì)進(jìn)程樹(shù)進(jìn)行遍歷,(對(duì)于實(shí)時(shí)進(jìn)程
的fifo機(jī)制不用)linux 是怎樣保證了高效呢?如果把最大線成數(shù)修改, 效率會(huì)不會(huì)降低
5, 進(jìn)程通訊用到的管道,信號(hào)結(jié)構(gòu)如何
待續(xù)..
新聞熱點(diǎn)
疑難解答
圖片精選