tags:

views:

129

answers:

4

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?

+11  A: 

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

SB
fclose() does not seem to get rid of the problem :<
Paul
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.
SB
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
A: 

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.

VerticalEvent
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
Why not try:pFile = null;while(pFile == null){pFile = fopen(paramsFileName, "r");}
VerticalEvent
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
+2  A: 

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...

tommieb75
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
+3  A: 

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);
}
Paul Rubel
Is that PHP code?
Lasse V. Karlsen
@Lasse: The original question showed C code; the question is tagged as C++ and this answer shows C code. Where did PHP come from?
Max Lybbert
Thanks for the error printing code! It really helped!
Paul
It was tagged php earlier.
Lasse V. Karlsen
OK, that makes sense.
Max Lybbert