A Perl question (fork,flock)...

Moshiul Shovon (shovon@gte.com)
Fri, 16 Aug 1996 15:51:32 -0500


Hello folks,

I am having a little problem with flock(). Here is a small script that
regenerates the problem:

----------------cut here---------------
#!/usr/local/bin/perl

$LOCK_SH=1;
$LOCK_EX=2;
$LOCK_NB=4;
$LOCK_UN=8;

sub lock{
        flock(fh, $LOCK_EX);
        seek(fh,0,2);
        }
sub unlock{     
       flock(fh,$LOCK_UN);
        }       
        
print "Started $0: ...\n";
open(fh,'>tmp.delete.me');

fork();
$counter=1000;
while($counter--){
    lock();
    print fh "$0: My pid is $$ \n";
    print "My pid is $$ \n";
    unlock();
    #sleep(1);
}
------------------Cut here--------------------------

The problem is: if one process bocomes blocked, it does not get
unblocked. However, if I put the sleep(1) statement, things look a lot
better. But putting a sleep(1) in my real script is not desirable. How
can I make sure all the processes get their share to write to the file?

I tested this script on HPUX-9.05, perl 5.003. You may run the script
above to see if you have the same problem or not.

Thanks for your help...


-- 


==========================================================================
Moshiul Shovon
Technical Staff Member - Development & Integration
GTE Laboratories Incorporated                       shovon@gte.com
2101 North Denton Drive                             214-242-4515
Carrollton, TX 75006
==========================================================================