File Locking and File Deletion 
Author Message
 File Locking and File Deletion

        I have about 6 different perl scripts that access the same flat file
and are used by around 200-300 users and is on there web sites and each time
they have a visitor, it is used. Every once in a while, it will delete data out
of the file.. Today I have 16 in the datafile when it was suppose to be 247.

        Another time it had increased itself to over 1 meg in size, and I was
told that my file locking wasn't correct and to use the code below instead. Any
ideas why the code is being delete or duplicated?

        I've considered using SQL but my server does not support any of these
so I'm stuck with flat file for now.

        I have file locking in place on every file, this is a s{*filter*}t:

&lock_file ($all, 5) || die "Can't lock $all";
unless ( open(FILE,">$datapath/all.txt") ) {
&unlock_file ($all);
die "Can't open $all for appending.";


&unlock_file ($all);

        And this is lock_file and unlock_file:

sub lock_file {

        local ($keen);

        for ($keen = 1; $keen <= $numtires; ++$keen) {
        if ( symlink ($all, $all . ".lck") ) {
        return 1;
        sleep 1;
        return 0;


sub unlock_file {

        unlink ($all . ".lck");


Thanks for all of your help,
Jordan DeLozier

Wed, 18 Jun 1902 08:00:00 GMT  
 File Locking and File Deletion

> &lock_file ($all, 5) || die "Can't lock $all";
> sub lock_file {

        [snip of broken code]

Not atomic. Race conditions. Broken code.

# perldoc perlfaq5
     How can I lock a file?
     What [WHY] can't I just open(FH, ">file.lock")?

# perldoc -f flock
look at col04

Martien Verbruggen                  |
Interactive Media Division          | If at first you don't succeed, try
Commercial Dynamics Pty. Ltd.       | again. Then quit; there's no use being
NSW, Australia                      | a damn fool about it.

Wed, 18 Jun 1902 08:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. scheduled file deletion...please help

2. Crafty automatic file deletion?

3. usernames deletion in passwd file

4. help with file deletion

5. PERLFUNC: flock - lock an entire file with an advisory lock

6. PERLFUNC: flock - lock an entire file with an advisory lock

7. File altering in-place w/ file locking

8. File Locking with tied [GNS]?DBM files...

9. File locking with fcntl()

10. Easy file locking technique

11. Q: File Access when/how to lock?

12. Unable to Lock password file


Powered by phpBB® Forum Software