2010-03-01 59 views
3

在Linux上,據說在fork或exec之後,進程的rlimit保持不變。但是在fork之後或者exec之後,我在孩子中失去了我的RLIMIT_STACK。有人請給一些解釋嗎?

這是我的程序的一些描述性輸出。爲什麼在linux上執行fork或exec之後RLIMIT_STACK會丟失?

//,家長有這樣的

RLIMIT_STACK的RLIMIT_STACK,軟 - 10485760,硬 - -1

//叉緊接着,孩子叉後會失去其RLIMIT_STACK

在孩子,RLIMIT_STACK,軟 - -1,硬 - -1

//在孩子Exec之前,RLIMIT_STACK軟設置爲10485760再次

RLI MIT_STACK設置OK。

在孩子集後,RLIMIT_STACK,軟 - 10485760,硬 - -1 兒童的pid = 3096

//之後的exec,新進程失去了再次RLIMIT_STACK

RLIMIT_STACK了,軟 - - 1,硬 - -1

在此先感謝

+0

什麼版本和平臺? RLIMIT_STACK行爲在2.6.23和2.6.25中改變。 – 2010-03-01 03:03:47

+0

2.6.9-52bs x86_64 SMP – Utoah 2010-03-01 03:36:46

+0

該應用程序是在一個2.4.20-18.7smp i686平臺上以32位編譯的。它在上面發佈的平臺上運行。只有在這種情況下,問題纔會顯現。如果它在運行的64位平臺上編譯,它將在沒有此問題的情況下運行正常,如果在32位平臺上進行編譯,它也會運行正常。 – Utoah 2010-03-01 03:51:26

回答

1

這似乎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 
相關問題