不是一个缺页中断的例子,是栈撑爆的例子【原创】

 经高人也是我的好朋友的指点后,发现此文并不是缺页中断的例子,用户空间这样写实际上是栈溢出的例子,一个页为4k,一个栈为8M,栈撑爆了就会segment fault

缺页中断在用户空间产生的话,只需要fork进程即可,创建新的进程就会产生缺页中断,因为会建立新的页表。

test1.c

#include <stdio.h>
#include <stdlib.h>

#define UNUSED_PARAMETER(x)  ((void)(x))

int main(int argc, char *argv)
{
    int i, j;
    char buf[4096][4096];
    
    UNUSED_PARAMETER(argc);
    UNUSED_PARAMETER(argv);
    
    for (i=0; i<4096; i++)
        for (j=0; j<4096; j++)
            buf[j][i] = 1;
        
    return 0;
}

test2.c

#include <stdio.h>
#include <stdlib.h>

#define UNUSED_PARAMETER(x)  ((void)(x))

int main(int argc, char *argv)
{
    int i, j;
    char buf[4096][4096];
    
    UNUSED_PARAMETER(argc);
    UNUSED_PARAMETER(argv);
    
    for (i=0; i<4096; i++)
        for (j=0; j<4096; j++)
            buf[i][j] = 1;
        
    return 0;
}

 

 

 

欢迎交流,如有转载请注明出处

新浪博客:http://blog.sina.com.cn/u/2049150530
博客园:http://www.cnblogs.com/sky-heaven/
知乎:http://www.zhihu.com/people/zhang-bing-hua

posted @ 2016-09-18 23:28  Sky&Zhang  阅读(1082)  评论(3编辑  收藏  举报