Stack size from proc / pid / maps to C

I saw the stack size from / proc / pid / maps for program C in linux-64 bits. I could see the following line regarding the size of the stack.

7fffc2e14000-7fffc2e35000 rw-p 00000000 00:00 0 [stack] 

I do not understand the above values. If I understand, I get the difference 135168 in decimal. But it is not close to 8 MB. Am I interpreting this wrong?

But if I type rlimit:

 int main (void) { struct rlimit limit; getrlimit (RLIMIT_STACK, &limit); printf ("\nStack Limit = %ld and %ld max\n", limit.rlim_cur, limit.rlim_max); } 

Output: Stack Limit = 8388608 and -1 max

I get Stack Limit = 8388608, which reaches 8 MB.

+4
source share
3 answers

rlim_cur is the soft limit of what the stack can do for this process. This is not an indication of the current stack usage. On the man page:

Each resource has a soft and hard limit associated with it, as defined by the rlimit structure:

 struct rlimit { rlim_t rlim_cur; /* Soft limit */ rlim_t rlim_max; /* Hard limit (ceiling for rlim_cur) */ }; 

The soft limit is the value that the kernel provides for the corresponding resource. A hard limit acts like a ceiling for a soft limit: an unprivileged process can only set its soft limit to a value in the range from 0 to the hard limit and (irreversibly) hard limit.

/proc/[pid]/maps , on the other hand, shows you what is currently displayed for this process. I see 0x21000, which is most likely (33) 4k pages. This is probably the best indicator of using the current stack for this process. However, it probably includes things other than the stack, such as environment variables.

+4
source

Linux does not commit the entire rlimit size for the stack when the process starts. It simply reserves this a lot of virtual address space (so other mmaps, etc. cannot block stack growth) and allows you to dynamically push the stack to the limit. However, the default stack size is set by default, which seems to be 128k or 132k depending on the system, and I don't know how to configure / configure it.

+2
source

RLIMIT_STACK indicates the maximum limit to which your stack can grow. Your stack is 132K right now. If you used more automatic variables, the stack size you saw in /proc/pid/maps would be larger.

0
source

Source: https://habr.com/ru/post/1403197/


All Articles