Re: How can i know if a file is open by another process?

6,153 views
Skip to first unread message

Hotei

unread,
Mar 23, 2013, 11:27:28 PM3/23/13
to golan...@googlegroups.com
Depending on your os you could try lsof.  If you've got some measure of control over the other programs you could have them create "semaphore" files when they open and then delete the semaphores when they close.  This can be done in a batch file even if you don't control the actual program that's creating/writing as long as you can convince/force people to execute the batch file.  Or you could poll for file size that stops growing after some time limit.   Or...

On Saturday, March 23, 2013 3:56:57 PM UTC-4, Alberto Paganelli wrote:
Hi,
i have a problem. The program that i'm writing reads some files from a folder, the problem is that those files are created and written at the same time that my program is running. So i would like to know if the file that i'm trying to open is closed by the other process, in that way my program will not occurs any problem.

Thanks
Alberto

Andreas Jellinghaus

unread,
Mar 24, 2013, 6:59:58 AM3/24/13
to Alberto Paganelli, golan...@googlegroups.com
You could use OpenFile(filename, O_RDONLY|O_EXCL, 0) on linux at least - this will only work if your process gets exclusive access to the file. If some other process still has the file open, it will fail.

The other option is: have your process create the files with a special filename and rename them, once all data has been written to them and closed the file. e.g. create it as ."filename.tmp" and rename later to "filename". your 2nd process would need to be careful to ignore all files starting e.g. a dot, or ending in ".tmp".

Or use different directories instad of filename extensions:creating the files use a different directory and move the file around once it has written all data and closed the file. For example:
base/tmp <- create new files here, put data in them, close them
base/new <- move file here so for the next process to pick up the file
base/tmp <- 2nd process can move the file around again as a sign of "I', working on this file and handling it"
base/done <- if you process files as a one shot, move them again to a differnt special directory
base/failed <- I often preferr to sort out files that couldn't be processed

Such a schema works only well, if all your filenames are unique. Otherwise you could end up creating the same file
in several steps, and moving the file around would overwrite other files.

Regards, Andreas

John Nagle

unread,
Mar 24, 2013, 2:45:39 PM3/24/13
to golan...@googlegroups.com
On 3/23/2013 12:56 PM, Alberto Paganelli wrote:
> Hi,
> i have a problem. The program that i'm writing reads some files from a
> folder, the problem is that those files are created and written at the same
> time that my program is running. So i would like to know if the file that
> i'm trying to open is closed by the other process, in that way my program
> will not occurs any problem.
>
> Thanks
> Alberto
>
Under Windows, all NT-based versions, opening for exclusive use works.

Under UNIX/Linux?MacOS X, it's a mess. Read

https://www.kernel.org/doc/Documentation/filesystems/mandatory-locking.txt

Files can be opened for exclusive use, but sometimes it doesn't
actually work.

John Nagle


Kevin Gillette

unread,
Mar 24, 2013, 3:50:50 PM3/24/13
to golan...@googlegroups.com, Alberto Paganelli
On Sunday, March 24, 2013 4:59:58 AM UTC-6, Andreas Jellinghaus wrote:
You could use OpenFile(filename, O_RDONLY|O_EXCL, 0) on linux at least - this will only work if your process gets exclusive access to the file. If some other process still has the file open, it will fail.

O_EXCL behavior is only defined when used with O_CREAT -- in which case it'll fail the open call didn't atomically create the file (thus you can use it as a file locking mechanism alongside something like inotify). If you want to make sure you're the only one reading an already existing file, you need file locks, which in many cases are just advisory (so all accessing programs would have to opt-in to locking to guarantee single process exclusivity). You can usually get around issues like this by designing your system to use atomic-renames (make a temporary copy of a file, modify that, then use os.Rename to put it back into place), or requiring all modifying accesses to open the file with O_APPEND -- as long as you output all semantically indivisible chunks in one write, you should safe from accidental corruption, regardless of the number of writers involved.

Alberto Paganelli

unread,
Mar 27, 2013, 4:05:52 PM3/27/13
to golan...@googlegroups.com
Unfortunately the other program don't use O_CREATE so i can't use OpenFile(filename, O_RDONLY|O_EXCL, 0), moreover i don't have any control on the other program so i can't use atomic-renames because i don't know when the other program ands to write the file

S Ahmed

unread,
Mar 27, 2013, 9:03:27 PM3/27/13
to golan...@googlegroups.com
Would it be possible for the files to be 'in flight' when moving to the other folder, and have the process access a file that hasn't fully copied over?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Dave Rose

unread,
Mar 27, 2013, 9:27:13 PM3/27/13
to golan...@googlegroups.com
Would it be feasible to use the modification time I.e the last time the files was modified? You could then test against this time to see how often the file is actually open.

http://golang.org/pkg/os/#FileInfo

Reply all
Reply to author
Forward
0 new messages