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?
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 *
精彩评论