locking files on Linux

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 18 09:58:48 EDT 2012


On 18 October 2012 14:44, andrea crotti <andrea.crotti.0 at gmail.com> wrote:
> 2012/10/18 Grant Edwards <invalid at invalid.invalid>:
>> On 2012-10-18, andrea crotti <andrea.crotti.0 at gmail.com> wrote:
>>
>>
>> File locks under Unix have historically been "advisory".  That means
>> that programs have to _choose_ to pay attention to them.  Most
>> programs do not.
>>
>> Linux does support mandatory locking, but it's rarely used and must be
>> manually enabled at the filesystem level. It's probably worth noting
>> that in the Linux kernel docs, the document on mandatory file locking
>> begins with a section titled "Why you should avoid mandatory locking".
>>
>> http://en.wikipedia.org/wiki/File_locking#In_Unix-like_systems
>> http://kernel.org/doc/Documentation/filesystems/locks.txt
>> http://kernel.org/doc/Documentation/filesystems/mandatory-locking.txt
>> http://www.thegeekstuff.com/2012/04/linux-file-locking-types/
>> http://www.hackinglinuxexposed.com/articles/20030623.html
>>
>> --
>> Grant Edwards               grant.b.edwards        Yow! Your CHEEKS sit like
>>                                   at               twin NECTARINES above
>>                               gmail.com            a MOUTH that knows no
>>                                                    BOUNDS --
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>
> Uhh I see thanks, I guess I'll use the good-old .lock file (even if it
> might have some problems too).

I think you've misunderstood what Grant meant.

>
> Anyway I'm only afraid that my same application could modify the
> files, so maybe I can instruct it to check if the file is locked.

In that case fcntl will work for you. The point is that fcntl only
locks the file if all of the applications accessing the file use
fcntl. Any other application such as vim can simply ignore the fcntl
lock. Have a read of the links that Grant posted.

Did you try writing twice from the same application that uses fcntl?

> Or maybe using sqlite would work even if writing from different
> processes?

That would also work.


Oscar



More information about the Python-list mailing list