Concurrent access and locks

This is a discussion on Concurrent access and locks within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'm developing a collaborative application in PHP which accesses local files and may modify them. How can I synchronize ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-02-2005
Bruno Barberi Gnecco
 
Posts: n/a
Default Concurrent access and locks

I'm developing a collaborative application in PHP which accesses
local files and may modify them. How can I synchronize the multiple
accesses? Note that I'm not worried about the algorithmic part of the
problem, or how to avoid conflicts, which I know how to solve; I just
need a few atomic primitive shared among any PHP processes that may be
running. I could even have a separate process serializing all operations
if such a thing is possible. flock() seems to be too limited for my needs.
Thanks a lot,

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
http://www.lsi.usp.br/~brunobg/
Reply With Quote
  #2 (permalink)  
Old 04-03-2005
ZeldorBlat
 
Posts: n/a
Default Re: Concurrent access and locks

What about flock() is too limited for your needs?

Reply With Quote
  #3 (permalink)  
Old 04-03-2005
Bruno Barberi Gnecco
 
Posts: n/a
Default Re: Concurrent access and locks

ZeldorBlat wrote:
> What about flock() is too limited for your needs?


Doesn't work over NFS and other FSs, is slow, does not
let me lock directory hierarchies (which is important for atomically
changing many files at once) or even many files atomically.

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
http://www.lsi.usp.br/~brunobg/
Reply With Quote
  #4 (permalink)  
Old 04-03-2005
mlemos
 
Posts: n/a
Default Re: Concurrent access and locks

Hello,

on 04/03/2005 11:35 AM Bruno Barberi Gnecco said the following:
> ZeldorBlat wrote:
>> What about flock() is too limited for your needs?

>
> Doesn't work over NFS and other FSs, is slow, does not


Not true. That is only a problem of some implementations.

If you are concerned with speed, you should not be using NFS anyway.


> let me lock directory hierarchies (which is important for atomically
> changing many files at once) or even many files atomically.


flock is exactly for what you need. If you want to change many files
with a single lock, you can pick a special file and lock it when you
want to access or change many files.


--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Reply With Quote
  #5 (permalink)  
Old 04-03-2005
Bruno Barberi Gnecco
 
Posts: n/a
Default Re: Concurrent access and locks

mlemos wrote:

>>> What about flock() is too limited for your needs?

>> Doesn't work over NFS and other FSs, is slow, does not

> Not true. That is only a problem of some implementations.
> If you are concerned with speed, you should not be using NFS anyway.


I'm not using it, but I wanted to support it. Let's not into
another "why NFS sucks" thread because I think we all agree with that :)

>> let me lock directory hierarchies (which is important for atomically
>> changing many files at once) or even many files atomically.

> flock is exactly for what you need. If you want to change many files
> with a single lock, you can pick a special file and lock it when you
> want to access or change many files.


Sorry, but I can't see how that solves the problem. If I create
a file containing the names of the locked files and lock it, there's
still the possibility of racing. If I use just one file for all
processes, listing all locked files, how can I know when a certain
file is unlocked?

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
http://www.lsi.usp.br/~brunobg/
It is easy when we are in prosperity to give advice to the afflicted.
-- Aeschylus
Reply With Quote
  #6 (permalink)  
Old 04-03-2005
mlemos
 
Posts: n/a
Default Re: Concurrent access and locks

Hello,

on 04/03/2005 06:18 PM Bruno Barberi Gnecco said the following:
>>>> What about flock() is too limited for your needs?
>>> Doesn't work over NFS and other FSs, is slow, does not

>> Not true. That is only a problem of some implementations.
>> If you are concerned with speed, you should not be using NFS anyway.

>
> I'm not using it, but I wanted to support it. Let's not into
> another "why NFS sucks" thread because I think we all agree with that :)


NFS does not suck. Files under NFS are remotely stored and you must deal
with the network access overhead. That could be useful in many
circumstances on which such overhead is bearable.

Your inconsistent is that while you claim flock is slow (I don't know
where did you get this idea), you are not concerned with supporting NFS
when it is much slower due to network overead.


>>> let me lock directory hierarchies (which is important for atomically
>>> changing many files at once) or even many files atomically.

>> flock is exactly for what you need. If you want to change many files
>> with a single lock, you can pick a special file and lock it when you
>> want to access or change many files.

>
> Sorry, but I can't see how that solves the problem. If I create
> a file containing the names of the locked files and lock it, there's
> still the possibility of racing. If I use just one file for all
> processes, listing all locked files, how can I know when a certain
> file is unlocked?


You can use shared locks for reading and exclusive locks for writing.


--

Regards,
Manuel Lemos

PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/

PHP Reviews - Reviews of PHP books and other products
http://www.phpclasses.org/reviews/

Metastorage - Data object relational mapping layer generator
http://www.meta-language.net/metastorage.html
Reply With Quote
  #7 (permalink)  
Old 04-04-2005
Bruno Barberi Gnecco
 
Posts: n/a
Default Re: Concurrent access and locks

mlemos wrote:

>> I'm not using it, but I wanted to support it. Let's not into
>> another "why NFS sucks" thread because I think we all agree with that :)

> NFS does not suck. Files under NFS are remotely stored and you must deal
> with the network access overhead. That could be useful in many
> circumstances on which such overhead is bearable.


NFS has several known problems. It has an awful lag (not due to
the network overhead, but to its caching), lack of a safe lock/unlock
system to avoid inconsistency, etc...

> Your inconsistent is that while you claim flock is slow (I don't know
> where did you get this idea),


The web. Since I may have to lock several hundred files in
some operations, I was worried when I read that flock was slow.
Now, if someone is running it on NFS, they are probably likely
to have a small number of files or know the potential problems.

>> Sorry, but I can't see how that solves the problem. If I create
>> a file containing the names of the locked files and lock it, there's
>> still the possibility of racing. If I use just one file for all
>> processes, listing all locked files, how can I know when a certain
>> file is unlocked?

> You can use shared locks for reading and exclusive locks for writing.


I still can't get it.
From what I understood of your solution, I could keep a separate
metafile with entries for all files that are currently "locked", and flock
only this file. Suppose now that process A locks "file1" for reading, and
process B wants to write to "file1". Do I need to stat the metafile
manually every N milliseconds to check when process A released the lock?

--
Bruno Barberi Gnecco <brunobg_at_users.sourceforge.net>
http://www.lsi.usp.br/~brunobg/
It is easier to fight for one's principles than to live up to them.
-- Alfred Adler
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:03 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0