> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, November 01, 2001 1:53 PM
> To: [EMAIL PROTECTED]
> Subject: Please ! Expert help needed - File locking
> 
> 
> 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.

File locking is *advisory* only. There is nothing to prevent you from
trashing a file somebody has locked. Your processes must cooperate and
get their locks before modifying the file.

You should not open the file with O_TRUNC. This will clobber the file
even if the other script has it locked. Open the file first and get
your lock. Then truncate the file if you need to with the truncate()
function.

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

Reply via email to