linux task_struct栈结构说明

在这里插入图片描述
struct thread_info {
struct task_struct *task;
struct exec_domain *exec_domain;
__u32 flags;
__u32 cpu;
mm_segment_t addr_limit;
__s32 preempt_count;
struct restart_block restart_block;
};

static inline struct thread_info *current_thread_info(void)
{
register unsigned long sp asm (“sp”);
return (struct thread_info *)(sp & ~THREAD_SIZE);
}
static inline struct task_struct * get_current(void)
{
return current_thread_info()->task;
}
#define current get_current()

找到current是通过thread_info结构,那么如何找到thread_info结构呢sp & ~THREAD_SIZE

这么看就比较清楚了 首先分配task_struct的时候申请THREAD_SIZE对应的页面数,所以task_struct的基地址的THREAD_SIZE-1 地址都为0,这样就可以根据sp(栈指针寄存器) 找到对应的task_struct结构。

最后再来解释下task_struct的数据结构
task_struct的第一个数据结构是state表示进程的状态。
第二个数据结构是thread_info指针 数据结构,

struct thread_info { 
     struct task_struct *task; 
       struct exec_domain *exec_domain;
         __u32 flags; 
     __u32 cpu; 
     mm_segment_t addr_limit; 
    __s32 preempt_count;   
       struct restart_block restart_block; 
 };

剩下的数据结构一般都是描述给这个进程分配的资源。

当创建task_truct的时候会分配THREAD_SIZE个字节的内存,一般为4k或者8k,task_struct肯定是使用不了的,剩下的部分就用来存放thread_info结构,这个结构描述了task_struct的线程上下文,并且剩下的部分用于该进程的内核栈空间

栈底为图中bash+THREAD_SIZE处,栈顶最小到thread_info数据的最底部。
kthread_end判断是否栈底

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值