I've been thinking of starting my program with a lower priority using
nice, but I don't think this will help nearly as much as the above
Thanks
Alec
You're right that busy-waiting is morally wrong.
> I don't want to do a sleep() since one second is actually long enough that
> I may miss a message, but I would like to lower the CPU usage. Is there a
> C function in Linux that will allow me to yield a little CPU time?
If you actually want to give up your timeslice (probably the right thing
here), then the traditional idiom is to sleep for 0 seconds. Modern
systems, however, provide sched_yield(2) as specified by POSIX.1b for the
same purpose.
Alternatively, if you just want to sleep for a sub-second interval, then you
may have a BSD-derived usleep(3) (to sleep for a time specified in
microseconds) or POSIX.1b-specified nanosleep(3) (for a time specified in
nanoseconds). The effective resolution of these sleeps is system-specific.
You can also use itimers (getitimer(2), setitimer(2)), or one of select(2)
or poll(2) with a timeout but no file descriptors. See the FAQ (available
on <URL:http://www.erlenstar.demon.co.uk/unix/>) for more on this topic,
including examples.
--
Aaron Crane <aaron...@pobox.com> <URL:http://pobox.com/~aaronc/>
How are these messages coming in? If it's a pipe, socket, or device you
should be able to use select() to wait for something to arrive, rather than
a loop checking for data.
--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Don't bother cc'ing followups to me.
: I've got an application that has 35% CPU usage when all it's really
: doing is sitting in a while loop to process messages. I don't want to
: do a sleep() since one second is actually long enough that I may miss
: a message, but I would like to lower the CPU usage. Is there a C
: function in Linux that will allow me to yield a little CPU time?
If your messages go via some file descriptor, best thing is to use
select system call on this descriptor. It'll put you in sleep until
something arrives to read
: I've been thinking of starting my program with a lower priority using
: nice, but I don't think this will help nearly as much as the above
: Thanks
: Alec
--
--------------------------------------------------------
I have tin news and pine mail...
Victor Wagner @ home = vi...@wagner.rinet.ru
struct timeval tv;
/* Wait for one quarter of a second */
tv.tv_usec = 250000;
tv.tv_sec = 0;
select(0, NULL, NULL, NULL, &tv)
...
I believe the linux version of select() will change the value
of tv after each call, so you will have to reset it between
calls.
Sam
Alec Lazarescu (al...@eden.rutgers.edu) wrote:
: I've got an application that has 35% CPU usage when all it's really
: doing is sitting in a while loop to process messages. I don't want to
: do a sleep() since one second is actually long enough that I may miss
: a message, but I would like to lower the CPU usage. Is there a C
: function in Linux that will allow me to yield a little CPU time?
: I've been thinking of starting my program with a lower priority using