Nice a tight loop?

J

Joseph

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.
 
I

Ian Collins

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?

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.
 
R

Richard Heathfield

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!
 
S

spibou

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?

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 said:
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
 
K

Keith Thompson

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.)
 
S

spibou

Keith said:
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 ?
 
R

Richard Heathfield

(e-mail address removed) said:

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.)
 
I

Ian Collins

Keith Thompson wrote:

(e-mail address removed) 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.
 
S

spibou

Richard said:
(e-mail address removed) said:



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.
 
K

Kenneth Brody

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:[email protected]>
 
N

Nelu

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.
 
C

Clever Monkey

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.
 
K

Keith Thompson

Nelu said:
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).
 
N

Nelu

Keith Thompson said:
Nelu said:
Richard Heathfield wrote:
(e-mail address removed) 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 :).
 
S

spibou

Clever said:
Keith said:
(e-mail address removed) 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 said:
Nelu said:
Richard Heathfield wrote:
(e-mail address removed) 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.
 
C

Clever Monkey

Clever said:
Keith Thompson wrote:

(e-mail address removed) 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.
 
I

Ian Collins

Richard Heathfield wrote:




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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top