標籤:
1.task_struct資料結構分析
對於linux而言,每個進程都有一個進程式控制制PCB(process control block)來儲存每個進程的相關資訊。其中task_struct則是PCB的具體的資料結構通過核心代碼可以發現,核心當中定義一個task_struct的結構體用來儲存進程的相關資訊。這裡先來分析下task_struct的結構體。task_struct的代碼如下:
http://codelab.shiyanlou.com/xref/linux-3.18.6/include/linux/sched.h#1235
1 #define TASK_RUNNING 0 2 #define TASK_INTERRUPTIBLE 1 3 #define TASK_UNINTERRUPTIBLE 2 4 #define __TASK_STOPPED 4 5 #define __TASK_TRACED 8 6 /* in tsk->exit_state */ 7 #define EXIT_DEAD 16 8 #define EXIT_ZOMBIE 32 9 #define EXIT_TRACE (EXIT_ZOMBIE | EXIT_DEAD)10 /* in tsk->state again */11 #define TASK_DEAD 6412 #define TASK_WAKEKILL 12813 #define TASK_WAKING 25614 #define TASK_PARKED 51215 #define TASK_STATE_MAX 1024
這裡可以看到定義了進程的相關運行狀態,這裡特殊的地方在於TASK_RUNNING這裡將運行態和就緒態的進程都用TASK_RUNNING表示。
進程狀態之間切換如下
1 pid_t pid;2 pid_t tgid;
pid以及tpid的區別如下
http://blog.chinaunix.net/uid-26849197-id-3201487.html
簡單說來,就是對於同一進程的不同線程而言pid不同,但是tgid相同
1 struct list {2 struct list *next, *prev;3 };
這裡顯示的核心進程鏈表的實現,核心通過這個鏈表實現進程間調度等等的功能。
1 struct task_struct __rcu *real_parent; /* real parent process */2 struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */3 /*4 * children/sibling forms the list of my natural children5 */6 struct list_head children; /* list of my children */7 struct list_head sibling; /* linkage in my parent‘s children list */8 struct task_struct *group_leader; /* threadgroup leader */
以上是進程之間父子關係的代碼,通過這些代碼,可以用來訪問父進程以及子進程,這樣在fork新進程時可以用來複製相關的資料。
1 union thread_union {2 struct thread_info thread_info;3 unsigned long stack[THREAD_SIZE/sizeof(long)];4 };
每個進程都有8kb的記憶體用來存放 thread_info以及進程的核心堆棧
1 * CPU-specific state of this task */ 2 struct thread_struct thread; 3 /* filesystem information */ 4 struct fs_struct *fs; 5 /* open file information */ 6 struct files_struct *files; 7 /* namespaces */ 8 struct nsproxy *nsproxy; 9 /* signal handlers */10 struct signal_struct *signal;11 struct sighand_struct *sighand;
這段這是表示CPU的工作狀態,以及檔案系統和檔案描述符的管理
1 struct mm_struct *mm, *active_mm;
這裡則是進程的記憶體管理
第六周 linux核心進程的建立