zoukankan      html  css  js  c++  java
  • chapt16、线程堆栈

    这里的堆栈指的就是栈 Stack

    线程堆栈默认大小是保留1M,初始提交2个页面,如8KB,1读写,1具有PAGE_GUARD保护属性

    堆栈扩展的时候,会因为保护属性触发异常,线程根据这个自动增长

    2000的堆栈
    在Win2000里,最后一个页面不会被用到,会被最终标记reserve,当提交到倒数第二个页面的时候,会触发EXCEPTION_STACK_ OVERFLOW,虽然这还不是最后一个page,但是这个时候就应该妥善处理这个问题了,否则容易发生严重的问题

    98的堆栈--A Thread's Stack Under Windows 98
    在Win98里,线程堆栈的前后是额外个增加了64KB的,即1MB+128KB,98没有PAGE_GUARD保护属性,使用PAGE_NOACCESS属性模拟
    98的堆栈比较特殊,有一段为兼容16位程序而模拟特殊的16KB读写段。最高的16KB地址用来做underflow保护,最低的16KB用来做overflow检测,它可用用全1MB的堆栈空间,而2000的最低一个页面则不会被使用

    线程堆栈检测函数--The C/C++ Run-Time Library's Stack-Checking Function
    在必要的时候,编译器会加入合适的代码来时堆栈进行合适的扩展
    如如下代码,
    void SomeFunction () {
       int nValues[4000];
    
       // Do some processing with the array.
       nValues[0] = 0;  // Some assignment
    }
    会被类似的扩展为
    // The C run-time library knows the page size for the target system.
    #ifdef _M_ALPHA
    #define PAGESIZE   (8 * 1024)   // 8-KB page
    #else
    #define PAGESIZE   (4 * 1024)   // 4-KB page
    #endif
    
    void StackCheck(int nBytesNeededFromStack) {
       // Get the stack pointer position.
       // At this point, the stack pointer has NOT been decremented
       // to account for the function's local variables.
       PBYTE pbStackPtr = (CPU's stack pointer);
    
       while (nBytesNeededFromStack >= PAGESIZE) {
          // Move down a page on the stack--should be a guard page.
          pbStackPtr -= PAGESIZE;
    
          // Access a byte on the guard page--forces new page to be
          // committed and guard page to move down a page.
          pbStackPtr[0] = 0;
    
          // Reduce the number of bytes needed from the stack.
          nBytesNeededFromStack -= PAGESIZE;
       }
    
       // Before returning, the StackCheck function sets the CPU's
       // stack pointer to the address below the function's
       // local variables.
    }


  • 相关阅读:
    【分享】管理的最高境界是简单
    建立市场化风险评估机制推进地方政府信用评级建设
    手游-神雕侠侣 85侠客纪攻略(已通关)
    使用git的分支功能实现定制功能摘取与组合的想法
    组内正则培训记录
    组内Linq培训记录
    一次代码重构记录
    git代码库误操作还原记录
    关于代码重构的开始
    如何管理高手、大牛?
  • 原文地址:https://www.cnblogs.com/xkxjy/p/3672260.html
Copyright © 2011-2022 走看看