[comp.lang.perl] alarm

hrjoist@medusa.informatik.uni-erlangen.de (Holger Joist) (11/29/90)

Hello!

Does anybody know, how to do a 'alarm(secs)' system call in perl?
-- 
Name  : Holger Joist
E-Mail: hrjoist@medusa.informatik.uni-erlangen.de

tchrist@convex.COM (Tom Christiansen) (11/30/90)

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 <tchrist@convex.com>
#
#	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);
} 

emv@ox.com (Ed Vielmetti) (12/01/90)

In article <109564@convex.convex.com> tchrist@convex.COM (Tom Christiansen) writes:

   Speaking of frequently asked questions, here are the new ones 
   for this month (I'll post RSN)....
   Any other candidates or suggestions?

A nice example of how to load data into an associative array with some
kind of <<ENDOFTABLE construct would be welcomed (if it's not already
in there).  Or several associative arrays, for that matter.  I'd like
to have some way of taking a table

news/readers	rn,nn,trn
news/nntp	nntp,nntpxmit,nntplink
gnu		binutils,flex,fileutils
x11/games	xtrek
lwall		rn,patch,perl
ENDOFTABLE

and turning it into

$category{"rn"} -> "news/readers,lwall"
$category{"nntpxmit"} -> "news/nntp"
$category{"binutils"} -> "gnu"

--Ed