2013-07-29 50 views

回答

3

你可以看到的最大允許打開文件(內核的限制)這樣做:

cat /proc/sys/fs/file-max 

Quote from kernel docs:

文件-MAX的值表示的文件 - 句柄的最大數量的Linux內核將分配。當您收到關於用完文件句柄的錯誤消息 的許多問題時,您可能想要增加此限制 。

+0

顯示1201326但開放的fopen限制僅1022 – naren

+3

這可能是用戶限制;)見'的ulimit -n' – nullpotent

+3

FOPEN_MAX不是限制。它給你實現保證可以同時打開的最小數量的流* – jh314

11

執行需要提供FOPEN_MAX<stdio.h>。這是實現保證可以同時打開的最小文件數量。您可能能夠打開更多,但要知道這是測試的唯一途徑。

請注意,內核限制與此不同 - 它告訴您可以(可能)用open,creat和其他OS調用打開多少個文件。 C實現的標準庫可以(並且通常會)強加它自己的限制(例如,通過靜態分配一個FILE的數組)。從理論上講,你可以打開的最大數量是內核和庫實現限制的最小值 - 但內核的限制總是(更高)幾乎

一般來說,如果你在乎這個,你可能是或者做錯了。

1

它由POSIX標準定義。刪除它會導致可移植性問題。此外,該宏在glibc.info中提到(至少在redhat-7.1中)。請參考下面的鏈接 OPEN_MAX not defined in limits.h

0

此代碼應該告訴您的機器上的最大值。在同一個文件夾中創建一個文件「test」並運行它。它基本上保持打開文件,直到它不能再。

# include <assert.h> 
# include <stdio.h> 
# include <stdlib.h> 
# include <unistd.h> 
# include <sys/types.h> 
# include <sys/stat.h> 
# include <sys/wait.h> 
# include <string.h> 
# include <fcntl.h> 


int main(){ 
    int t; 

    for(;;){ 
    t = open("test", O_RDONLY); 
    if (t < 0){ 
     perror("open"); 
     exit(1); 
    } 
    printf("%d: ok\n", t); 
    } 
} 
相關問題