Linux核心task_struct獲取程序Task的名稱
阿新 • • 發佈:2019-02-03
在核心中,如果你已經獲取到相關Task(程序)的task_struct結構,你可以通過task_struct結構中的 char comm[TASK_COMM_LEN];成員可以獲取到程序的名稱。
//注意 TASK_COMM_LEN 的長度是16位元組
sprintf(buffer,"%s",pTaskStruct->comm);
附上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;
};