http://qs321.pair.com?node_id=826813

bichonfrise74 has asked for the wisdom of the Perl Monks concerning the following question:

The code below prevents having itself run more than once. The code is working in the sense that if you try to run it more than once, you will get a message of 'Another $script is already running.'.

But if you uncomment the "Check_Race_Condition()", and comment out the lines following it -- "open and flock", and try to run the code more than once, it will not give any error message. This means that the script is able to run more than once.

I'm not sure why this is happening... Am I missing something obvious?
#!/usr/bin/perl use warnings; use strict; use Fcntl qw( :flock ); print "Script $0 is running now.\n"; # Check_Race_Condition(); open( my $self, '<', $0 ) or die( "Another $0 is already running.\n"); flock( $self, LOCK_EX | LOCK_NB ) or die( "Another $0 is already running.\n"); sleep( 100 ); print "End.\n"; sub Check_Race_Condition { open( my $self, '<', $0 ) or die( "Another $0 is already running.\n"); flock( $self, LOCK_EX | LOCK_NB ) or die( "Another $0 is already running.\n"); }