in reply to Re^3: Ensuring only one copy of a perl script is running at a time
in thread Ensuring only one copy of a perl script is running at a time
I find extremely rude posts that do nothing but contradict the parent when the poster could have verified that the parent was correct.
You're wrong. flock does NOT use its own advisory locking. If flock implemented some kind of advisory locking, Windows's type command would know nothing of it, yet it can't read a file locked using flock.
use Fcntl qw( :flock ); open(my $fh, '>', 'file') or die("Unable to open file: $!\n"); print $fh ("ok\n"); print("type without lock:\n"); system("type file"); print("\n"); flock($fh, LOCK_EX|LOCK_NB) or die("Unable to lock file: $!\n"); print("type with lock:\n"); system("type file"); unlink('file');
outputs
>perl 590735.pl type without lock: ok type with lock: The process cannot access the file because another process has locked +a portion of the file.
Tested with 5.6.0, 5.6.1, 5.8.0 and 5.8.8.
Update: Updated to show that type works when the file isn't locked by flock.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^5: Ensuring only one copy of a perl script is running at a time
by tye (Sage) on Dec 19, 2006 at 18:41 UTC | |
by ikegami (Patriarch) on Dec 19, 2006 at 18:59 UTC |