Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

File Use

by qball (Beadle)
on Apr 04, 2001 at 22:55 UTC ( #69796=perlquestion: print w/replies, xml ) Need Help??
qball has asked for the wisdom of the Perl Monks concerning the following question:

How do I test whether a file is in use, ie. being written to by a program. Once I know if the file's in use, I can then my program wait before it uses the file.

Replies are listed 'Best First'.
(Ovid -flock) Re: File Use
by Ovid (Cardinal) on Apr 04, 2001 at 23:05 UTC

    The straightforward way to do this is to use flock, but it's "advisory only." There are a couple of basic ways to flock a file (shared, exclusive), but if other programs don't use flock, you're out of luck.

    If your program requests a flock and the file is already exclusively locked, your program knows not to open the file, but again, that doesn't work if the other programs don't bother to use flock. If you can't control the behavior of other programs, perhaps you should examine your requirements and determine an alternative.


    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Only my program will be accessing the file. I just need to make sure it's not being used if someone else runs my program. I'm looking at flock now, but can it return true if the file is in use? qball-"I have node idea?!"
        Then you're in luck. You want an exclusive lock on the file:
        use Fcntl ':flock'; # import LOCK_* constants my $filename = "somefile.txt"; open FILE, ">>$filename" or die "Cannot open $filename for writing: $! +"; flock FILE, LOCK_EX or die "Cannot get exclusive lock on $filenam +e: $!";
        Just be sure not to forget to unlock the file when done. Plus, my code above is for appending, which is what I suspect you want.


        Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: File Use
by thabenksta (Pilgrim) on Apr 04, 2001 at 23:07 UTC

    If I understand you situation correctly you can take care of this with flock.

    open(HANDLE, ">file.dat"); flock(HANDLE, 2); #lock file # file processing flock(HANDLE, 8); #unlock file close(HANDLE);

    This will lock you file so no one else can write to it while the program is. Any other program will have to wait to write to it.

    Hope that helps.

    my $name = 'Ben Kittrell'; $name=~s/^(.+)\s(.).+$/\L$1$2/g; my $nick = 'tha' . $name . 'sta';
Re: File Use
by qball (Beadle) on Apr 04, 2001 at 23:29 UTC
    Thanks Ovid and thabenksta. Both of your answers are exactly what I need. I appreciate quality feedback.

    qball~"I have node idea?!"

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://69796]
Approved by root
[stevieb]: well, what happens is the Arduino 'listens' for requests r/w, and does the appropriate thing when it's interrupted based on the 'register' address sent in. It's ugly as it was my first attempt, but I've got great new ideas I'm just sitting.
[stevieb]: ...down to implement now. Here's the sketch as it currently sits
[shmem]: well I use I2C and SPI and stuff, but creating a pseudo chip looks to me like lot of indirection and memory clutter... not?
[choroba]: Are you going to use the device soon? Related to your comment about "not having much time to do a lot of coding"...
[stevieb]: sure, but I'm just learning ;) I consider it practice to get a good understanding of what goes on *after* an I2C/SPI request is made
[shmem]: ah ok. Gonna read that. but now....
shmem compiles himself into his template
[stevieb]: this is my RPi:: distribution overall automation build system physical layout. It doesn't encompass all of my RPi dists yet, ...
[stevieb]: ...but I'm getting close. I've been soldering my own PCBs to gain that experience as well, before I have my own manufactured.
[stevieb]: heh, nice *template* ;) 'night shmem!

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (9)
As of 2017-06-25 22:45 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (572 votes). Check out past polls.