Linux進程管理描述符 task_struct,linuxtask_struct

來源:互聯網
上載者:User

Linux進程管理描述符 task_struct,linuxtask_struct

轉:http://blog.csdn.net/hongchangfirst/article/details/7075026

 

 

大家都知道進程,可是知道linux是怎麼管理其進程的嗎?每一個進程都有一個進程描述符,具體是task_struct結構體儲存相關的資訊,在linux/sched.h檔案裡定義,那麼我們先看看linux核心3.0版本的task_struct結構體的定義吧(刪除了不必要的欄位,只保留了重要的欄位)。
 1 struct task_struct { 2 //這個是進程的運行時狀態,-1代表不可運行,0代表可運行,>0代表已停止。 3  volatile long state; 4  /* 5 flags是進程當前的狀態標誌,具體的如: 6 0x00000002表示進程正在被建立; 7 0x00000004表示進程正準備退出; 8 0x00000040 表示此進程被fork出,但是並沒有執行exec; 9 0x00000400表示此進程由於其他進程發送相關訊號而被殺死 。10 */11  unsigned int flags; 12 //表示此進程的運行優先順序,prio表示動態優先順序,根據static_prio和互動性獎罰算出,static_prio是進程的靜態優先順序,在進程建立時確定,範圍從-20到19,越小優先順序越高。13 int prio, static_prio, normal_prio;14 //這裡出現了list_head結構體,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/707622515  struct list_head tasks;16 //這裡出現了mm_struct 結構體,該結構體記錄了進程記憶體使用量的相關情況,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/707620717  struct mm_struct *mm;18 /* 接下來是進程的一些狀態參數*/19  int exit_state;20  int exit_code, exit_signal;21 //這個是進程號,注意這是核心自己維護的進程號,因為在Linux中線程是由進程實現的,使用者看到的進程號是tgid域。22  pid_t pid;23 //這個是線程組號,和線程組內的領頭進程的進程號一致,我們在使用者程式中調用getpid()其實返回的是tgid值。24  pid_t tgid;25 //real_parent是該進程的”親生父親“,不管其是否被“寄養”。26  struct task_struct *real_parent;27 //parent是該進程現在的父進程,有可能是”繼父“28  struct task_struct *parent; 29 //這裡children指的是該進程孩子的鏈表,可以得到所有孩子的進程描述符,但是需使用list_for_each和list_entry,list_entry其實直接使用了container_of,詳情請參考http://blog.csdn.net/hongchangfirst/article/details/707622530  struct list_head children; 31 //同理,sibling該進程兄弟的鏈表,也就是其父親的所有孩子的鏈表。用法與children相似。32  struct list_head sibling; 33 //這個是主線程的進程描述符,也許你會奇怪,為什麼線程用進程描述符表示,因為linux並沒有單獨實現線程的相關結構體,只是用一個進程來代替線程,然後對其做一些特殊的處理。34  struct task_struct *group_leader; 35 //這個是該進程所有線程的鏈表。36  struct list_head thread_group;37 //顧名思義,這個是該進程使用cpu時間的資訊,utime是在使用者態下執行的時間,stime是在核心態下執行的時間。38  cputime_t utime, stime;39 //下面的是啟動的時間,只是時間基準不一樣。40  struct timespec start_time;   41  struct timespec real_start_time;42 //comm是儲存該進程名字的字元數組,長度最長為15,因為TASK_COMM_LEN為16。43  char comm[TASK_COMM_LEN];44 /* 檔案系統資訊計數*/45  int link_count, total_link_count;46 /*該進程在特定CPU下的狀態*/47  struct thread_struct thread;48 /* 檔案系統相關資訊結構體*/49  struct fs_struct *fs;50 /* 開啟的檔案相關資訊結構體 */51  struct files_struct *files;52  /* 訊號相關資訊的控制代碼*/53  struct signal_struct *signal;54  struct sighand_struct *sighand;55  /*這些是鬆弛時間值,用來規定select()和poll()的逾時時間,單位是納秒nanoseconds  */56  unsigned long timer_slack_ns;57  unsigned long default_timer_slack_ns;58 };

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.