這似乎linuxthread實現的libpthread的問題(我不知道這是否是一個bug)。
我寫了一個簡單的程序:
#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
但是,當一臺機器,其中上運行它動態鏈接到libpthread的NPTL版本,它給出了預期的結果:
In parent, RLIMIT_STACK, soft-10485760, hard--1
In child after fork, RLIMIT_STACK, soft-10485760, hard--1
什麼版本和平臺? RLIMIT_STACK行爲在2.6.23和2.6.25中改變。 – 2010-03-01 03:03:47
2.6.9-52bs x86_64 SMP – Utoah 2010-03-01 03:36:46
該應用程序是在一個2.4.20-18.7smp i686平臺上以32位編譯的。它在上面發佈的平臺上運行。只有在這種情況下,問題纔會顯現。如果它在運行的64位平臺上編譯,它將在沒有此問題的情況下運行正常,如果在32位平臺上進行編譯,它也會運行正常。 – Utoah 2010-03-01 03:51:26