main()
{
FILE *fps[MAXFOPEN];
int fds[MAXOPEN];
char fname[256];
int i, j;
/*
* Test total number of fopen()"s which can be completed
*/
for (i = 0; i < MAXFOPEN; i) {
sprintf(fname, "fopen_d", i);
if ((fps[i] = fopen(fname, "w ")) == NULL) {
perror("fopen fails");
break;
}
}
printf("fopen() completes: %dn", i);
/*
* Close the file descriptors
*/
for (j =0; j < i; j) {
if (fclose(fps[j]) == EOF) {
perror("fclose fails");
exit(1);
}
}
for (i = 0; i < MAXOPEN; i) {
sprintf(fname, "open_d", i);
if ((fds[i] = open(fname, O_CREAT | O_RDWR, 0644)) == -1) {
perror("open fails");
break;
}
}
printf("open() completes: %dn", i);
}
A sample compilation and run will be shown in the Process Environment Limitations section.
Note that 64-bit binaries are not subject to the fopen(3C) limitation. However, 64-bit binaries are subject to the file open hard limit.
Process Environment Limitations
The process environment limit is one which can be easily adjusted by the user with the ulimit command. Following is sample output of the ulimit command, along with the test program which uses the open() and fopen() calls. This example is run under the Korn shell (/bin/ksh). Other shells may use different ulimit syntax.
$ cc filetest.c -o filetest
$ ulimit -a
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 8192
coredump(blocks) unlimited
nofiles(descriptors) 256
memory(kbytes) unlimited
$ filetest
fopen fails: Too many open files
fopen() completes: 253
open fails: Too many open files
open() completes: 253
$ ulimit -n 512
$ ulimit -a
time(seconds) unlimited
file(blocks) unlimited
data(kbytes) unlimited
stack(kbytes) 8192
coredump(blocks) unlimited
nofiles(descriptors) 512
memory(kbytes) unlimited
$ filetest
fopen fails: Too many open files
fopen() completes: 253
open fails: Too many open files
open() completes: 509
This is an example of raising the file limit from the process maximum of 256 up to 512 by using the ulimit -n command. The "filetest" program attempts to open(2) and fopen(3C) as many files as possible, and to print results on failure. Note that the program fails at the limit minus three. As mentioned earlier, the three other descriptors are the standard stdin, stdout and sdterr. Those descriptors are open for every process; they can be closed if the program does not need their functionality.
This example shows the soft limit being decreased from 512 to 256. The default soft and hard limits can be found by using the sysdef command and looking at the "Process Resource Limit Tunables" section of the output.
Kernel Limitations
The ulimit maximum (hard limit) is based on a kernel parameter. The kernel variable which controls the limit can be set in /etc/system. The variable name is rlim_fd_max. The value can be changed by adding the following line to /etc/system.
set rlim_fd_max=2048
This will set the maximum number of file descriptors (hard limit) per process up to 2048. Different versions of the Solaris OE have different limits. The following table summarizes the changes.
Default Solaris file limitations Solaris Release Default soft file limit Default hard file limit
Solaris 7 64 1024
Solaris 8 256 1024
Solaris 9 256 65536
The default settings listed above mean that the ulimit command will not be able to go above that level in a newly-installed system. When the rlim_fd_max variable is set, users will be able to go up to the maximum specified value.
If a program cannot rely on the ulimit value being set properly, code can be written which will check the hard and soft limits currently set on file descriptors. The program can then ask the user to take proper actions. Following is an example program which uses getrlimit(2) to request the current values.
推荐阅读
- Quick_Instaling_OpenSSH_for_Solaris 8.0_method-1
- Solaris OpenBoot PROM
- Solaris 8 定制OpenWindows工作区
- 在本地OpenWindows V3.x系统显示远程操作OpenWindows V2.x
- Solaris 8 如何定制您的OpenWindows工作区
- 如何在本地OpenWindows V3.x系统显示远程OpenWindows V2.x
- 续 HOPEN智能嵌入手机操作系统之艰难摸索
- HOPEN智能嵌入手机操作系统之艰难摸索
- oc是什么意思
- no和nc表示什么
