473,387 Members | 1,516 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,387 software developers and data experts.

flock won't non-block at all

Hi there,

I'm trying to just prevent multiple instances of a script running.

I would have thought that the following code would do this for me.

<php
$fp = fopen("foo.txt", "w");
flock($fp, LOCK_EX + LOCK_NB) or die ("Could not get lock!\n");
echo "Got lock!\n";
sleep(10);
flock($fp, LOCK_UN);
?>

The first script to run would obtain the lock on the file, and would
execute.
Any other scripts would fail immediately (because of my non-blocking
LOCK_NB).

But it seems that ALL of my scripts just wait, and ALL of them end up
printing "Got lock!"

Can anyone help me out?

I am on winXP with php5.

Cheers,
Sven.

Sep 21 '06 #1
7 4464
I think you need to open the file with w+ e.g. $fp = fopen("foo.txt",
"w+");

Give it ago see what happends, I might be wrong.

sv***********@gmail.com wrote:
Hi there,

I'm trying to just prevent multiple instances of a script running.

I would have thought that the following code would do this for me.

<php
$fp = fopen("foo.txt", "w");
flock($fp, LOCK_EX + LOCK_NB) or die ("Could not get lock!\n");
echo "Got lock!\n";
sleep(10);
flock($fp, LOCK_UN);
?>

The first script to run would obtain the lock on the file, and would
execute.
Any other scripts would fail immediately (because of my non-blocking
LOCK_NB).

But it seems that ALL of my scripts just wait, and ALL of them end up
printing "Got lock!"

Can anyone help me out?

I am on winXP with php5.

Cheers,
Sven.
Sep 22 '06 #2
NC
sv***********@gmail.com wrote:
>
I'm trying to just prevent multiple instances of a script running.
....
I am on winXP with php5.
Read the documentation:

On some operating systems flock() is implemented at the process
level. When using a multithreaded server API like ISAPI you may
not be able to rely on flock() to protect files against other PHP
scripts running in parallel threads of the same server instance!

http://www.php.net/flock

This is the bad news. The good news is that you don't necessarily
need flock() for this. Here's a possible workaround:

$file = 'foo.txt';
if (file_exists($file)) {
$time = date('Y-m-d H:i:s', trim(file_get_contents($file)));
die('Another instance is running since ' . $time);
}
$fp = fopen($file, 'w');
fwrite($fp, time());
fclose($fp);
// the script goes here...
unlink($file);

Cheers,
NC

Sep 22 '06 #3
On 22 Sep 2006 09:30:54 -0700, "NC" <nc@iname.comwrote:
>This is the bad news. The good news is that you don't necessarily
need flock() for this. Here's a possible workaround:

$file = 'foo.txt';
if (file_exists($file)) {
$time = date('Y-m-d H:i:s', trim(file_get_contents($file)));
die('Another instance is running since ' . $time);
}
$fp = fopen($file, 'w');
Although the fun then begins when you hit the (admittedly short-duration) race
condition here...
>fwrite($fp, time());
fclose($fp);
// the script goes here...
unlink($file);
... or the never-released lock when the script dies before reaching here :-(

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 22 '06 #4
NC
Andy Hassall wrote:
>
$fp = fopen($file, 'w');

Although the fun then begins when you hit the (admittedly
short-duration) race condition here...
You are absolutely right, but how else could you work around the file
locking issue on Windows?

Cheers,
NC

Sep 23 '06 #5
On 23 Sep 2006 09:46:38 -0700, "NC" <nc@iname.comwrote:
>Andy Hassall wrote:
>>
$fp = fopen($file, 'w');

Although the fun then begins when you hit the (admittedly
short-duration) race condition here...

You are absolutely right, but how else could you work around the file
locking issue on Windows?
Not sure whether there's anything convenient in PHP itself that'll give you a
per-thread reliable and atomic lock. The win32 API has plenty of
synchronisation functions that would work, perhaps one could be called using
the win32api extension?

In some situations where I've needed locks and have had to work across Windows
and NFS filesystems (which have their own file locking quirks) I've used locks
within a database (user-defined locks in Oracle) - but in those cases I was
connecting to a database already.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Sep 24 '06 #6
Andy Hassall wrote:
>
$fp = fopen($file, 'w');

Although the fun then begins when you hit the (admittedly
short-duration) race condition here...
You are absolutely right, but how else could you work around the file
locking issue on Windows?

Not sure whether there's anything convenient in PHP itself that'll give you a
per-thread reliable and atomic lock. The win32 API has plenty of
synchronisation functions that would work, perhaps one could be called using
the win32api extension?

In some situations where I've needed locks and have had to work across Windows
and NFS filesystems (which have their own file locking quirks) I've used locks
within a database (user-defined locks in Oracle) - but in those cases I was
connecting to a database already.
Thanks for the tips guys, here's a rundown of the end result:
It turns out that whenever I was testing my little example above, I was
opening the script through a browser. For some reason the flock() never
seems to work for me this way, however when I ran it from the
command-line php things sorted themselves out. My application was
going to run in the background anyway, so I've ended up getting it to
work by having my web php call command-line php as a background
process.

The file check/create/unlink option is a good one, but my script is one
that will fire occassionaly, but will sit in a while loop for hours at
a time. So, the dead-script problem is a real one here.

Cheers for the input people,
Sven.

Sep 26 '06 #7
NC
Sven wrote:
>
It turns out that whenever I was testing my little example
above, I was opening the script through a browser. For
some reason the flock() never seems to work for me this
way, however when I ran it from the command-line php
things sorted themselves out.
Well, the reason is that inder Windows, scripts executed by an HTTP
server (such as Apache or IIS) run in threads, so it's possible for
multiple instances of a script to run under the same process ID (even
though the thread ID is different). With command-line execution, no
threading takes place; each instance has its own process ID...

Cheers,
NC

Sep 26 '06 #8

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...
3
by: Rex Karz | last post by:
Newbie 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...
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...
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
6
by: Bill H | last post by:
I am wondering if this is possible. I have a file that is accessed by multiple users and keeps track of activity (the file is polled by flash every 2 seconds). As users leave, the flash program...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...

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.