Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 199901

Re: [PHP3] File Locking From: Sascha Schumann (sas <email protected>)
Date: 01/23/99

On Fri, Jan 22, 1999 at 08:05:44PM -0800, Dave Walton wrote:
> On Jan 20, Jum Winstead wrote:
> >
> > Another thing that is useful to know: mkdir() provides an
> > essentially atomic file-check-and-create function that could be
> > used for coordinated locking.
>
> Interesting idea. I'll try to remember that next time I need locking...

Or get the one True Source of information. Synchronization (including record
locking) is described in detail in Stevens' Unix Network Programming, Vol II.

> > Obviously this only is useful if all of
> > the applications that are accessing the file that needs such
> > locking coordinate using this method.
>
> But, as we've already established, the same is true of flock().

... it's true for every kind of advisory locking. Unfortunately, mandatory
locking which could be the solution for synchronizing between uncooperating
processes is not defined in Posix.1 nor Unix 98.

> > It seems that none of the core (or otherwise very active)
> > developers of PHP have a need for such file locking because they
> > have found better solutions.

Its name is Perl. Perl is available on probably 99% of all Unix hosts.

> If that is true, and there are other, better solutions that are equally
> simple, then perhaps the real problem is that these other solutions
> just aren't well enough known. mkdir() is one example that I hadn't
> thought of. Got any others?

Synchronization is a wide spread theme. Stevens describes the following
techniques in the above mentioned book:

 - Mutexes and condition variables
 - Read-Write locks (thread specific)
 - Record Locking (fcntl stuff)
 - Posix and Sys V semaphores

It's a very, very bad idea to try to use mkdir() for synchronizing between
processes.

- the lock is not released automatically, but your programs have to check each
  time, if the process holding the lock is still alive (more code, more bugs)
- mkdir is not guaranteed to be atomic (race condition)

A related technique is to use open(...,O_EXCL) which is an _atomic_ method of
saying

fd = open(..., O_EXCL);
flock(fd, LOCK_EX);

If all other methods of locking were not available, I'd use the above method,
because it does not have the problems of mkdir.

-- 
                     
          Regards,
						                                
                            Sascha Schumann | 
                                 Consultant | finger sas <email protected>
                                            | for PGP public key

-- PHP 3 Mailing List http://www.php.net/ To unsubscribe send an empty message to php3-unsubscribe <email protected> To subscribe to the digest list: php3-digest-subscribe <email protected> For help: php3-help <email protected> Archive: http://www.php.net/mailsearch.php3 List administrator: zeev-list-admin <email protected>