RE: open and edit file to prevent race conditions
- From: Bob_Showalter@xxxxxxxxxxxxxxx (Bob Showalter)
- Date: Mon, 29 Aug 2005 09:56:36 -0400
Brent Clark wrote:
> Hi List
>
> I have a scenario / debate if you wish with my colleague.
>
> The problem is I have to update a CSV file and the way I do it open
> the file while reading / editting it I then send it to a temp file,
> unlink the original and the rename the temp to the original file name
> (Baring in mind I do flock the file)
>
> My colleague, disagrees with my approach to this, he is adamant
> that I must open the file get the file in memory and use seek etc to
> update the file (also using flock etc.)
>
> So basically from the millisecond it takes to unlock , close,
> unlink, and then rename the file, I am open to race condition.
>
> So my question is, whats the best what to update / amend a file.
He's right; there is a race condition. But worse than that, if an other
process is continuously writing to the file, your unlink and rename
operation will cause the data from the other process to be lost.
Instead of unlinking and renaming, you could just copy the data from the
temp file back into the original file. Or, use Tie::File and update the file
in place.
.
- Prev by Date: Re: Unsure how to start
- Next by Date: RE: Directories into arrays, again.
- Previous by thread: Re: open and edit file to prevent race conditions
- Next by thread: Find first day of every week in a month
- Index(es):
Relevant Pages
|