为什么在 Linux 上 fork 或 exec 后 RLIMIT_STACK 会丢失?

发布于 2024-08-23 11:04:05 字数 553 浏览 4 评论 0原文

在 Linux 上,据说进程的 rlimit 在 fork 或 exec 之后保持不变。但是在 fork 或 exec 之后,我在子进程中丢失了 RLIMIT_STACK 。有人可以解释一下吗?

这是我的程序的一些描述性输出。

//父进程有一个像这样的RLIMIT_STACK

RLIMIT_STACK, soft - 10485760,hard - -1

//在fork之后,子进程立即失去它的RLIMIT_STACK

在fork之后的子进程中,RLIMIT_STACK, soft - -1,hard - -1

//在child,在exec之前,RLIMIT_STACK软设置为10485760,再次

RLIMIT_STACK设置OK。

在设置后的子级中,RLIMIT_STACK,软 - 10485760,硬 -1 Child pid = 3096

//exec后,新进程再次失去RLIMIT_STACK RLIMIT_STACK

得到,soft - -1,hard - -1

提前感谢

On linux, it is said that rlimit of a process is kept intact after either fork or exec. But I lose my RLIMIT_STACK in the child either after fork or after exec. Would someone please give some explain?

Here is some descriptive output of my program.

//The parent has an RLIMIT_STACK like this

RLIMIT_STACK, soft - 10485760, hard - -1

//Right after fork, the child loses its RLIMIT_STACK

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

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

RLIMIT_STACK set OK.

In child after set, RLIMIT_STACK, soft - 10485760, hard - -1
Child pid = 3096

//After exec, the new process loses its RLIMIT_STACK again

RLIMIT_STACK got, soft - -1, hard - -1

Thanks in advance
Feng

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-08-30 11:04:05

这似乎是 libpthread 的 linuxthread 实现的一个问题(我不确定这是否是一个错误)。
我写了一个简单的程序:

#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

This seems a problem(I am not sure if it is a bug) of linuxthread implementation of libpthread.
I wrote a simple program:

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

}

If this program is compiled and linked without -lpthread option, it runs OK everywhere. But when it is linked with -lpthread option, wired things happen: if it is run on a machine where it is dynamically linked to a linuxthread version of libpthread, it gives:

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

But when run on a machine where it is dynamically linked to a NPTL version of libpthread, it gives the expected result:

In parent, RLIMIT_STACK, soft-10485760, hard--1
In child after fork, RLIMIT_STACK, soft-10485760, hard--1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文