Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

$SIG{'ALRM'} on FreeBSD

by b888 (Beadle)
on Feb 24, 2006 at 10:21 UTC ( [id://532508]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings.

I have written server application on perl and everything looked fine utill Linux to FreeBSD migration. Now the same code seems to work in different way sometimes.

while( 1 ){ my $paddr = accept( Client, Server ); next if not $paddr; my $buf = ''; eval { local $SIG{'ALRM'} = sub { die "time\n" }; Time::HiRes::alarm( $READ_TIMEOUT ); recv Client, $buf, $MAX_REQ_LEN, 0; Time::HiRes::alarm( 0 ); }; if( $@ ){ # operation timeout / met errors d 'Timeout on read'; close Client; next; } ...

I've redirected stdout and stderr in file and sometimes the string "time" appear in it + server goes away. Wonder why that happens and how to avoid that? Thanks :)

p.s.
v5.8.7 built for i386-freebsd-64int
FreeBSD 6.0-RELEASE FreeBSD 6.0-RELEASE #0: Wed Feb 8 13:33:28 UTC 2006

Replies are listed 'Best First'.
Re: $SIG{'ALRM'} on FreeBSD
by sh1tn (Priest) on Feb 24, 2006 at 11:46 UTC
    Seems to work fine:
    perl -e '$SIG{ALRM} = sub { die "signal alarm caught!\n" }; kill 14, $ +0'

    STDERR: signal alarm caught!

    `uname -a`: FreeBSD zuluz 6.0-RELEASE FreeBSD 6.0-RELEASE #0: Thu Nov  3 09:36:13 UTC 2005     root@x64.samsco.home:/usr/obj/usr/src/sys/GENERIC  i386


      Yes, that works for me to :) The most strange thing is that it falls under some conditions. Sometimes beyond 1 or 2 days, sometimes in a week of work it falls. And that is what i'm trying to understand. Why?

      p.s. Average load is 10-30 request per second.

        Average load is 10-30 request per second.

        If I had that kind of load, I think I'd be tempted to use Net::Server::Prefork. Call me crazy.

Re: $SIG{'ALRM'} on FreeBSD
by jonadab (Parson) on Feb 24, 2006 at 13:19 UTC

    Did you copy and paste that code, or attempt to retype it?

    $ perl temp.pl String found where operator expected at temp.pl line 13, near "d 'Time +out on read'" (Do you need to predeclare d?) syntax error at temp.pl line 13, near "d 'Timeout on read'" Missing right curly or square bracket at temp.pl line 16, at end of li +ne syntax error at temp.pl line 16, at EOF Execution of temp.pl aborted due to compilation errors.

    update: Ah, now (with your sub d) I get 100% CPU utilization, for much longer than a second, which I guess is probably similar to what you're getting, and which is presumably not what you want. So yeah, it appears the problem is not just something weird with your local setup, which is what I was trying to confirm. I threw in an extra warn and confirmed that $paddr is always undef, but I imagine what you're trying to do is limit execution time when there's no connection. Hmmm... select undef,undef,undef,$delay can measure fractional parts of a second, but I don't know how small its resolution goes. That would also stop you from consuming 100% CPU, though: just loop through $n tries with $totaltimelimit/$n delay each time. Would that work?

    $ perl -v This is perl, v5.8.7 built for i386-freebsd-64int (with 2 registered patches, see perl -V for more detail) Copyright 1987-2005, Larry Wall Perl may be copied only under the terms of either the Artistic License + or the GNU General Public License, which may be found in the Perl 5 source ki +t. Complete documentation for Perl, including FAQ lists, should be found +on this system using `man perl' or `perldoc perl'. If you have access to + the Internet, point your browser at http://www.perl.org/, the Perl Home Pa +ge.

    I wanted to help you out by testing your code on another FreeBSD system, but I think there's more wrong than $SIG{ALRM} only.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. Why, I've got so much sanity it's driving me crazy.

      Well, i thought the mistake is in my approach of usage of alrm signal, that's why i pasted only part where i used it. Sorry :) In fact i can't simulate that situation myself. But it happend on 3 servers already (all under freebsd) and not even on 1 from 4 under Linux.

      Maybe you can suggest me another way of usage of timeout-ed operations? Means when need to limit time of execution of some subs. And limit for time smaller then second.

      p.s. my "d" sub :)

      sub time_stamp { my $time = shift; $time = time() if ! defined $time; my ($sec, $min, $hour, $mday, $mon, $year) = localtime($time); return sprintf("%02d:%02d:%02d %02d/%02d/%04d", $hour, $min, $sec, $mday, $mon+1, $year+1900); } sub d { print "[".time_stamp()."] ".(caller(0))[0].", "; print shift; print "\n"; }

        After some experimentation, I have concluded that on my system the resolution of select undef,undef,undef,$fractionalsecond goes down to something on the order of 1/300 of a second, give or take. This is an estimate, and I haven't done proper benchmarks, and YMMV.

        perl -e ' my $times = 5000; print "Starting: " . localtime() . "\n"; for (1..$times) { select undef,undef,undef,1/$times; } print "Done: " . localtime() . "\n"; ' Starting: Fri Feb 24 10:54:56 2006 Done: Fri Feb 24 10:55:06 2006

        About ten seconds to do 5000 iterations, would lead me to say 500 iterations per second would be the limit (on this system), but if I set $times to 300 it takes a full second or slightly more, so there's some overhead making the calculation imprecise. HTH.HAND.

Re: $SIG{'ALRM'} on FreeBSD
by samizdat (Vicar) on Feb 24, 2006 at 20:03 UTC
    http://www.freebsd.org/cgi/getmsg.cgi?fetch=736507+739644+/usr/local/w +ww/db/text/2006/freebsd-stable/20060219.freebsd-stable > > interrupt total rate > > irq0: clk 203894894 1000 a clock rate of 1000 Hz is probably to high for a 486 class cpu. You should add "options HZ=100" to your kernel config to get back to the 100 Hz that where default before 6.0 Wolfgang
    One of the recent changes (as of 6.0) to FreeBSD has been a change in the polling frequency, given the huge increase in CPU speed of modern processors. Dunno for sure, but this may affect the signal-propagation as well as interrupt processing.

    Don Wilde
    "There's more than one level to any answer."
Re: $SIG{'ALRM'} on FreeBSD
by acid06 (Friar) on Feb 24, 2006 at 14:02 UTC
    Signals are unrealiable and non-portable.
    I've seen this sort of non-deterministic signals behaviour when under stress also on Linux. Plus, if you ever plan to run this under Win32, forget it.

    The best advice I can give is to try not using signals and to do it some other way around.


    acid06
    perl -e "print pack('h*', 16369646), scalar reverse $="

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://532508]
Approved by GrandFather
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found