What is the maximum size of stack segment that can be allocated to a process?

#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;
}

Output

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

Just before the code crashed the total memory allocated was 9520k . Everytime I run it I get different values, all of them in the range of 9000k - 10000k So I want to know what is the maximum size of stack segment that can be allocated to a process in Linux.

Another thing I noticed was

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

so each time the virtual address of the stack segment changes. Is it so that every time a new memory is allocated and the content from the old memory are copy-pasted to the new memory location.


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

链接地址: http://www.djcxy.com/p/80278.html

上一篇: C程序的堆栈会缩小吗?

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