Stack Size : behind the code

How to know the stack size of a process in Linux?
#Interview Question

The stack word is very popular when it comes to C programming. It often intrigues us that how much is the stack size of a process and how to know it.
The answer to this question can not be given without considering the "platform independent" thing. Well, we all are familiar with the word "platform independent". It is only word which comes to our mind when we don't know the answer. :P
So, the stack size depends on the platform, the architecture - microprocessor is of how many bits - 16, 32, 64? But I will explain the answer with respect to Linux operating system.

Method 1
The simplest method which I found is through
cat /proc/-pid-/limits
  • cat command is used to access a file.
  • proc is a directory which contains information about all the running processes.
  • -pid- is the process id of the process. We can know the process id of any process using ps -ef command. See man page of ps for more.
E.g:
I wrote a simple C program. And I put an infinite loop in that program(using while( )) so that it will be running. Then I used ps -ef to find out the -pid- of the process. I found it to be 3274. Then I did the following:
gene@user-laptop:~$ cat /proc/3274/limits
Limit                 Soft Limit     Hard Limit         Units  
Max cpu time          unlimited      unlimited          seconds
Max file size         unlimited      unlimited          bytes  
Max data size         unlimited      unlimited          bytes  
Max stack size        8388608        unlimited          bytes
Max core file size    0              unlimited          bytes  
Max resident set      unlimited      unlimited          bytes  
Max processes         32084          32084              processes
Max open files        1024           4096               files  
Max locked memory     65536          65536              bytes  
Max address space     unlimited      unlimited          bytes  
Max file locks        unlimited      unlimited          locks  
Max pending signals   32084          32084              signals
Max msgqueue size     819200         819200             bytes  
Max nice priority     0              0                 
Max realtime priority 0              0                 
Max realtime timeout  unlimited      unlimited          us 
Check out the line which is marked- Max stack size.
It is 8388608 bytes = 8 MB.

Method 2
There is also a different method to find out the stack size programmatically :
We can find out the stack size using getrlimit( ) system call.
Read about it :
gene@user-laptop:~ man getrlimit
Below is a program to find out the stack size of a process:
After running this program, we will get this output (Linux):
gene@user-laptop:~ gcc -ostacksize stacksize.c
gene@user-laptop:~ ./stacksize
Lower limit = 8388608
Upper limit = 4294967296
These results are in bytes. So, the stack size is 8388608 bytes = 8 MB for a 32 bit processor. The upper limit is the maximum limit upto which size of the stack can be incremented. It is 4294967296 bytes = 4 GB(size of virtual memory space assigned to a process). Though it is 4GB, incrementing it to this limit may overwrite certain other important data.
There is also a system call prlimit which is used to find out the stack size of any arbitrary process. I am leaving that for you to try. Tell me if you face any problem.
I have kept this explanation very compact because it can be found in man page of getrlimit. Still if anyone want more explanation here, just comment below.
-----
Feel free to give suggestions. :)

Comments

Popular posts from this blog

error: stray '\' in program

error: invalid application of ‘sizeof’ to incomplete type ‘struct ’

ssh: Could not resolve hostname git: No address associated with hostname