Linux Cファイルオープン数制限(ファイル記述子)
1593 ワード
/*
Linux , (windows , “ ”)。 open , ,Linux 。
0:
1:
2:
open , , , , ,
*/
#include"stdio.h"
#include"sys/stat.h"
#include"fcntl.h"
#include"errno.h"
#include"string.h"
#define SUCCESS 0
#define FAILURE -1
int main(int argc,char ** argv)
{
int iFd;
int i = 0;
iFd = open("/etc/file",O_RDWR);
if (iFd < 0)
{
printf("error....
");
return FAILURE;
}
i = i + 1;
while (iFd > 0)
{
iFd = open("/etc/file",O_RDWR);
if (iFd > 0)
{
i++;
}
else
{
perror("open Failed");
break;
}
}
printf("open file is %d
",i+3); // 0 1 2
}
/* ,
[root@desktop97 root]# ./a.out
open Failed: Too many open files
open file is 1024
ulimit -HSn 10240
[root@desktop97 root]# ./a.out
open Failed: Too many open files
open file is 10240
*/