note 59197 added to function.flock

From: Date: Tue, 29 Nov 2005 11:31:27 +0000
Subject: note 59197 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-99288@lists.php.net to get a copy of this message
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; ------------------------------------ ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59197 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59197&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59197&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99288) next »