可以分配给进程的堆栈段的最大大小是多少?

时间:2011-02-18 09:24:35

标签: stack segmentation-fault dynamic-memory-allocation segment

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

void start(){

  double xyz[135168];
  char str[20];

  xyz[0] = 1.2;

  sprintf(str,"pmap %d",getpid());
  system(str);

  start();
}

int main(){

  char str[20];

  sprintf(str,"pmap %d",getpid());
  system(str);

  printf("entering start()\n");
  start();    

  return 0;
}

输出

anirudh@anirudh-Aspire-5920:~/Desktop/testing$ ./a.out 
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bfe1c000    132K rw---    [ stack ]
 total     1676K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bfd32000   1068K rw---    [ stack ]
 total     2612K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bfc2a000   2124K rw---    [ stack ]
 total     3668K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bfb22000   3180K rw---    [ stack ]
 total     4724K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bfa1a000   4236K rw---    [ stack ]
 total     5780K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bf911000   5296K rw---    [ stack ]
 total     6840K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bf809000   6352K rw---    [ stack ]
 total     7896K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bf701000   7408K rw---    [ stack ]
 total     8952K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bf5f9000   8464K rw---    [ stack ]
 total    10008K
3695:   ./a.out
00110000   1372K r-x--  /lib/libc-2.12.1.so
00267000      8K r----  /lib/libc-2.12.1.so
00269000      4K rw---  /lib/libc-2.12.1.so
0026a000     12K rw---    [ anon ]
00b64000      4K r-x--    [ anon ]
00c27000    112K r-x--  /lib/ld-2.12.1.so
00c43000      4K r----  /lib/ld-2.12.1.so
00c44000      4K rw---  /lib/ld-2.12.1.so
08048000      4K r-x--  /home/anirudh/Desktop/testing/a.out
08049000      4K r----  /home/anirudh/Desktop/testing/a.out
0804a000      4K rw---  /home/anirudh/Desktop/testing/a.out
b78eb000      4K rw---    [ anon ]
b78fe000      8K rw---    [ anon ]
bf4f1000   9520K rw---    [ stack ]
 total    11064K
Segmentation fault

在代码崩溃之前,分配的总内存为9520k。 我每次运行它都会获得不同的值,所有这些值都在9000k - 10000k的范围内 所以我想知道可以在Linux中分配给进程的堆栈段的最大大小是什么。

我注意到的另一件事是

 bfe1c000    132K rw---    [ stack ]
 .
 bf911000   5296K rw---    [ stack ]
 .
 bf4f1000   9520K rw---    [ stack ]

所以每次堆栈段的虚拟地址都会改变。是这样的,每次分配新内存并将旧内存中的内容复制粘贴到新内存位置时。

1 个答案:

答案 0 :(得分:2)

ulimit -s应该显示最大堆栈大小。