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