Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-11

File locking/protection in Linux

5 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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.

L505
to Antal
Show quoted text 5 lines
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!

Tony Pelton
to L505

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

L505 wrote 22 lines
> 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!
Michael Van Canneyt
to Antal: How can be avoided in Linux the file corruption due to simultaneous write access from two…
Antal wrote 5 lines
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.

Tony Pelton
to Michael Van Canneyt
Michael Van Canneyt wrote 6 lines
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