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

flock won't non-block at all

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.