Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!newstop!texsun!letni!mic!convex!convex.COM From: tchrist@convex.COM (Tom Christiansen) Newsgroups: comp.lang.perl Subject: Re: alarm() Message-ID: <109564@convex.convex.com> Date: 30 Nov 90 07:17:24 GMT References: <3294@medusa.informatik.uni-erlangen.de> Sender: news@convex.com Reply-To: tchrist@convex.COM (Tom Christiansen) Organization: CONVEX Software Development, Richardson, TX Lines: 110 In article <3294@medusa.informatik.uni-erlangen.de> hrjoist@medusa.informatik.uni-erlangen.de (Holger Joist) writes: >Does anybody know, how to do a 'alarm(secs)' system call in perl? 1. Wait for the patch that has alarm() as a built-in. 2. If your system has [sg]etitimer() system calls, use them with syscall. 3. I'm a BSD brat, so can't help you if #2 doesn't apply. 4. Nice to see Perl being used in Europe. Here are my itimer manipulation routines again. I promise not to post them more than once a month. :-) Speaking of frequently asked questions, here are the new ones for this month (I'll post RSN). 22) How can I manipulate fixed-record-length files? 23) How can I make a file handle local to a subroutine? 24) How can I extract just the unique elements of an array? I'm still hesitant on adding: xx) What's the best way to see if an array element is present? xx) How do I call alarm(3C)? Any other candidates or suggestions? --tom # itimers.pl - timer manipulation functions # written by tom christiansen # # getitimer, setitimer - like syscalls but return true on success # NB: require packed data for args # # itimer - conversion function for packing and # unpacking itimers. packs in scalar context, # unpacks in array context. # # alarm - like libc call but can take and returns floats # require 'sizeof.ph'; require 'syscall.ph'; require 'sys/time.ph'; # # careful: implementation dependent! # $itimer_t = 'L4'; # itimers consist of four longs $sizeof{'itimer'} = '16' unless defined $sizeof{'itimer'}; # from sizeof.ph? ########################################################################### # itimer conversion function; this one goes both ways # sub itimer { if (wantarray) { warn "itimer: only expected one arg in array context" if $#_; warn "itimer: itimer to unpack not length ".$sizeof{'itimer'} unless length($_[0]) == $sizeof{'itimer'}; return unpack($itimer_t, $_[0]); } else { return pack($itimer_t, $_[0], $_[1], $_[2], $_[3]); } } ########################################################################### sub setitimer { local($which) = shift; local($retval); die "setitimer: input itimer not length ".$sizeof{'itimer'} unless length($_[0]) == $sizeof{'itimer'}; $_[1] = &itimer(0,0,0,0); syscall(&SYS_setitimer, $which, $_[0], $_[1]) != -1; } ########################################################################### sub getitimer { local($which) = shift; $_[0] = &itimer(0,0,0,0); syscall(&SYS_getitimer, $which, $_[0]) != -1; } ########################################################################### # # alarm; send me a SIGALRM in this many seconds (fractions ok) # # sub alarm { local($ticks) = @_; local($itimer,$otimer); local($isecs, $iusecs, $secs, $usecs); $secs = int($ticks); $usecs = ($ticks - $secs) * 1e6; $otimer = &itimer(0,0,0,0); $itimer = &itimer(0,0,$secs,$usecs); &setitimer(&ITIMER_REAL, $itimer, $otimer) || warn "alarm: setitimer failed: $!"; ($isecs, $iusecs, $secs, $usecs) = &itimer($otimer); return $secs + ($usecs/1e6); }