Hello,
I'm writing a program that makes a call to pipe and fork in a loop. I
didn't notice the problem at first because the number of iterations in
the loop I was testing was around 50-100. The program can run hundreds
maybe thousands of iterations, but the greater the number of iteration,
the more likely it will exit due to an error (EMFILE = Too many open
files) caused by calls to pipe. The code is as follows...
....
for ( i = 0; i < num_of_segs; i++ ){ // - - - - - - - - - - - //
if (0 != pipe(proci) || 0 != pipe(proco))
return 1; // pipe exits the program, errno is set to 24
/* Create the Child */
pid = fork();
if (0 == pid){
dup2(proci[0], 0); /* copy reading end to stdin */
dup2(proco[1], 1); /* copy reading end to stdout */
close(proci[0]);
close(proci[1]);
close(proco[0]);
close(proco[1]);
execl(cmd, "cmd", flags, NULL);
return 2;
}
/* Parent Process */
if (pid > 0) {
close(proci[0]);
close(proco[1]);
while (0 < (n = read(proco[0], buffer, 38)) ){
...
}
close(proci[1]);
close(proco[0]);
}
wait(&status);
} // End for - - - - - - - - - - - - - - - - - - - - - - - - - //
....
I have tried kill(pid,SIGKILL) instead of waiting, but this did not help.
Thanks in advanced,
~Jerome
|
|
0
|
|
|
|
Reply
|
Jerome
|
2/2/2004 10:59:15 PM |
|