473,324 Members | 1,678 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,324 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 4752
"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
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.