歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> linux進程描述符task_struct詳解

linux進程描述符task_struct詳解

日期:2017/3/1 17:22:04   编辑:關於Linux
大家都知道進程,可是知道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;



//表示此進程的運行優先級

unsigned int rt_priority;



//這裡出現了list_head結構體,詳情請參考

struct list_head tasks;



//這裡出現了mm_struct 結構體,該結構體記錄了進程內存使用的相關情況,詳情請參考

struct mm_struct *mm;



/* 接下來是進程的一些狀態參數*/
int exit_state;
int exit_code, exit_signal;


//這個是進程號

pid_t pid;



//這個是進程組號
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,詳情請參考
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的神秘面紗。
Copyright © Linux教程網 All Rights Reserved