On 6/6/06, Jeff Pang <[EMAIL PROTECTED]> wrote:
Hello,lists,

See these code piece please:

    while ($cycles--)
    {
        my $c;

        eval {
            local $SIG{ALRM} = sub { die "flock timeout" };
            alarm 3;

            open (SOCKLOCK,SOCK_LOCK) or die "open lock file fail: $!";
            flock (SOCKLOCK,LOCK_EX);

            die "can't accept" unless $c = $sock->accept;

            flock (SOCKLOCK,LOCK_UN);
            close SOCKLOCK;

            alarm 0;
        };

        next if $@;

        my $line = <$c>;
        close $c;

        do_real_thing($line) if $line;
    }


In above codes,I flock a file to avoid childs competing to call the 
'accept'.But since there are so many childs (my socket server accept about 200 
clients' connections),so each child maybe wait somewhat long time to obtain the 
file-lock.So I call the 'alarm' to set a timer.After waiting for 3 seconds but 
the child still can't get the file-lock,it would timeout and die the 'eval'.Now 
the codes run normally,but I run 'strace -p 1234' (where '1234' is one of the 
childs' pid) under Linux shell,I see this output last out for some seconds 
(much more than 3 seconds):

$ strace -p 28373
Process 28373 attached - interrupt to quit
flock(6, LOCK_EX

It seems that the 'alarm' don't get executed.Why this happen?Thanks.


--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




You may want to use the non-blocking flock instead:
my $tries;
for $tries (0 .. $maxtries) {
   break unless flock (SOCKLOCK,LOCK_EX | LOCK_NB);
}
die "Could not get an exclusive lock: $!" if $tries == $maxtries;

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to