大家都知道進程,可是知道linux是怎麼管理其進程的嗎?每一個進程都有一個進程描述符,具體是task_struct結構體存儲相關的信息,在linux/sched.h文件裏定義,那麼我們先看看linux內核3.0版本的task_struct結構體的定義吧(刪除了不必要的字段,只保留了重要的字段)。同時歡迎大家轉載和交流。
struct task_struct {
//這個是進程的運行時狀態,-1代表不可運行,0代表可運行,>0代表已停止。
volatile long state;
/*
flags是進程當前的狀態標誌,具體的如:
0x00000002表示進程正在被創建;
0x00000004表示進程正準備退出;
0x00000040 表示此進程被fork出,但是並沒有執行exec;
0x00000400表示此進程由於其他進程發送相關信號而被殺死 。
*/
unsigned int flags;
//表示此進程的運行優先級,prio表示動態優先級,根據static_prio和交互性獎罰算出,static_prio是進程的靜態優先級,在進程創建時確定,範圍從-20到19,越小優先級越高。
int prio, static_prio, normal_prio;
//這裏出現了list_head結構體,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/7076225
struct list_head tasks;
//這裏出現了mm_struct 結構體,該結構體記錄了進程內存使用的相關情況,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/7076207
struct mm_struct *mm;
/* 接下來是進程的一些狀態參數*/
int exit_state;
int exit_code, exit_signal;
//這個是進程號,注意這是內核自己維護的進程號,因爲在Linux中線程是由進程實現的,用戶看到的進程號是tgid域。
pid_t pid;
//這個是線程組號,和線程組內的領頭進程的進程號一致,我們在用戶程序中調用getpid()其實返回的是tgid值。
pid_t tgid;
//real_parent是該進程的”親生父親“,不管其是否被“寄養”。
struct task_struct *real_parent;
//parent是該進程現在的父進程,有可能是”繼父“
struct task_struct *parent;
//這裏children指的是該進程孩子的鏈表,可以得到所有孩子的進程描述符,但是需使用list_for_each和list_entry,list_entry其實直接使用了container_of,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/7076225
struct list_head children;
//同理,sibling該進程兄弟的鏈表,也就是其父親的所有孩子的鏈表。用法與children相似。
struct list_head sibling;
//這個是主線程的進程描述符,也許你會奇怪,爲什麼線程用進程描述符表示,因爲linux並沒有單獨實現線程的相關結構體,只是用一個進程來代替線程,然後對其做一些特殊的處理。
struct task_struct *group_leader;
//這個是該進程所有線程的鏈表。
struct list_head thread_group;
//顧名思義,這個是該進程使用cpu時間的信息,utime是在用戶態下執行的時間,stime是在內核態下執行的時間。
cputime_t utime, stime;
//下面的是啓動的時間,只是時間基準不一樣。
struct timespec start_time;
struct timespec real_start_time;
//comm是保存該進程名字的字符數組,長度最長爲15,因爲TASK_COMM_LEN爲16。
char comm[TASK_COMM_LEN];
/* 文件系統信息計數*/
int link_count, total_link_count;
/*該進程在特定CPU下的狀態*/
struct thread_struct thread;
/* 文件系統相關信息結構體*/
struct fs_struct *fs;
/* 打開的文件相關信息結構體 */
struct files_struct *files;
/* 信號相關信息的句柄*/
struct signal_struct *signal;
struct sighand_struct *sighand;
/*這些是鬆弛時間值,用來規定select()和poll()的超時時間,單位是納秒nanoseconds */
unsigned long timer_slack_ns;
unsigned long default_timer_slack_ns;
};
到這裏,我們已經看到了很多的字段,但這些字段只是task_struct的冰山一角,希望更多的人可以爲我們揭開task_struct的神祕面紗。至於內存描述符可參考http://blog.csdn.net/hongchangfirst/article/details/7076207。
轉載請註明出處:
原文:http://blog.csdn.net/hongchangfirst/article/details/7075026
作者:hongchangfirst