note 59197 deleted from function.flock by nlopess
| From: | nlopess@php.net | Date: | Tue, 29 Nov 2005 18:41:50 +0000 |
| Subject: | note 59197 deleted from function.flock by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99316@lists.php.net to get a copy of this message | ||
Note Submitter: Rafa JT (Tudela-NAVARRA)
----
PHP-CGI dont works like Perl with flock() under LINUX.
In a file already locked by flock() in a php currently executing process, if you want to write in it
with another php process but you want to check if that file is lock, then you must to include newest
flock before to try write in it, because if you dont do it the new write process merges the result
with the old process.
This command flock() added in the second process wait the execution until the first process ends or
the flock($filehandle,LOCK_UN) has been executed.
To Install:
- mkdir /root/flock (the dir where we will install this files)
- Create these programs in /root/flock with (chown root.root and chmod 700)
- Open 2 tty windows for access (root) to a linux system
To check PERL (this takes 30 secs for each test):
- Access to the /root/flock directory in both ttys
- In tty-1 execute... ./flock.pl A 1 (writes "A" Locking the file)
- Quickly while the 1st process is in execution, in tty-2 execute... ./flock.pl B (writes
"B" without lock the file)
- Type... "cat perl.txt" and you will get a result like this
AAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBB
To check PHP (this takes 30 secs for each test):
- Access to the /root/flock directory in both ttys
- In tty-1 execute... ./flock.php A 1 (writes "A" Locking the file)
- Quickly while the 1st process is in execution, in tty-2 execute... ./flock.php B (writes
"B" without lock the file)
- Type... "cat php.txt" and you will get a result like this
AAAAAAAABABABABABABABABABABABABABABABABABABABABABABABABBBBBBBB
file: flock.php
------------------------------------
#!/usr/bin/php
<?
$arg1=$_SERVER['argv'][1];
if(isset($_SERVER['argv'][2])){$arg2=$_SERVER['argv'][2];} else {$arg2=false;}
$myfile="/root/flock/php.txt";
echo "FLOCK EN PHP\n";
$fp = fopen($myfile, 'a') or die("NO PUEDO ABRIR EL FICHERO\n");
print "Archivo abierto...\n";
if($arg2){flock($fp, 2 ) or die("NO SE PUEDE BLOQUEAR");print "ARCHIVO
BLOQUEADO\n";}
echo "Escribiendo: ";
for ($f=0; $f<=30; $f++){ fwrite($fp, $arg1); echo "$arg1-";sleep(1); }
echo "\n";
if($arg2){flock($fp, 6 );print "ARCHIVO DES-BLOQUEADO\n";}
fclose($fp);
print "Archivo cerrado...\n";
?>
------------------------------------
file: flock.pl
------------------------------------
#!/usr/bin/perl
$myfile="/root/flock/perl.txt";
$|=1;
$arg1=$ARGV[0];
$arg2=$ARGV[1];
print("FLOCK EN PERL\n");
open($fh,">>$myfile");
print("Archivo abierto\n");
if($arg2){flock($fh,2) or die("NO SE PUEDE BLOQUEAR");print("Archivo
BLOQUEADO\n");}
for($f=1;$f<30;$f++){ print($fh $arg1); print($arg1."-"); sleep(1);}
print("\n");
if($arg2){flock($fh,6);print("Archivo DESBLOQUEADO\n");}
close($fh);
print("Archivo cerrado\n");
exit;
------------------------------------