Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!elroy.jpl.nasa.gov!jpl-devvax!lwall From: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Newsgroups: comp.lang.perl Subject: Re: itimers Message-ID: <7077@jpl-devvax.JPL.NASA.GOV> Date: 15 Feb 90 01:55:40 GMT References: <5334@convex.convex.com> Reply-To: lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) Organization: Jet Propulsion Laboratory, Pasadena, CA Lines: 140 In article <5334@convex.convex.com> tchrist@convex.com (Tom Christiansen) writes: : I've worked up [sg]etitimer routines and an alarm(), but when : I run my test case, this is the output I get: : : timeleft is 0, 0 : setting alarms at Sun Feb 11 12:18:04 CDT 1990 : Bad free() ignored at itimers/ttimer line 61. : alarm: setitimer failed: Invalid argument at itimers/ttimer line 79. : alarm had 0 secs left : : This means the getitimer was happy, but then there was some : internal glitch and then the setitimer failed. : : Larry, is the failed system related to the bad free(), or am : I miscalling the system call someway as it would have me believe? You're miscalling the system call. The two timer values are 4 longs long, not 2, since each one has both an interval and a value, and each of those has both a seconds and a microseconds. Passing a short string for syscall to write over could well cause a bad free, depending on which malloc package you use. : Also, ttimer hasn't got 79 lines, why does it say it does? I'm not sure. Part of the problem is that it should say timers.pl, not ttimer, since the warn is over there. Try the following. Larry Wall lwall@jpl-devvax.jpl.nasa.gov #!/bin/sh : make a subdirectory, cd to it, and run this through sh. echo 'If this kit is complete, "End of kit" will echo at the end' echo Extracting timers.pl sed >timers.pl <<'!STUFFY!FUNK!' -e 's/X//' X# X# timer manipulation functions X# X# getitimer X# setitimer X# alarm X Xdo 'source.pl' unless defined &source; Xdo source('syscall.h') unless defined &SYS_setitimer; Xdo source('sys/time.h') unless defined &ITIMER_REAL; X X# X# careful: implentation dependent! X# X$itimer_t = 'L4'; # itimers consist of four longs X$sizeof{'itimer'} = '16' unless defined $sizeof{'itimer'}; X X########################################################################### X# itimer conversion function; this one goes both ways Xsub itimer { X wantarray ? unpack($itimer_t, $_[0]) X : pack($itimer_t, $_[0], $_[1], $_[2], $_[3]); X} X X X########################################################################### Xsub setitimer { X local($which) = shift; X X die "setitimer: input itimer not length ".$sizeof{'itimer'} X unless length($_[0]) == $sizeof{'itimer'}; X X $_[1] = &itimer(0,0,0,0); X !syscall(&SYS_setitimer, $which, $_[0], $_[1]); X} X X########################################################################### Xsub getitimer { X local($which) = shift; X X $_[0] = &itimer(0,0,0,0); X X !syscall(&SYS_getitimer, $which, $_[0]); X} X X########################################################################### X# X# alarm; send me a SIGALRM in this many seconds X# Xsub alarm { X local($ticks) = @_; X local($itimer,$otimer); X local($secs, $usecs); X X $otimer = &itimer(0,0,0,0); X $itimer = &itimer(0,0,$ticks,0); X X do setitimer(&ITIMER_REAL, $itimer, $otimer) || warn "alarm: setitimer failed: $!"; X X ($isecs, $iusecs, $secs, $usecs) = &itimer($otimer); X return $secs; X} X !STUFFY!FUNK! echo Extracting ttimer sed >ttimer <<'!STUFFY!FUNK!' -e 's/X//' X#!../perl X Xdo 'source.pl' || die "can't do source.pl"; X Xdo source('timers.pl'); Xdo source('ctime.pl'); X X$| = 1; X X$SIG{'ALRM'} = 'bingo'; X Xsub bingo { X print "i was alarmed at time ", &ctime(time); X $hit++; X} X Xdo timeleft(); X Xprint "setting alarms at ", &ctime(time); X Xdo alarm(7); X Xprintf "alarm had %d secs left\n", &alarm(3); X Xdo { &timeleft; } until $hit; X Xprint "escaped at ", &ctime(time); X Xsub timeleft { X &getitimer(&ITIMER_REAL, $timeleft) || die "can't getitimer: $!"; X printf "timeleft is %d, %d, %d, %d\n", &itimer($timeleft); X} !STUFFY!FUNK! echo "" echo "End of kit" : I do not append .signature, but someone might mail this. exit