- 论坛徽章:
- 0
|
代码如下:int main(int argc,char** argv)
{
pid_t pid;
pid = fork();
if(pid > 0)
{
int i = 4;
cout << "parent" << endl;
while(i --)
{
sleep(3);
cout << "sleep" << endl;
}
}
else if(pid == 0)
{
rlimit rl;
rl.rlim_cur = 4;
rl.rlim_max = 4;
FILE* ho = freopen("result.out","w+",stdout);
FILE* hi = freopen("arg","r+",stdin);
if(setrlimit(RLIMIT_NOFILE,&rl))
cout << "setrlimit error" << endl;
//cout << "in child" << endl;
if(execl("./targ","targ",(char*)0))
cout << "abcdefg" << endl;
}
return 0;
} |
父进程只是等待。
子进程重定向了标准输入和标准输出之后,限定了能打开的文件数为4。
如果targ的功能是执行两个数的加法,而arg中是1 3。这时result.out中为4。
如果把限定能打开的文件数变为3,则程序执行失败,result.out的内容为cout << "abcdefg" << endl;
因为打开文件走过上限了,但是targ中并没有打开文件。如下:
#include<iostream>
using namespace std;
int main(int argc,char** argv)
{
int i,j;
cin >> i >> j;
cout << i + j;
return 0;
} |
请问为什么打开文件数是4的时候可以运行,打开文件数限制为3时就被终止了。。。。。? |
|