flock() and NFS

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Newbie [PHP] here.

I interpret the fine print at
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

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.

Re: flock() and NFS

Quoted text here. Click to load it

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

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

Re: flock() and NFS

Peter Albertsson wrote:

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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.


Re: flock() and NFS

Quoted text here. Click to load it

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

Quoted text here. Click to load it

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 /

Site Timeline