Why is RLIMIT_STACK lost after fork or exec on linux?

Linux says that the rlimit of a process is kept intact after fork or exec. But I lose my RLIMIT_STACK in the child, either after fork or after exec. Anyone please explain?

Here are some descriptive results of my program.

// The parent has an RLIMIT_STACK like this

RLIMIT_STACK, soft - 10485760, hard - -1

// Immediately after the fork, the child loses his RLIMIT_STACK

In the child after fork, RLIMIT_STACK, soft - 1, hard - 1

// In the child, before exec, RLIMIT_STACK soft is again set to 10485760

RLIMIT_STACK is set to OK.

In the child after typing, RLIMIT_STACK, soft - 10485760, hard - 1 Children's pid = 3096

// After exec, the new process loses RLIMIT_STACK again

RLIMIT_STACK received, soft - -1, hard - -1


+3
1

( , ) linuxthread libpthread.
:

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(int argc, char **argv){
struct rlimit resource_limit;
if(getrlimit(RLIMIT_STACK, &resource_limit) != 0){
    fprintf(stderr, "Failed to get rlimit: %s\n", strerror(errno));
    return 1;
}
else{
    fprintf(stderr, "In parent, RLIMIT_STACK, soft-%d, hard-%d\n",
            resource_limit.rlim_cur, resource_limit.rlim_max);
}

int child_status = 0;
pid_t pid = fork();
switch(pid){
    case 0://child
        if(getrlimit(RLIMIT_STACK, &resource_limit) != 0){
            fprintf(stderr, "Failed to get rlimit: %s\n", strerror(errno));
            return 1;
        }
        else{
            fprintf(stderr, "In child after fork, RLIMIT_STACK, soft-%d, hard-%d\n",
                    resource_limit.rlim_cur, resource_limit.rlim_max);
        }
        break;
    case -1:
        fprintf(stderr, "Fork error: %s\n", strerror(errno));
        break;
    default://parent
        waitpid(pid, &child_status, 0);
        break;
}
return 0;

}


-lpthread, . -lpthread, : , libpthread linuxthread, :

In parent, RLIMIT_STACK, soft-10485760, hard--1
In child after fork, RLIMIT_STACK, soft--1, hard--1

, NPTL- libpthread, :

In parent, RLIMIT_STACK, soft-10485760, hard--1
In child after fork, RLIMIT_STACK, soft-10485760, hard--1
+1

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


All Articles