views:

127

answers:

1

Continuing from this question:

When I am trying to do fopen on Windows, I get a "Too many open files" error. I tried to analyze, how many open files I have, and seems like not too much.

But when I executed Process Explorer, I noticed that I have many open handles with similar names: "\Device\NamedPipe\Win32Pipes.00000590.000000e2", "\Device\NamedPipe\Win32Pipes.00000590.000000e3", etc. I see that the number of these handles is exactly equal to the number of the iterations that my program executed, before it returned "Too many open files" and stopped.

I am looking for an answer, what are these handles, and could they actually cause the "Too many open files" error?

In my program I am loading files from remote drive, and I am creating TCP/IP connections. Could one of these operations create these handles?

+1  A: 

Are you remembering to fclose() your pipe each time through the iteration? (see -> below).

If not you are leaking open pipes.

for(i = 0; i < lotsOfIterations; i++)
{
    FILE *fp;

    fp = fopen(filename[i], "r");
    if (fp != NULL)
    {
        ... do work, etc

        fclose(fp); // finished with this file handle (add this line!)
    }
}

However, if your intent is have a lot of file handles open at once, then the other thing to be aware of is that the C runtime typically specifies a number of file handles you can have open at any one time. This number will typically be a lot less than the operating system is capable of providing. To use the OS provided file handles you will need to use Win32/Win64 API functions:

  • CreateFile
  • ReadFile
  • WriteFile
  • GetFileSize
  • CloseHandle

OS provided file handles are of type HANDLE not FILE *

Stephen Kellett
You will run out of open files when you run out of open handles, e.g. e.g. a FILE* is backed by a handle.
nos
I don't intend to have a lot of file handles open at once. I want to find the root cause of the problem - which handles aren't getting closed in my program. It seems like these are not file handles, but those NamedPipes. I am trying to understand what are them, and are they related to the open files, or open sockets, or anything else.
Igor Oks
Stephen: Eventually I found out that I had a pipe that I didn't close. Thanks.
Igor Oks