Nice a tight loop?

Discussion in 'C Programming' started by Joseph, Jul 7, 2006.

  1. Joseph

    Joseph Guest

    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.
     
    Joseph, Jul 7, 2006
    #1
    1. Advertising

  2. Joseph

    Ian Collins Guest

    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.
     
    Ian Collins, Jul 7, 2006
    #2
    1. Advertising

  3. 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)
     
    Richard Heathfield, Jul 7, 2006
    #3
  4. Joseph

    Guest

    Re: Nice a tight loop?

    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, 2006
    #4
  5. Re: Nice a tight loop?

    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) <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.
     
    Keith Thompson, Jul 7, 2006
    #5
  6. Joseph

    Guest

    Re: Nice a tight loop?

    Keith Thompson wrote:

    > 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, 2006
    #6
  7. Re: Nice a tight loop?

    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)
     
    Richard Heathfield, Jul 7, 2006
    #7
  8. Joseph

    Ian Collins Guest

    Re: Nice a tight loop?

    wrote:
    > Keith Thompson wrote:
    >
    >
    >> 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.
     
    Ian Collins, Jul 7, 2006
    #8
  9. Joseph

    Guest

    Re: Nice a tight loop?

    Richard Heathfield wrote:

    > 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, 2006
    #9
  10. Re: Nice a tight loop?

    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:>
     
    Kenneth Brody, Jul 7, 2006
    #10
  11. Joseph

    Nelu Guest

    Re: Nice a tight loop?

    writes:

    > Richard Heathfield wrote:
    >
    > > 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...)
     
    Nelu, Jul 7, 2006
    #11
  12. Re: Nice a tight loop?

    wrote:
    > Keith Thompson wrote:
    >
    >> 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.
     
    Clever Monkey, Jul 7, 2006
    #12
  13. Re: Nice a tight loop?

    Nelu <> writes:
    > writes:
    >> Richard Heathfield wrote:
    >> > 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) <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.
     
    Keith Thompson, Jul 7, 2006
    #13
  14. Joseph

    Nelu Guest

    Re: Nice a tight loop?

    Keith Thompson <> writes:

    > Nelu <> writes:
    > > writes:
    > >> Richard Heathfield wrote:
    > >> > 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...)
     
    Nelu, Jul 7, 2006
    #14
  15. Joseph

    Guest

    Re: Nice a tight loop?

    Clever Monkey wrote:

    > wrote:
    > > Keith Thompson wrote:
    > >
    > >> 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 <> writes:
    > > writes:
    > >> Richard Heathfield wrote:
    > >> > 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, 2006
    #15
  16. Re: Nice a tight loop?

    wrote:
    > Clever Monkey wrote:
    >
    >> wrote:
    >>> Keith Thompson wrote:
    >>>
    >>>> 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.
     
    Clever Monkey, Jul 7, 2006
    #16
  17. Joseph

    Ian Collins Guest

    Re: Nice a tight loop?

    wrote:
    > Richard Heathfield wrote:
    >
    >
    >> 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.
     
    Ian Collins, Jul 7, 2006
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Knoppert

    This could be shorter tight? (String to Hex)

    Edwin Knoppert, Feb 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    495
    Edwin Knoppert
    Feb 17, 2006
  2. Navaneeth

    Using malloc/free in a tight loop

    Navaneeth, Nov 17, 2010, in forum: C Programming
    Replies:
    9
    Views:
    1,529
    Nobody
    Nov 19, 2010
  3. gry

    performance of tight loop

    gry, Dec 14, 2010, in forum: Python
    Replies:
    8
    Views:
    308
    Peter Otten
    Dec 14, 2010
  4. Richard A. DeVenezia

    Update a status object while in a tight loop ?

    Richard A. DeVenezia, Apr 3, 2004, in forum: Javascript
    Replies:
    1
    Views:
    117
    Richard Cornford
    Apr 4, 2004
  5. Isaac Won
    Replies:
    9
    Views:
    398
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page