flock()总是返回true

Been over this for the last hour, and cant really figure out whats wrong.

This is the code I was using to debug the issue.

Monitor.php

$fp = fopen("lock.txt", "r+");
$var=flock($fp, LOCK_EX | LOCK_NB);
var_dump($var);
$var=flock($fp, LOCK_SH | LOCK_NB);
var_dump($var);
exit;

locker.php

$fp = fopen("lock.txt", "r+");
sleep(60);

To see if flock is working properly, I first run the locker.php, so that file will be locked for 60 seconds, and I then try running the monitor.php to see if I can get a lock.

I get TRUE for both exclusive and shared lock.

What would be the problem?

Solution : Assumed opening the file would lock it. The locker should issue an flock to lock the file.

Missed that part in the hurry. Deadlines.Deadlines :)

locker.php is not locking the file. fopening a file will not lock it. flock is a cooperative advisory locking system. All parties need to participate in it and use flock to lock and/or check for locks. Unless locker.php explicitly flocks the file it is not locked.

Under PHP (and many UNIX systems), flock is advisory, meaning that all processes that want to use the file must use locking. Those that don't can still do whatever they want to the file and will not prevent other processes from obtaining locks.

Your locker program should both open and lock the file, such as:

$fp = fopen("lock.txt", "r+");
$var=flock($fp, LOCK_SH | LOCK_NB);
sleep(60);