Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 24 Oct 2000 21:43:32 -0700 (PDT)
From:      William Richard <wdr@tdl.com>
To:        hackers@freebsd.org
Subject:   Interval Timer Question
Message-ID:  <Pine.BSF.4.21.0010242120240.279-100000@saffron.wraltd>

next in thread | raw e-mail | index | archive | help
Greetings, Hackers.

This is my first post to -hackers, so forgive me if I'm asking in the
wrong list.

I've been writing a little program to wait for a specified length of time,
then beep on the terminal and exit.  I originally made the process wait
the specified number of seconds with a call to sleep(3).  Once invoked,
the program would sit there, not give any indication that it was running,
until the interval had passed and it beeped.

So I wrote a cheap little twirling baton routine and stuck it into my
program.  Obviously, the baton won't twirl if the process is sleep(3)ing,
so I delved into the manual and came across the setitimer(2) system call.

Setitimer takes three arguments: a #define from time.h specifying whether
the timer should run in real time, process virtual time, or on the
profiling timer; a pointer to a struct itimerval (which contains two
struct timevals) which indicates how long the timer should run for and
what to do when it runs out; and a pointer to a struct itimerval to store
the old value of the timer before setitimer resets it.  When the interval
timer expires, it sends the process a SIGALRM, the default action for 
which is to terminate the process.

I rewrote my timer program to use setitimer, defining a signal handler
for SIGALRM with signal(3), and it worked as I expected.  In this version,
the process called setitimer, then pause(3)ed until the process received a
signal and invoked the signal handler (which beeped the terminal and
exited).

I then rewrote the program again to include the twirling baton, and this
is where things went awry.  The baton is a for loop, which putchars an
element of the baton, flushes the stdout, write a backspace, and tests to
see if we're at the end of the array which stores the -\|/ of the baton
and if so, reset the for loop iterator.

After adding the for loop, the interval timer I established with setitimer
stopped working.  I removed the fflush and the backspace, and couldn't get
it to work.  I can kill -ALRM the process and make it invoke the signal
handler, but the setitimer interval timer isn't sending a SIGALRM.

I rewrote the program again to use alarm(3) instead of setitimer, and the
program works as expected.  I am confused as to why setitimer didn't work
in this case, but alarm did.

The question is: I would like to know the reason why doesn't setitimer
work when I invoke this for loop?  If someone knows, or at least can give 
me a pointer to where I can start looking for the answer, that would be
great.  Maybe I can add something to the setitimer man page to document
this gotcha.

I can provide source code, either in the list or on a Web page, if it
would help anyone so kind as to answer my question.

Cheers,
William Richard
wdr@tdl.com



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0010242120240.279-100000>