By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,592 Members | 1,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,592 IT Pros & Developers. It's quick & easy.

flock() and NFS

P: n/a
Newbie [PHP] here.

I interpret the fine print at
http://us2.php.net/manual/en/function.flock.php
to mean that flock() does not work where the file being locked is
on an NFS filesystem.

The commentary following the "official" description of flock()
offers numerous circumventions broken flock() where the file
needing atomic access is on an NFS filesystem.

One comment offers a PHP function that creates a temporary file in
the same directory (and on the NFS filesystem) as the target of
the write with some code for ifexists to assure concurrency. Then
another poster says that fopen() is not atomic, that mkdir() is
atomic.

There is much discussion of process interruption while holding the
lockfile in these functions where to concequence of the
interruption is a blocked site. The discussion continues with how
to detect an interrupted process and recovery via detecting
filetimes on the lockfile.

Do any of these actually work? What's the truth?
--
The name, "Rex Karz" is a pseudonym. The From: email address
is a spam trap. Messages sent to that address may cause the
sender's IP-address to be listed in one or more DNSBls.

Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Rex Karz" <Re*****@gmail.com> wrote in message
news:11*************@news.supernews.com...
Newbie [PHP] here.

I interpret the fine print at
http://us2.php.net/manual/en/function.flock.php
to mean that flock() does not work where the file being locked is on an
NFS filesystem.

The commentary following the "official" description of flock() offers
numerous circumventions broken flock() where the file needing atomic
access is on an NFS filesystem.

One comment offers a PHP function that creates a temporary file in the
same directory (and on the NFS filesystem) as the target of the write with
some code for ifexists to assure concurrency. Then another poster says
that fopen() is not atomic, that mkdir() is atomic.

There is much discussion of process interruption while holding the
lockfile in these functions where to concequence of the interruption is a
blocked site. The discussion continues with how to detect an interrupted
process and recovery via detecting filetimes on the lockfile.

Do any of these actually work? What's the truth?
--
The name, "Rex Karz" is a pseudonym. The From: email address
is a spam trap. Messages sent to that address may cause the
sender's IP-address to be listed in one or more DNSBls.


NFS or SMB shares do not support file locking, and I would guess, since not
even the Berkeley DB can do file locking on network shares, that it is not
possible.

Use a lock file on the local filesystem instead, or any other locking
mechanisms, such as semaphores or locks in a database if your files that
needs to be locked may be accessed from multiple hosts.

Concurrency equals head ache...

Best Regards.

Peter Albertsson
Jul 17 '05 #2

P: n/a
Peter Albertsson wrote:
"Rex Karz" <Re*****@gmail.com> wrote in message
news:11*************@news.supernews.com...
Newbie [PHP] here.

I interpret the fine print at
http://us2.php.net/manual/en/function.flock.php
to mean that flock() does not work where the file being locked is on an
NFS filesystem.


NFS or SMB shares do not support file locking, and I would guess, since
not even the Berkeley DB can do file locking on network shares, that it is
not possible.


Oh yes they do. Using 'flock()', only the machine on which the function is
called knows about the lock. Both protocols support locking though. IIRC
PHP's flock is a straight wrapper around the POSIX flock() function. PHP
provides access to the fcntl() function (see Direct I/O) which will lock
over NFS (depending on implementation of NFS).....but for practicality it's
simpler to consider that conventional locking is not available.
There is much discussion of process interruption while holding the
lockfile in these functions where to concequence of the interruption is a
blocked site. The discussion continues with how to detect an interrupted
process and recovery via detecting filetimes on the lockfile.


Do the sums. How probable is contention? If it's even remotely possible that
you may have contention in the time it takes to setup a lockfile, then you
should be able to afford to look at a different solution.

C.
Jul 17 '05 #3

P: n/a
Rex Karz <Re*****@gmail.com> writes:

One comment offers a PHP function that creates a temporary file in the
same directory (and on the NFS filesystem) as the target of the write
with some code for ifexists to assure concurrency. Then another poster
says that fopen() is not atomic, that mkdir() is atomic.
Is there an official documentation which says that mkdir is guaranteed
to be atomic ? You best bet seems to be to

- create a lock_status table
- add a column in it (running_myscript)
- check the value of running_myscript column. If it is true, then exit
- if not true then update that column to true
- Do your work
- set running_myscript to false
- exit

Do any of these actually work? What's the truth?


The answer is 42 :-)

--
Raj Shekhar Y! : Operations Engineer
MySQL DBA, programmer and slacker Y!IM : lunatech3007
home : http://rajshekhar.net blog : http://rajshekhar.net/blog/
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.