Open File Problems - WIERD!!!

Ian Collins
Sun May 9 19:21:00 GMT 1999

Can someone please help me with this. I have reproduced the problem on three
seperate gnu-win32 installs.
All three gnu-win32 installs are running on B20.1 using gcc version
egcs-2.91.57 19980901 (egcs-1.1 release).

The problem manifests itself after a program has opened it's 30th file. I
see shell errors appearing ("Pipe call failed"), and even i/o corruptions
after the 30th file is opened.  

Why should 30 be the magic number? It does not correlate with limits.h
(which implies 16 is the max for fopen - surely not!!!). (Incidentally, On
my HPUX machine and SCO, these limits appear to be 60, on Linux this is 256
and AIX is 2000)
Note also that I don't get an error from fopen, and also that the when the
error manifests itself, it is actually in a child process - NOT in the
process that has all the files open!

Try this simple program that demonstrates the problem. This program (which
is functionally useless but demonstrates the point), loops from 0 to 31
opening a file, file0, file1, ... file31 using fopen. (Not forgetting that
stdin, stdout and stderr are already open).
After opening the file, it calls a child process which just uses a pipe
(ls|cat > /dev/null).

(This program is the result of a binary chop on 45Mb of code that behaved
strangely occasionally).

Here we go,

// ================= snip ================
// Link with cc -o myfile myfile.c
#include <stdio.h>
int main()
  int i;
  char fileZ[10];
  FILE *f;

  // remember that there are three files are ready opened, namely stdin,
stdout and sterr.

  for(i=0; i<32; i++) {
    sprintf(fileZ, "file%d", i);
    if ((f=fopen(fileZ, "r+b")) == (FILE*) NULL) {
      if ((f=fopen(fileZ, "w+b")) == (FILE*) NULL) exit(2);
    if(i >= 26) {    // Just for brevity - the problem does not appear yet.
      printf("After open of %s\n", fileZ);
      system("ls|cat > /dev/null");
// ================= snip ================

As you can see, this is fairly self explanatory. What I see (and what I hope
you will see too), is the following, when the program is run,

$ ./tc3
After open of file26
After open of file27
Pipe call failed
After open of file28
After open of file29
After open of file30
After open of file31

When we run our real application, we actually see file corruptions as well
as the error message ("Pipe call failed"). We actually found it when
starting a child process to sort a file. The sort process is started by a
script which parses it's arguments, and passes the result to an executable
that performs the sort.
We noticed that, 1. The above "pipe" error appeared, and 2. The sorted file
was corrupt.
The problem ONLY appears when the parent program has 30 files opened!!!

Hoping someone can help,
Ian Collins.

Want to unsubscribe from this list?
Send a message to

More information about the Cygwin mailing list