Sobes.tech
Junior — Middle

How to determine the standard size of a process stack in a Linux system?

sobes.tech AI

Answer from AI

In Linux, the default process stack size can be checked using the command ulimit -s, which shows the stack size in kilobytes. You can also obtain this size in a C/C++ program through the system call getrlimit with the parameter RLIMIT_STACK.

Example in C:

#include <stdio.h>
#include <sys/resource.h>

int main() {
    struct rlimit rl;
    if (getrlimit(RLIMIT_STACK, &rl) == 0) {
        printf("Stack size: %ld bytes\n", rl.rlim_cur);
    } else {
        perror("getrlimit");
    }
    return 0;
}

The value rlim_cur is the current limit of the stack size in bytes.