9

Hey, for this piece of code, the person who wrote the system communicates data between processes using textfiles. I have a loops that looks (for all intents and purposes) like this:

while (true)
{
 //get the most up-to-date info from the other processes
 pFile = fopen(paramsFileName, "r");

 // Do a bunch of stuff with pFile

 Sleep(100);
}

This will work for several hundred times, but for whatever reason it will return NULL after a while, even though it has opened that same file path several hundred times already! I have double checked that the file exists and has data in it when the fopen returns NULL, and have tried to put a delay/retry in there to no effect.

What can you think of that would cause this?

4
  • Most operating systems have a limit to how many files that can be opened at one time. What OS is this running on?
    – a'r
    Aug 23, 2010 at 16:37
  • 1
    word-detective.com/2009/01/16/intensive-purposes
    – user410344
    Aug 23, 2010 at 16:40
  • 2
    Windows 7. Testing now to see if the fclose() solves my problem. I hate fixing other people's code.....
    – Paul
    Aug 23, 2010 at 16:48
  • Dangit, what language IS this? I get PHP, C++ and Java coming up in my search results...
    – user1228
    Aug 23, 2010 at 17:09

5 Answers 5

15

You're hitting the open file / file descriptor limit for your OS. It should run forever if you do fclose(pFile) in your loop.

3
  • fclose() does not seem to get rid of the problem :<
    – Paul
    Aug 23, 2010 at 16:56
  • 3
    where are you putting the fclose? Please show more code. And have you counted the number of times you succeed before failure? If that number is consistent, it is most likely the file descriptor limit.
    – NG.
    Aug 23, 2010 at 17:13
  • HMM it would appear that I am a fool and failed to fclose() whenever i leave the function, and not at the end of it. This fixed it!
    – Paul
    Aug 23, 2010 at 17:20
5

You really want to check your return codes. I suspect perror/strerror with the right errno would report that you've exausted your file descriptor limit.

Try something like this and see if you get a good error message.

FILE* f = fopen(filename);
if (NULL == f) {
    fprintf(stderr, 
            "Could not open: %s. %s\n", 
            filename, 
            strerror(errno);
}
1
  • 1
    @Lasse: The original question showed C code; the question is tagged as C++ and this answer shows C code. Where did PHP come from? Aug 23, 2010 at 17:16
2

Why are you doing it that way? Two ways to deal with this

while (true)
{
 //get the most up-to-date info from the other processes
 pFile = fopen(paramsFileName, "r");

 // Do a bunch of stuff with pFile

 fclose(pFile);

 //
 Sleep(100);
}

or Move the fopen call to outside of the loop

//get the most up-to-date info from the other processes
    pFile = fopen(paramsFileName, "r");
    while (true)
    {
     // Do a bunch of stuff with pFile

     Sleep(100);
    }
    fclose(pFile);

Not surprising that you hit the OS's limit on the number of files open by constantly calling fopen in your case...

1
  • I can't rework the way that the system shares information, so I must open the passed file to get updated information about what the system is doing. I know it's a terrible/slow way to do it, but I can't rewrite it.
    – Paul
    Aug 23, 2010 at 16:59
1

I'm guessing a thread is started in that loop, and the pFile is sent to that thread, leaving it up to that thread to close the file.

not the best way to code, and if you can, look into wrapping the pFile in a smart/shared pointer, that will call fclose, when the reference count drops to zero (look up that design pattern if you are unfamiliar with it).

simply put, you need to make sure who even gets the pointer to the pFile calls fclose on it (don't call fclose on it from the main thread, if another thread needs to be working on it).

hope this helps.

btw, the '' in the FILE, tells me this is c++ code (java does not have a '*' next to it's types).

0

Try doing a count of how many times the file has been opened before it fails.

As well, it's possible the OS is opening the file for a scan, which blocks the fopen function, returning null, since it was unsuccessful in opening the file.

3
  • I thought it might be that (or another process writing to it), but putting in a while(pFile == NULL) { sleep(100); pFile = fopen(..); } didn't solve it.
    – Paul
    Aug 23, 2010 at 16:39
  • Why not try: pFile = null; while(pFile == null){ pFile = fopen(paramsFileName, "r"); } Aug 23, 2010 at 16:47
  • it's to the same effect, the sleep was just in there to delay and give other processes time to finish. regardless neither work.
    – Paul
    Aug 23, 2010 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.