Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a C application that occasionally fails to open a file which is stored on a /tmp share.

Here is the relevant chunk of code:

  // open file and start parsing

  notStdin = strcmp(inFile, "-");
  if (notStdin) {
     coordsIn = fopen(inFile, "r");   <----- inFile = file that I want to open
     if (coordsIn == NULL) {
        fprintf(stderr, "ERROR: Could not open coordinates file: %s\n\t%s\n", inFile, strerror(errno));
     coordsIn = stdin;

Once out of eight to ten trials, I get a NULL FILE pointer. Here is an example error message:

ERROR: Could not open coordinates file: /tmp/coordinates.txt
       File or directory does not exist

However, the file /tmp/coordinates.txt does indeed exist, as I can open it with standard utilities like head, cat or more, etc.

The permissions of different /tmp/coordinates.txt trial files are the same.

Here is the result from uname -a:

$ uname -a
Linux hostname 2.6.18-128.2.1.el5 #1 SMP Wed Jul 8 11:54:47 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux

If I use a different inFile that is stored in a different, non-/tmp share, then I do not observe this symptom.

Is there anything that would cause fopen() to fail on a file stored in the /tmp share? Are there other troubleshooting steps I can pursue?

share|improve this question

5 Answers 5

up vote 5 down vote accepted

Too Many Open Files?
Is your program opening lots of files? Perhaps you are running out of file descriptors? Here is a link about how to change your program, the shell, and the OS if this is the case. To see you many you are using with your program:

sudo lsof | grep <PID> | wc -l

On my Ubuntu system, the shell limit is 1024 include the stdout, stderr, and stdin. This is set in /etc/security/limits.conf. The following little program shows this:

#include <stdio.h>

int count=0;

int main( void ) {
    while(1) {
    	FILE *fd = fopen("foo", "r");
    	if ( fd == NULL) {
    		printf("%i\n", count);

When I run it prints "1021" with an exit status of 1.

Check For System Errors:
More generically, you can always check the output of dmesg or /var/log/messages for any errors.

Watch the file, see if something else is messing with it:
Perhaps the file doesn't exist, something is deleting it out from under you? You might want to use inotify to watch all events on the file, or tools that uses inotify such as incron or inotify-tools.

share|improve this answer
Actually, errno would say too Many open files if it was the first problem, But I will leave it anyways, might be interesting to someone :-) –  Kyle Brandt Aug 6 '09 at 12:11
+1 for inotify, it's awesome. Although, ulimit -n (bash) or getrlimit (C) is a bit nicer than brute-force detection of max open file-descriptors. –  Kjetil Joergensen Aug 6 '09 at 12:38
Kjetil: Ya of course, I should have worded that better. It was meant as a proof of concept, not a tool to use :-) –  Kyle Brandt Aug 6 '09 at 12:41

Maybe some program is locking this file? It can be another copy of your program.

Does lsof /tmp/coordinates.txt show anything?

share|improve this answer

I can't think of anything special with /tmp which should make files be intermittently not be there. /tmp is just a regular directory with slightly special permissions allowing everybody to mess around, but limit non-root users to not owning files to delete them.

Is there anything external to your program modifying/changing that file, or, are your program multi-threaded and work with that file ? If so, it could be a race-condition.

To check if this is the case you could use inotify as Kyle suggests (you're probably after delete and moved from events) to see exactly what happens with the file.

Alternatively, you could try to stat the file and see if it's ctime and/or mtimes coincide with when fopen failed with ENOENT.

share|improve this answer


Just a wild guess. Do you have the /tmp/coordinates.txt file open in vim while this problem occur?

I have experienced problems with the file I am editing in vim disappearing from the file system and reappearing very shortly after. I have never actually seen the file missing with ls, but I have had e.g. gcc fail in trying to open the file, only to succeed in the second try.

This is not a very seldom artifact, it happens regularly during the day.

share|improve this answer

I don't know what the solution is, but I know that I have observed this same problem, and not just on /tmp. It can happen over NFS mounts, and even local mounts, like /lib. A problem I found today reproduces this by (incorrectly) opening, seeking, reading a little and closing the same file thousands of times (instead of just keeping the file open for the whole operation). Sporadically, one of the fopen()'s would fail with an ENOENT error. This is not too many files open as one person suggested -- it literally thinks the file isn't there for a split second.

I've been searching to see if anyone else has had similar problems, and this is the closest I've come. I don't have any answers, though, just looking for a solution.

I don't think this is endemic to Linux (or RHEL), because I don't see this everywhere, just in one environment. I don't know what's different about that environment that could cause this problem (there are differences). Though it isn't good to see that whatever it is isn't fixed in RHEL5 (I see it on RHEL4).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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