Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FLOCK command in Windows

Status
Not open for further replies.

lagerplease

Technical User
Dec 15, 2001
38
GB
I've always had my suspicions that the FLOCK command still allows people to append to a file even though it's supposedly locked. Now I find out, after all these years, that the command isn't supported under Windows!

Is this really true? If so, is there a bespoke way to reliably lock files and prevent users accessing them whilst they're being updated without resorting to writing my own solution?
 
According to a post on flock here sometime ago flock is supported on Windows (NT and above, not desktop ie 95,98,Me).

I had my doubts also, but found that it was my code at fault.

Fancy posting your code?

--Paul
 
The way that I understand it is that FLOCK is used to prevent more than one user from working with the file at the same time. It's true that FLOCK is not supported on a Windows Desktop (single-user) machine but I was unaware of the NT thing. I may be off-base here but it appears that you're trying to "lock" a file by issuing the FLOCK command. This only works as long as your script is running. When it terminates, so does FLOCK.

There's always a better way. The fun is trying to find it!
 
Fairly bog standard stuff, Paul ...


open (DATAFILE, "$datafile");
@datafile = <DATAFILE>;
close (DATAFILE);

open (DATAFILE, &quot;>$datafile&quot;);
flock (DATAFILE, 2);
seek (DATAFILE, 0, 0);

foreach $dataline (@datafile)
{
chomp $dataline;
($reference, $data1, $data2, $data3) = split(/::/, $dataline);

if ($reference eq $form{reference})
{
print DATAFILE &quot;$reference::$form{data1}::$form{data2}::$form{data3}::\n&quot;;
}
else
{
print DATAFILE &quot;$dataline\n&quot;;
}
}

flock (DATAFILE, 8);
close (DATAFILE);


Phil.
 
you're assuming your flock worked first time, and carried on regardless.

I'm away from my machine at present, and can't get access to my own code, but you should check for success of the flock before proceeding

along the lines of
flock_file {
($handle, $method)=@_;
$success_flock=#(need to check success status)
$rc=flock($handle,$method);
if ($rc == $success_flock) {
return $success_flock
}
}

In light of what you need wouldn't you be better off to open a temporary file
Code:
open (DATAFILE ,&quot;<$datafile&quot;);
open (OUTFILE, &quot;>$datafile.tmp&quot;);
while (<DATAFILE>) {
  chomp $_;
  ($reference, $data1, $data2, $data3) = split(/::/, $_);
   if ($reference eq $form{reference}) {
     print OUTFILE &quot;$reference::$form{data1}::$form{data2}::$form{data3}::\n&quot;;
   } else {
     print DATAFILE &quot;$_\n&quot;;
   }
}
close DATAFILE;
close OUTFILE;
unlink ($datafile);
rename &quot;$datafile_tmp&quot;, $datafile&quot;;

HTH
--Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top