Check whether file is being written to

L

loial

How can I check whether a file is being written to by another process
before I access it?

Platform is unix.
 
S

Seebs

How can I check whether a file is being written to by another process
before I access it?

You mean "written to" or "open for possible writing"?

It may be possible (with sufficient privileges) to determine that a
file has been opened for writing. I don't think you can detect
being-written-to.

More importantly, though, the question is irrelevant, because even if
you absolutely, conclusively, prove that a file is not open nor being
written to, before your next machine instruction loads, the file could
be opened and rewritten multiple times by whole new processes that weren't
even running when you checked.

I suggest you look into advisory locks.

-s
 
T

Thomas Jollans

How can I check whether a file is being written to by another process
before I access it?

Platform is unix.

As such, you can't. But you can lock the file using the functions in the fcntl
module.
 
D

Diez B. Roggisch

Thomas Jollans said:
As such, you can't. But you can lock the file using the functions in the fcntl
module.

Last time I checked, file-locking in unix was co-operative.

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top