[fpc-pascal]Re: fpc-pascal digest, Vol 1 #1470 - 5 msgs
Michael Van Canneyt
michael.vancanneyt at wisa.be
Fri Aug 23 17:58:15 CEST 2002
On Fri, 23 Aug 2002, mili wrote:
> > On Thu, 22 Aug 2002, mili wrote:
> > > Hello, Every one
> > >
> > > I opened a file with TFileStream, and I don't want any other program to
> > > access this file. So I try to lock it with FLock function in unit Linux
> > > as following:
> > >
> > >
> > > var
> > > fs : TFileStream;
> > > h : integer;
> > > begin
> > > fs:=TFileStream.Create('test.dat',fmOpenReadWrite);
> > > h:=fs.Handle;
> > > if LockF(h,LOCK_EX) then writeln('Lock OK!') else writeln('Lock
> > > Failed!'); readln; // ----> Now, I test if I can still open this file
> > > with other program
> > > fs.Free;
> > > end.
> > > <<<<<<
> > >
> > > But, Still, I can open the same file with other programs.
> > > So, Can you tell me how to lock a file correctly with free pascal?
> >
> > This is not possible on linux. Only other programs that also place a
> > lock will fail when they try to place the lock; locking is advisory at
> > best.
> >
> > Michael.
> >
>
> Hello, Michael
>
> I don't know exactly your idea by "not possible on linux". Do you mean "It's
> not possible to exclude other program to access the same file which has been
> locked under linux"
Yes.
> or "The described phenomenon of this example is not
> possible to occur"?
> I am sure LockF has returned success because it output 'Lock OK!', but I
> still can open it with Kate,Kedit or any other program.
Yes, because they do not try to lock it. If they would try to lock it,
it would fail, and they would not be able to open it.
That is why I said that locking is advisory - all programs accessing a
file must attempt to lock it for locking to have effect.
Michael.
More information about the fpc-pascal
mailing list