File locking/protection in Linux

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

File locking/protection in Linux

Antal
How can be avoided in Linux the file corruption due to simultaneous write
access from two different process?
Or how can it be avoided to access the same file by two different process?
I experienced a few (but painful) data loss due to this problem, and I'm
unable to solve the problem.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: File locking/protection in Linux

L505
> How can be avoided in Linux the file corruption due to simultaneous write
> access from two different process?
> Or how can it be avoided to access the same file by two different process?
> I experienced a few (but painful) data loss due to this problem, and I'm
> unable to solve the problem.


The Pascal Server Pages project has a unit in it that allows this if you are interested. It
is not specific to Pascal Server Pages as far as I know.

Basically how it works is a simple time delay is put on the file until it is free again.
You can check FileShare.pp if you are intersted in a file locking read/write method.

SVN:
https://opensvn.csie.org/pspcgi/psp-1.5.x-devel/src/fileshare.pp

For documentation on how those units would be used, check:
http://z505.com/cgi-bin/psp/docs/1.4.1/psp_doc_i.cgi?p=mod.fileshare.html

Again, these units and documentation were written with regards to CGI programming. However,
file sharing is not specific to cgi programming of course!

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: File locking/protection in Linux

Tony Pelton
without looking at what the PSP code is doing ...

an article on this subject, specific to file locking on linux ...

http://lists.jammed.com/ISN/2003/06/0104.html

Tony

On 11/1/05, L505 <[hidden email]> wrote:

> > How can be avoided in Linux the file corruption due to simultaneous write
> > access from two different process?
> > Or how can it be avoided to access the same file by two different process?
> > I experienced a few (but painful) data loss due to this problem, and I'm
> > unable to solve the problem.
>
>
> The Pascal Server Pages project has a unit in it that allows this if you are interested. It
> is not specific to Pascal Server Pages as far as I know.
>
> Basically how it works is a simple time delay is put on the file until it is free again.
> You can check FileShare.pp if you are intersted in a file locking read/write method.
>
> SVN:
> https://opensvn.csie.org/pspcgi/psp-1.5.x-devel/src/fileshare.pp
>
> For documentation on how those units would be used, check:
> http://z505.com/cgi-bin/psp/docs/1.4.1/psp_doc_i.cgi?p=mod.fileshare.html
>
> Again, these units and documentation were written with regards to CGI programming. However,
> file sharing is not specific to cgi programming of course!
>
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: File locking/protection in Linux

Michael Van Canneyt
In reply to this post by Antal


On Wed, 2 Nov 2005, Antal wrote:

> How can be avoided in Linux the file corruption due to simultaneous write
> access from two different process?
> Or how can it be avoided to access the same file by two different process?
> I experienced a few (but painful) data loss due to this problem, and I'm
> unable to solve the problem.

You can use the fpflock call (unit 'unix') to lock (parts of) a file.
But be aware that BOTH processes must use this mechanism;
File locking is optional on Linux.
Nothing prevents a third process from using the file without locks.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: File locking/protection in Linux

Tony Pelton
On 11/2/05, Michael Van Canneyt <[hidden email]> wrote:
> On Wed, 2 Nov 2005, Antal wrote:
>
> > How can be avoided in Linux the file corruption due to simultaneous write
> > access from two different process?
>
> Nothing prevents a third process from using the file without locks.

that was what i thought too.

but that link i posted seemed to imply that, if you mounted the
filesystem with an extra flag, that the kernel would cause any regular
process to wait for access to files, if those files had been "flocked"
by any one process, so it then only takes 1 process using the "flock"
calls to get exclusive access.

from the link :

"In these cases, you can enforce locking at the kernel level with
mandatory locks. Mandatory locking is implemented on a file-by-file
basis. When a program attempts to lock a file with lockf or fcntl
that has mandatory locking set, the kernel will prevent all other
programs from accessing the file.[1] Processes which use flock will
not trigger a mandatory lock."

>
> Michael.

Tony
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal