Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to Yield CPU time in a polling loop

638 views
Skip to first unread message

Alec Lazarescu

unread,
Jan 17, 1999, 3:00:00 AM1/17/99
to

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
nice, but I don't think this will help nearly as much as the above

Thanks

Alec

Aaron Crane

unread,
Jan 17, 1999, 3:00:00 AM1/17/99
to
In article <36a13def...@news.giganews.com>,

al...@eden.rutgers.edu (Alec Lazarescu) writes:
> 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.

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/>

Barry Margolin

unread,
Jan 17, 1999, 3:00:00 AM1/17/99
to
In article <36a13def...@news.giganews.com>,

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.

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.

Victor Wagner

unread,
Jan 17, 1999, 3:00:00 AM1/17/99
to
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?

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

Samuel Schofield

unread,
Jan 24, 1999, 3:00:00 AM1/24/99
to
You can use select() to sleep for a time interval less than one second.
Call it with the file descriptor sets each null and just set the timeout
to be the duration you want to sleep. i.e.

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

0 new messages