进程内核栈
進程內核棧
為什么有進程內核棧
? 進程在創建的時候也可以理解為一個程序,或者在簡單的理解也可以把進程理解為一個函數,只不過這個函數很大而已,這個進程也需要有一些函數調用,也需要有一些函數去標記一些信息,于是 便有了進程內核棧這個東西。
? 簡單理解,進程內核棧實際上就是為進程開辟一個棧幀空間。
? 但是這個棧幀空間不是用戶的棧幀空間,因為用戶的棧幀空間時不安全的,所以內核會專門為它開辟一個空間,這個就是內核棧。
進程內核棧和進程描述符的關系
- 進程描述符
? 進程描述符就是進程結構體(struct task_struct),每一個進程在創建之后都會有一個進程結構體,用來記錄進程的所有信息。這其中的有一個信息就是就是進程內核棧,用一個指針指示。void *stack;就是指向下面的內核棧結構體的“棧底”。
- 內核棧結構體
?
union thread_union {struct thread_info thread_info;unsigned long stack[THREAD_SIZE/sizeof(long)];};
其中的stack成員就是內核棧。
而其中的struct thread_info是記錄部分進程信息的結構體,其中包括了進程上下文信息。
從這里可以看出內核??臻g和 thread_info是共用一塊空間的。如果內核棧溢出, thread_info就會被摧毀,系統崩潰了~~~
讓我們詳細的看一下內核棧結構體。
/** low level task data that entry.S needs immediate access to.* __switch_to() assumes cpu_context follows immediately after cpu_domain.*/struct thread_info {unsigned long flags; /* low level flags */int preempt_count; /* 0 => preemptable, <0 => bug */mm_segment_t addr_limit; /* address limit */struct task_struct *task; /* main task structure */struct exec_domain *exec_domain; /* execution domain */__u32 cpu; /* cpu */__u32 cpu_domain; /* cpu domain */struct cpu_context_save cpu_context; /* cpu context */__u32 syscall; /* syscall number */__u8 used_cp[16]; /* thread used copro */unsigned long tp_value;struct crunch_state crunchstate;union fp_state fpstate __attribute__((aligned(8)));union vfp_state vfpstate;#ifdef CONFIG_ARM_THUMBEEunsigned long thumbee_state; /* ThumbEE Handler Base register */#endifstruct restart_block restart_block;};
關鍵是其中的task成員,指向的是所創建的進程的struct task_struct結構體。就是進程描述符。
圖文表示三者關系
內核?!獌群藯=Y構體(struct thread_info)—-進程描述符(struct task_struct)三者的關系入下圖
內核棧的產生
在進程被創建的時候,fork族的系統調用中會分別為內核棧和struct task_struct分配空間,調用過程是:
fork族的系統調用—>do_fork—>copy_process—>dup_task_struct
總結
- 上一篇: 琵琶多少钱啊?
- 下一篇: Linux进程描述符task_struc