Subject: Re: usleep and signals
To: None <>
From: maximum entropy <>
List: current-users
Date: 08/18/1997 04:43:45
>From: Rick Byers <>
>I'm working on a program that will receive an alarm signal once a second.
>It needs to sleep for a period of time.  How can I do this in a way that
>is compatable with both -current and 1.2.1?  Using nanosleep (with a loop
>to watch for the signal) works great under current, but nanosleep doesn't
>exist under 1.2.1.  usleep works under 1.2.1, but doesn't return how much
>time is left to sleep when a signal hits it (and the signal doesn't get
>handled - unlike nanosleep).

You need to be very careful when mixing usleep() and alarm().  In
historical systems (including 1.2.1), both use the same interval timer
to schedule a SIGALRM.  Under POSIXish systems, sleep() makes some
guarantees about delivery of the signal when mixed with alarm(), but
usleep() doesn't make any similar guarantees.  This is probably why
your signal isn't being handled in 1.2.1 -- usleep() is stealing
SIGALRM for its own purposes.  Under current, both usleep() and
sleep() are implemented using nanosleep(), most likely to avoid
stealing alarms from user programs without going through really nasty

>Is there any easy way around this?  I'm thinking I'll have to resort to
>turning off my alarm before the usleep, and turning it back on afterwards.
>The only problem with this is that it doesn't get processed during the
>sleep.  I guess I could make a loop of 1 second usleeps (or sleeps for
>that matter), and call the handler inside the loop.  I assume nanosleep
>was put in to avoid this kludge ?

How you deal with this depends on how long your program needs to
sleep.  If it's a longish period of time, and a granularity of 1
second is OK, you should be OK if you simply use sleep().  If it's a
shorter period, your best bet for portability (at least amond BSDish
systems) is to check the system clock using gettimeofday(), go to
sleep for your short period using select(), call gettimeofday() when
select() returns, calculate the remaining time to sleep, and continue


entropy -- it's not just a good idea, it's the second law.

This message may refer to a product containing software developed by
Christopher G. Demetriou for the NetBSD Project.