By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Nice a tight loop?

P: n/a
Environment: Linux C program (gcc), on linux kernel 2.6.16

Is there any function I can put inside a tight loop in order to make
the process a little nicer. In that it will give at least the CPU
temporarily back to the OS so that it doesn't seem like the current
program is consuming 50% of cpu resource?

The said loop had to process 32 million records in an structure array
in memory, and when it goes into this loop. Well the system seems to
freeze. The original programmer/designer had long left the company.
We want to touch this piece of code as little as possible due to
time/resource constraint. Thanks.

Jul 7 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Joseph wrote:
Environment: Linux C program (gcc), on linux kernel 2.6.16

Is there any function I can put inside a tight loop in order to make
the process a little nicer. In that it will give at least the CPU
temporarily back to the OS so that it doesn't seem like the current
program is consuming 50% of cpu resource?

The said loop had to process 32 million records in an structure array
in memory, and when it goes into this loop. Well the system seems to
freeze. The original programmer/designer had long left the company.
We want to touch this piece of code as little as possible due to
time/resource constraint. Thanks.
OT here, try a Linux group, you will have to profile and find the cause.

--
Ian Collins.
Jul 7 '06 #2

P: n/a
Joseph said:
Environment: Linux C program (gcc), on linux kernel 2.6.16

Is there any function I can put inside a tight loop in order to make
the process a little nicer. In that it will give at least the CPU
temporarily back to the OS so that it doesn't seem like the current
program is consuming 50% of cpu resource?
Yes, there are at least two ways to spread the load, one programmatic and
one from within the shell. The C language itself doesn't have any specific
way to do this, so the details of your solution are off-topic here, but a
Linux group should be able to set you on the right road in fairly short
order. I suggest comp.os.linux.development.apps as a starting point. I
would guess that they would tell you about nice(1), sleep(3), and
usleep(3), but that's just a guess really.

Oh, btw, I know this sounds a bit obvious, but do bear in mind that yielding
the CPU will increase the runtime of the application!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 7 '06 #3

P: n/a
Joseph wrote:
Environment: Linux C program (gcc), on linux kernel 2.6.16

Is there any function I can put inside a tight loop in order to make
the process a little nicer. In that it will give at least the CPU
temporarily back to the OS so that it doesn't seem like the current
program is consuming 50% of cpu resource?

The said loop had to process 32 million records in an structure array
in memory, and when it goes into this loop. Well the system seems to
freeze. The original programmer/designer had long left the company.
We want to touch this piece of code as little as possible due to
time/resource constraint. Thanks.
out_of_topic {
I would suggest that there is something wrong with the system
then. On the Linux system I use there's often at least one
process taking at least 97% of cpu time and I notice no delay
in the system's response times for everyday jobs like text editing
for example. It certainly doesn't freeze.
}

Richard Heathfield wrote:
Yes, there are at least two ways to spread the load, one programmatic and
one from within the shell. The C language itself doesn't have any specific
way to do this, so the details of your solution are off-topic here, but a
Linux group should be able to set you on the right road in fairly short
order. I suggest comp.os.linux.development.apps as a starting point. I
would guess that they would tell you about nice(1), sleep(3), and
usleep(3), but that's just a guess really.
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.

Spiros Bousbouras

Jul 7 '06 #4

P: n/a
sp****@gmail.com writes:
[...]
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 7 '06 #5

P: n/a

Keith Thompson wrote:
sp****@gmail.com writes:
[...]
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.

Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)
Why do they call it ""Standard C Library Functions"" then ?

Jul 7 '06 #6

P: n/a
sp****@gmail.com said:

<snip>
>
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
The Solaris man pages do not define the C language. The C Standard does. And
it doesn't mention sleep or usleep in its library section. Therefore, they
are not standard C library functions.

The sleep function is, however, a POSIX function. (I don't believe that
usleep is POSIX - it appears to be a BSD function.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 7 '06 #7

P: n/a
sp****@gmail.com wrote:
Keith Thompson wrote:

>>sp****@gmail.com writes:
[...]
>>>Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.

Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)


Why do they call it ""Standard C Library Functions"" then ?
Because they live in the Solaris's standard library.

--
Ian Collins.
Jul 7 '06 #8

P: n/a

Richard Heathfield wrote:
sp****@gmail.com said:

<snip>

Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.

The Solaris man pages do not define the C language. The C Standard does.
I'm aware of that. I was simply assuming that when the Solaris man
pages say
"standard" they mean in the C standard. I guess I was wrong.

Jul 7 '06 #9

P: n/a
sp****@gmail.com wrote:
>
Joseph wrote:
[...]
program is consuming 50% of cpu resource?

The said loop had to process 32 million records in an structure array
in memory, and when it goes into this loop. Well the system seems to
[...]
out_of_topic {
I would suggest that there is something wrong with the system
then. On the Linux system I use there's often at least one
process taking at least 97% of cpu time and I notice no delay
in the system's response times for everyday jobs like text editing
for example. It certainly doesn't freeze.
Apparently, many people have learned / been taught that "a system
should never go to 100% CPU usage". Our company has had many
support calls/e-mails over the years from people complaining that
"something is wrong" because the CPU usage goes to 100% when they
run a report or build an index, even when using "nice". Well, I
would hope that a CPU-bound operation, such as an in-memory sort
would cause the system to go to 100% usage. (If not, then the
system is wasting _my_ time by sitting idle when it could be
sorting.) They can't seem to grasp the concept that "100% usage"
doesn't mean "starving all the other processes", but rather
"using all of the otherwise-idle time".
}
[...]
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th*************@gmail.com>
Jul 7 '06 #10

P: n/a
sp****@gmail.com writes:
Richard Heathfield wrote:
sp****@gmail.com said:

<snip>
>
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
The Solaris man pages do not define the C language. The C Standard does.

I'm aware of that. I was simply assuming that when the Solaris man
pages say
"standard" they mean in the C standard. I guess I was wrong.
My Linux man pages say:

$ man 3 sleep:
Linux Programmer's Manual
bla bla bla
CONFORMING TO
POSIX.1

$ man usleep
Linux Programmer's Manual
bla bla bla
CONFORMING TO
4.3BSD. The SUSv2 version return int, and this is also the
prototype used by glibc 2.2.2. Only the EINVAL error return is
documented by SUSv2.

$ man 3 printf
Linux Programmer's Manual
bla bla bla
CONFORMING TO
.....(printf functions) functions conform to ANSI X3.159-1989
(``ANSI C'') and ISO/IEC 9899:1999 (``ISO C99'')... (a lot more
information snipped).

Is there a CONFORMING TO section in your Solaris man pages? If yes,
that will tell you where the function is coming from.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 7 '06 #11

P: n/a
sp****@gmail.com wrote:
Keith Thompson wrote:
>sp****@gmail.com writes:
[...]
>>Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)

Why do they call it ""Standard C Library Functions"" then ?
My company has created and maintained a boatload of "standard C library
functions" and they have nothing to do with ISO C. There is also a
POSIX standard; this is what we often consider our "standard" in our
documentation, when we refer to such functions outside of the context of
ISO C.
Jul 7 '06 #12

P: n/a
Nelu <sp*******@gmail.comwrites:
sp****@gmail.com writes:
>Richard Heathfield wrote:
sp****@gmail.com said:
<snip>
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
The Solaris man pages do not define the C language. The C Standard does.
I'm aware of that. I was simply assuming that when the Solaris man
pages say "standard" they mean in the C standard. I guess I was
wrong.
[snip]
Is there a CONFORMING TO section in your Solaris man pages? If yes,
that will tell you where the function is coming from.
Alas, there isn't (at least not on my Solaris 9 system).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 7 '06 #13

P: n/a
Keith Thompson <ks***@mib.orgwrites:
Nelu <sp*******@gmail.comwrites:
sp****@gmail.com writes:
Richard Heathfield wrote:
sp****@gmail.com said:
<snip>
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
The Solaris man pages do not define the C language. The C Standard does.
I'm aware of that. I was simply assuming that when the Solaris man
pages say "standard" they mean in the C standard. I guess I was
wrong.
[snip]
Is there a CONFORMING TO section in your Solaris man pages? If yes,
that will tell you where the function is coming from.

Alas, there isn't (at least not on my Solaris 9 system).
I used to have access to a SUN Sparc (U10?) running Solaris 7, but it
was replaced with an IBM PC so I couldn't check it anymore. I refuse
to install Solaris on x86 :-).

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jul 7 '06 #14

P: n/a
Clever Monkey wrote:
sp****@gmail.com wrote:
Keith Thompson wrote:
sp****@gmail.com writes:
[...]
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)
Why do they call it ""Standard C Library Functions"" then ?
My company has created and maintained a boatload of "standard C library
functions" and they have nothing to do with ISO C. There is also a
POSIX standard; this is what we often consider our "standard" in our
documentation, when we refer to such functions outside of the context of
ISO C.
I hope then that your documentation documents which standard it refers
to when using the word standard. Otherwise people might get the wrong
idea.

Keith Thompson wrote:
Nelu <sp*******@gmail.comwrites:
sp****@gmail.com writes:
Richard Heathfield wrote:
sp****@gmail.com said:
<snip>
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
The Solaris man pages do not define the C language. The C Standard does.
I'm aware of that. I was simply assuming that when the Solaris man
pages say "standard" they mean in the C standard. I guess I was
wrong.
[snip]
Is there a CONFORMING TO section in your Solaris man pages? If yes,
that will tell you where the function is coming from.

Alas, there isn't (at least not on my Solaris 9 system).
Solaris 8 doesn't either. But it does mention when the function comes
from BSD.

My confusion was due to the fact that on every other occasion when I
checked the Solaris man page for some C function and it said that it
was standard , then the function was part of the C standard.

Jul 7 '06 #15

P: n/a
sp****@gmail.com wrote:
Clever Monkey wrote:
>sp****@gmail.com wrote:
>>Keith Thompson wrote:

sp****@gmail.com writes:
[...]
Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.
Nope.

That section of man pages is titled "Standard C Library Functions";
that doesn't imply that they're all defined by the C standard. (Or,
if it does, it's wrong.)
Why do they call it ""Standard C Library Functions"" then ?
My company has created and maintained a boatload of "standard C library
functions" and they have nothing to do with ISO C. There is also a
POSIX standard; this is what we often consider our "standard" in our
documentation, when we refer to such functions outside of the context of
ISO C.

I hope then that your documentation documents which standard it refers
to when using the word standard. Otherwise people might get the wrong
idea.
Our man pages do, along the same lines as the "conformance" sections
discussed else-thread. We have a large library we use internally, and a
big set of commercial tools and libraries to provide analogs to
"standard" libraries for systems that do not (traditionally) have them
(where standard is defined as any number of standards organizations and
levels). This is less common now, but was a real problem for Win32 and
mainframe developers in the past.

But, yes, conformance to specific standards is documented fully in our
customer facing stuff. Internal APIs and libraries mention standards
only when a comment discusses a function, feature or bugfix in the
context of a specific standard or (quite often) RFC section.
Jul 7 '06 #16

P: n/a
sp****@gmail.com wrote:
Richard Heathfield wrote:

>>sp****@gmail.com said:

<snip>
>>>Aren't sleep and usleep standard C ? The Solaris man pages seem to
think so.

The Solaris man pages do not define the C language. The C Standard does.


I'm aware of that. I was simply assuming that when the Solaris man
pages say
"standard" they mean in the C standard. I guess I was wrong.
Solaris is a POSIX system, so 'standard' in the man pages means POSIX/SUSv3.

--
Ian Collins.
Jul 7 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.