473,441 Members | 1,805 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

flock() and NFS

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
3 4761
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marc | last post by:
Hello, when using flock() I get a permission denied error: Warning: fopen("<filename>", "r+") - Permission denied in <pathtofile> on line 7 I do this: $fileToOpen=substr($PHP_SELF,...
0
by: Daniel Brunthaler | last post by:
hi, i've a problem with file locking. seems that flock() and gzopen() do not work together. i get the error (or warning) message ... Warning: flock(): cannot represent a stream of type ZLIB...
14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
4
by: writeson | last post by:
Hi all, I've got a PHP program that I've added flock() to in order to protect multiple scripts trying to write to the same file. After I added the flock() to the code the performance of the code...
3
by: siliconmike | last post by:
I've scoured over manual page for flock on http://in2.php.net/flock but I can't understand a few things: 1. will flock wait until a different process releases the lock ? 2. please clarify this...
3
by: thakadu | last post by:
The following code works as expected when run in the main body of a python script (ver 2.3.5) on OpenBSD v3.8. but when it is in the body of a function definition it does not work. It does not...
4
by: Time Waster | last post by:
Is this a stupid use of flock: FILE *fp=fopen(SOME_FILE_CONSTANT,"r+"); flock(fileno(fp),LOCK_EX); something important here, including reads and a write to fp flock(fileno(fp),LOCK_UN);...
2
by: xucs007 | last post by:
I ran following 2 programs (lock1, lock2) at almost same time, to write either "123456", or "222" to file "aaa" at the same time. But I often just got "222456" in "aaa" . Is this a bug of python...
1
by: Seb | last post by:
I'm trying to implement a file server using the code below. However the locking doesn't work. I can delete while put'ing a file. Anyone got an idea about why? best regards, seb
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.