博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux内核 2.4和2.6的进程内核
阅读量:4125 次
发布时间:2019-05-25

本文共 1555 字,大约阅读时间需要 5 分钟。

原文链接:

Linux内核 2.42.6的进程内核堆栈和task描述符存储不太一样,这儿总结一下。

在内核2.4
中堆栈是这么定义的:
union task_union {


        struct task_struct task;
        unsigned long stack[INIT_TASK_SIZE/sizeof(long)];
    };
INIT_TASK_SIZE只能是8K

内核为每个进程分配一个task_struct结构时,实际上分配两个连续的物理页面(8192),如图所示。底部用作task_struct结构(大小约为1K字节),结构的上面用作内核堆栈(大小约为7K字节)访问进程自身的task_struct结构,使用宏操作current, 2.4中定义如下:

#define current get_current()
static inline struct task_struct * get_current(void)
{

      struct task_struct *current;
      __asm__("andl %%esp,%0; ":"=r" (current) : "" (~8191UL));
      return current;
}

  ~8191UL
表示最低13位为0, 其余位全为1 %esp指向内核堆栈中,当屏蔽掉%esp的最低13后,就得到这个两个连续的物理页面的开头,而这个开头正好是task_struct的开始,从而得到了指向task_struct的指针。


在内核2.6中堆栈这么定义:
union thread_union {

      struct thread_info thread_info;
      unsigned long stack[THREAD_SIZE/sizeof(long)];
};

根据内核的配置,THREAD_SIZE既可以是4K字节(1个页面)也可以是8K字节(2个页面)thread_info52个字节长。

下图是当设为
8KB时候的内核堆栈:Thread_info在这个内存区的开始处,内核堆栈从末端向下增长。进程描述符不是在这个内存区中,而分别通过taskthread_info指针使thread_info与进程描述符互联。所以获得当前进程描述符的current定义如下:

#define current get_current()
static inline struct task_struct * get_current(void)
{

      return current_thread_info()->task;
}
static inline struct thread_info *current_thread_info(void)
{

       struct thread_info *ti;
       __asm__("andl %%esp,%0; ":"=r" (ti) : "" (~(THREAD_SIZE - 1)));
       return ti;
}
    
根据THREAD_SIZE大小,分别屏蔽掉内核栈的12-bit LSB(4K)13-bit LSB(8K),从而获得内核栈的起始位置。

struct thread_info {

      struct task_struct    *task;       /* main task structure */
      struct exec_domain    *exec_domain; /* execution domain */
      unsigned long           flags;       /* low level flags */
      unsigned long           status;       /* thread-synchronous flags */
      ... ..
}

转载地址:http://bwmpi.baihongyu.com/

你可能感兴趣的文章
对话周鸿袆:从程序员创业谈起
查看>>
web.py 0.3 新手指南 - 如何用Gmail发送邮件
查看>>
web.py 0.3 新手指南 - RESTful doctesting using app.request
查看>>
web.py 0.3 新手指南 - 使用db.query进行高级数据库查询
查看>>
web.py 0.3 新手指南 - 多数据库使用
查看>>
一步步开发 Spring MVC 应用
查看>>
python: extend (扩展) 与 append (追加) 的差别
查看>>
「译」在 python 中,如果 x 是 list,为什么 x += "ha" 可以运行,而 x = x + "ha" 却抛出异常呢?...
查看>>
浅谈JavaScript的语言特性
查看>>
LeetCode第39题思悟——组合总和(combination-sum)
查看>>
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>
驱动力3.0,动力全开~
查看>>
记CSDN访问量10万+
查看>>
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>