Hi,

I'm trying to use file locking to work with the same file from 
different scripts.

Each script may open the file in different mode.

For example:

Script1 opens for APPEND and Script2 opens the file for writing, 
thus truncating the file and then writing the data.

I have read the perldoc.com pages, taken examples from there etc.

It just won't work, somehow the file gets corrupted.
Example:

Script1 opens the file for append and locks it, then 
Script2 opens the file for write and tries to lock (unsuccessfully 
because script1 has the lock) The Script2 ends gracefully.
Then Script1 finishes processing.

The result is that instead of having Script1 append to the file;
data from Script2 (that was supposed NOT to do anything) gets put 
into the file, thus truncating the file.

It happens to me on UNIX and windows platforms


Why is this so obscure and hidden why doesn't the instructions 
from the manuals/books work !!!
Do we need to take a crash course just to write 10 lines to lock 
and read from files ?

Any help is greatly appreciated.

Here are the pieces of code I'm using:


Thanks, 
Edgar.

Script1 - opens for append
=================================================================
sysopen(FH, $name, O_WRONLY | O_APPEND | O_CREAT)
   or die "can't open filename: $!";
#  autoflush FH
 $ofh = select(FH); $| = 1; select ($ofh);
if (defined FH) {                         # if successful try to lock file 
   until(flock(FH, LOCK_EX | LOCK_NB)) {     # do lock until successful
      $lockwait++;                         # add 1 to wait counter 
      if($lockwait > $wait_for_lock) {     # if counter greater max time to
wait for lock
         print STDERR "timed out waiting for lock\n";    # bail out and
return undef
         print "can not lock file\n";
         return(undef);
      }
      sleep(1);    # sleep one second and retry
   } # close until
   return(FH);    # here a file handle was obtained and the file locked
return FH
} # close if defined FH   
return(undef);     # could not obtain FH in the first place.
}

=================================================================
Script2 - opens for write

sysopen(FH2, $name, O_WRONLY | O_TRUNC | O_CREAT)
   or die "can't open filename: $!";
#  autoflush FH2
 $ofh = select(FH2); $| = 1; select ($ofh);
if (defined FH2) {                         # if successful try to lock file 
   until(flock(FH2, LOCK_EX | LOCK_NB)) {     # do lock until successful
      $lockwait++;                         # add 1 to wait counter 
      if($lockwait > $wait_for_lock) {     # if counter greater max time to
wait for lock
         print STDERR "timed out waiting for lock\n";    # bail out and
return undef
         print "can not lock file\n";
         return(undef);
      }
      print "waiting for lock\n";
      sleep(1);    # sleep one second and retry
   } # close until
   return(FH2);    # here a file handle was obtained and the file locked
return FH2
} # close if defined FH2   
return(undef);     # could not obtain FH2 in the first place.
}
=================================================================



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to