Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file is still being written?

Tags:

c++

linux

gcc

How can I check if a file is still being written? I need to wait for a file to be created, written and closed again by another process, so I can go on and open it again in my process.

like image 773
thk987 Avatar asked Apr 22 '11 18:04

thk987


3 Answers

In general, this is a difficult problem to solve. You can ask whether a file is open, under certain circumstances; however, if the other process is a script, it might well open and close the file multiple times. I would strongly recommend you use an advisory lock, or some other explicit method for the other process to communicate when it's done with the file.

That said, if that's not an option, there is another way. If you look in the /proc/<pid>/fd directories, where <pid> is the numeric process ID of some running process, you'll see a bunch of symlinks to the files that process has open. The permissions on the symlink reflect the mode the file was opened for - write permission means it was opened for write mode.

So, if you want to know if a file is open, just scan over every process's /proc entry, and every file descriptor in it, looking for a writable symlink to your file. If you know the PID of the other process, you can directly look at its proc entry, as well.

This has some major downsides, of course. First, you can only see open files for your own processes, unless you're root. It's also relatively slow, and only works on Linux. And again, if the other process opens and closes the file several times, you're stuck - you might end up seeing it during the closed period, and there's no easy way of knowing if it'll open it again.

like image 83
bdonlan Avatar answered Sep 24 '22 13:09

bdonlan


You could let the writing process write a sentinel file (say "sentinel.ok") after it is finished writing the data file your reading process is interested in. In the reading process you can check for the existence of the sentinel before reading the data file, to ensure that the data file is completely written.

like image 37
vinodkone Avatar answered Sep 24 '22 13:09

vinodkone


@blu3bird's idea of using a sentinel file isn't bad, but it requires modifying the program that's writing the file.

Here's another possibility that also requires modifying the writer, but it may be more robust:

Write to a temporary file, say "foo.dat.part". When writing is complete, rename "foo.dat.part" to "foo.dat". That way a reader either won't see "foo.dat" at all, or will see a complete version of it.

like image 42
Keith Thompson Avatar answered Sep 20 '22 13:09

Keith Thompson