How to find out if another process is using a file

T

Tom Wright

I'm writing a program which reads a series of data files as they are dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.

Is there any way of doing this? I've drawn a blank with google so far.

A nasty hack would be to use the file modification time, and wait until
that's a few seconds in the past, but is there a nice solution?
 
J

js

How about using lock?
Let writing process locks the files before writing, and unlock after
the job's done.

I think it'd work file in most environment.
 
G

Gabriel Genellina

I'm writing a program which reads a series of data files as they are
dumped
into a directory by another process. At the moment, it gets sporadic bugs
when it tries to read files which are only partially written.

I'm looking for a function which will tell me if a file is opened in
write-mode by another process - if it is, my program will ignore it for
now
and come back to it later. This needs to work on linux and windows. Mac
OS would be a bonus too. An os-independent solution would be good, but I
could write os-specific options and have it pick the appropriate one.

Use os.open with the O_EXCL flag; will fail if the other process has the
file still open (and will fail if another process is reading the file, too,
not just if someone is writing).
 
D

Donn Cave

"Gabriel Genellina said:
Use os.open with the O_EXCL flag; will fail if the other process has the
file still open (and will fail if another process is reading the file, too,
not just if someone is writing).

O_EXCL fails if the file exists at all - whether closed or open.

Donn Cave, (e-mail address removed)
 
N

Nick Maclaren

|> In article <[email protected]>,
|> > "Tom Wright" <[email protected]> escribió en el mensaje
|> > |> >
|> > > I'm writing a program which reads a series of data files as they are
|> > > dumped
|> > > into a directory by another process. At the moment, it gets sporadic bugs
|> > > when it tries to read files which are only partially written.
|> > >
|> > > I'm looking for a function which will tell me if a file is opened in
|> > > write-mode by another process - if it is, my program will ignore it for
|> > > now
|> > > and come back to it later. This needs to work on linux and windows. Mac
|> > > OS would be a bonus too. An os-independent solution would be good, but I
|> > > could write os-specific options and have it pick the appropriate one.
|> >
|> > Use os.open with the O_EXCL flag; will fail if the other process has the
|> > file still open (and will fail if another process is reading the file, too,
|> > not just if someone is writing).
|>
|> O_EXCL fails if the file exists at all - whether closed or open.

Yes. In theory. In practice, it usually works on normal files, provided
that all opens are local. Under some circumstances, it will even work
for NFS mounted files, as far as I recall.

MVS (now zOS) and other mainframe systems had what the poster wants, but
modern systems don't. I shall not recommend that the poster asks IBM
for a copy of zOS, for reasons that will be well-known to anyone who used
MVS (which are NOT the ones claimed by the Unix brigade, which are mostly
bogus) :)

Under Linux, you can do something with fuser, and I am pretty certain that
modern Macintoshes (i.e. BSD) will have an equivalent. It can't be made
reliable (unlike under MVS), but might reduce the number of problems.


Regards,
Nick Maclaren.
 
D

Donn Cave

|> In article <[email protected]>,
|> > "Tom Wright" <[email protected]> escribió en el mensaje
|> > news:[email protected]...
|> > Use os.open with the O_EXCL flag; will fail if the other process has the
|> > file still open (and will fail if another process is reading the file,
|> > too,
|> > not just if someone is writing).
|>
|> O_EXCL fails if the file exists at all - whether closed or open.

Yes. In theory. In practice, it usually works on normal files, provided
that all opens are local. Under some circumstances, it will even work
for NFS mounted files, as far as I recall.

Mm, by "fail", I meant

An attempt to open with O_EXCL set will "fail" if the file exists at all,
i.e., the file will not be opened, a negative value will be returned,
and errno will be set to EEXIST.

What I neglected to mention is that this effect obtains when O_EXCL
is used in combination with O_CREAT. Without O_CREAT, O_EXCL doesn't
mean anything and is ignored.

If there is any significant difference between theory and practice
in this matter, it's news to me.

Donn
 
N

Nick Maclaren

|> > |>
|> > |> O_EXCL fails if the file exists at all - whether closed or open.
|> >
|> > Yes. In theory. In practice, it usually works on normal files, provided
|> > that all opens are local. Under some circumstances, it will even work
|> > for NFS mounted files, as far as I recall.
|>
|> Mm, by "fail", I meant
|>
|> An attempt to open with O_EXCL set will "fail" if the file exists at all,
|> i.e., the file will not be opened, a negative value will be returned,
|> and errno will be set to EEXIST.
|>
|> What I neglected to mention is that this effect obtains when O_EXCL
|> is used in combination with O_CREAT. Without O_CREAT, O_EXCL doesn't
|> mean anything and is ignored.
|>
|> If there is any significant difference between theory and practice
|> in this matter, it's news to me.

Actually, it is undefined behaviour in POSIX, and I have used a Unix
where O_EXCL on its own failed if the file existed (of course, it also
failed if it DIDN'T exist, because of the lack of O_CREAT). I can't
remember which - perhaps AIX or OSF/1 (not DEC).

But I was referring to a different problem. Some remote and parallel
filing systems handle such things very badly, and it is often possible
to create a file even if it exists and O_CREAT|O_EXCL is set. A similar
remark applies to 'special' files, even on fairly mainstream, local
filing systems.


Regards,
Nick Maclaren.
 
T

Tom Wright

js said:
How about using lock?
Let writing process locks the files before writing, and unlock after
the job's done.

Is locking mandatory or co-operative? I don't have any control over the
process which is doing the writing, so if it's co-operative it's no good to
me.

If it's mandatory, then I can try to acquire a lock on the file - if this
fails or blocks, then the other process must have it open. Will this work?
 
D

Diez B. Roggisch

Tom said:
Is locking mandatory or co-operative? I don't have any control over the
process which is doing the writing, so if it's co-operative it's no good
to me.

If it's mandatory, then I can try to acquire a lock on the file - if this
fails or blocks, then the other process must have it open. Will this
work?

AFAIK it's cooperative.

Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top