Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, November 19, 2016

Is there a reason fopen() wouldn't work after several hundred opens?

Is there a reason fopen() wouldn't work after several hundred opens?


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?

Answer by NG. for Is there a reason fopen() wouldn't work after several hundred opens?


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

Answer by VerticalEvent for Is there a reason fopen() wouldn't work after several hundred opens?


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.

Answer by t0mm13b for Is there a reason fopen() wouldn't work after several hundred opens?


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

Answer by Paul Rubel for Is there a reason fopen() wouldn't work after several hundred opens?


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);  }  

Answer by Eyal Lev for Is there a reason fopen() wouldn't work after several hundred opens?


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


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.