Temporary program suspension

Discussion in 'C Programming' started by Thomas Carter, Feb 21, 2005.

  1. Is it possible, in an OS-independent way, to suspend a C program for some
    specified amount of time, in such a way that CPU load, due to this
    program, while remaining suspended, is non-existant or, failing that,
    minimal?
    Thomas Carter, Feb 21, 2005
    #1
    1. Advertising

  2. In article <>,
    Thomas Carter <> wrote:
    > Is it possible, in an OS-independent way, to suspend a C program for some
    >specified amount of time, in such a way that CPU load, due to this
    >program, while remaining suspended, is non-existant or, failing that,
    >minimal?


    No. The best you can do is create an abstraction layer that will call an
    appropriate API function for the platform you're running on. F'rexample:

    (Not compiled or tested, standard disclaimers apply)
    --------
    /*os-abstraction.h*/
    #ifndef H_OS_ABSTRACTION
    #define H_OS_ABSTRACTION

    /*Suspend execution for n seconds, using an OS-specific API call to
    avoid using CPU time if possible.
    Does not return any error indication is the sleep is interrupted!
    */
    void suspend(unsigned n);

    #endif /*H_OS_ABSTRACTION #include guard*/
    --------
    /*os-abstraction.c*/

    #include "os-abstraction.h"

    #ifdef HAVE_POSIX_SLEEP
    #include <unistd.h>
    /*POSIX sleep() will suspend execution for the number of seconds you give it*/
    void suspend(unsigned n) {sleep(n);}
    #endif /*HAVE_POSIX_SLEEP*/

    #ifdef HAVE_WIN32_SLEEP
    #include <windows.h>
    /*Win32 Sleep() will suspend execution for the number of milliseconds
    you give it
    */
    void suspend(unsigned n) {Sleep(n*1000);}
    #endif

    /*You may also want to include a fallback to use clock() to spin
    until the requested time has elapsed if you can't find any more
    multitasking-friendly API calls to do it.
    */
    --------

    Then in your code:
    --------
    #include "os-abstraction.h"

    /*...and when you want to sleep for a while:*/
    suspend(delay);
    --------


    dave

    --
    Dave Vandervies
    > And if we see a post about VisualAda++, we will storm Redmond and shove the
    > One Boot up the Gates of Hell. --August Derleth and Richard Heathfield

    Why wait for a post about VisualAda++? If it feels good, do it. in CLC
    Dave Vandervies, Feb 21, 2005
    #2
    1. Advertising

  3. Thomas Carter wrote:

    > Is it possible, in an OS-independent way,
    > to suspend a C program for some specified amount of time,
    > in such a way that CPU load, due to this program, while remaining suspended,
    > is non-existant or, failing that, minimal?


    It's not [completely] OS-independent
    but POSIX compliant systems will provide a mechanism:

    > man 7 signal

    SIGNAL(7) Linux Programmer’s Manual SIGNAL(7)



    NAME
    signal - list of available signals
    .
    .
    .
    Signal Value Action Comment
    ------------------------------------------------------
    SIGCONT 19,18,25 Continue if stopped
    SIGSTOP 17,19,23 Stop Stop process
    .
    .
    .

    > kill -l

    HUP INT QUIT ILL TRAP ABRT BUS FPE KILL USR1 SEGV USR2 PIPE
    ALRM TERM STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ
    VTALRM PROF WINCH POLL PWR SYS RTMIN RTMIN+1 RTMIN+2 RTMIN+3
    RTMAX-3 RTMAX-2 RTMAX-1 RTMAX
    > xclock -update 1 &
    > ps ax | grep xclock

    7053 pts/1 S 0:00 xclock -update 1
    7060 pts/1 R 0:00 grep xclock
    > kill -s STOP 7053

    [1] + Suspended (signal) xclock -update 1
    > kill -s CONT 7053
    E. Robert Tisdale, Feb 21, 2005
    #3
  4. > /*You may also want to include a fallback to use clock() to spin
    > until the requested time has elapsed if you can't find any more
    > multitasking-friendly API calls to do it.
    > */


    As mentioned there's no real portable way that's safe and does what you
    want, yet you probably could do something like:

    void ansi_wait(int seconds)
    {
    volatile clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;

    while(clock() < endwait)
    ;
    }

    But again, it's quiet naieve and might fail miserably on some platforms, and
    might even take more resources than you want it to.

    Cheers,
    Gibby Koldenhof
    Gibby Koldenhof, Feb 21, 2005
    #4
  5. "Gibby Koldenhof" <flux@> writes:
    >> /*You may also want to include a fallback to use clock() to spin
    >> until the requested time has elapsed if you can't find any more
    >> multitasking-friendly API calls to do it.
    >> */

    >
    > As mentioned there's no real portable way that's safe and does what you
    > want, yet you probably could do something like:
    >
    > void ansi_wait(int seconds)
    > {
    > volatile clock_t endwait = clock() + seconds * CLOCKS_PER_SEC ;
    >
    > while(clock() < endwait)
    > ;
    > }


    The clock() function returns an indication of CPU time, not wall clock
    time. Using clock() guarantees that the program will consume CPu
    resources.

    > But again, it's quiet naieve and might fail miserably on some platforms, and
    > might even take more resources than you want it to.


    That too.

    --
    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, Feb 21, 2005
    #5
  6. In article <cvddb8$mnt$>,
    E. Robert Tisdale <> wrote:
    >Thomas Carter wrote:
    >
    >> Is it possible, in an OS-independent way,
    >> to suspend a C program for some specified amount of time,
    >> in such a way that CPU load, due to this program, while remaining suspended,
    >> is non-existant or, failing that, minimal?

    >
    >It's not [completely] OS-independent
    >but POSIX compliant systems will provide a mechanism:
    >
    > > man 7 signal


    ....which isn't portable (which the OP specifically requested), and
    doesn't correctly answer the OP's question (how would you specify the
    amount of time to suspend for?).


    dave
    (let me go attack the sigmonster with a pointy stick...)

    --
    Dave Vandervies
    This is an excellent answer. It's off-topic, it's wrong, and it doesn't
    answer the question.
    --Richard Heathfield in comp.lang.c
    Dave Vandervies, Feb 21, 2005
    #6
  7. "E. Robert Tisdale" <> writes:
    > Thomas Carter wrote:
    >> Is it possible, in an OS-independent way,
    >> to suspend a C program for some specified amount of time,
    >> in such a way that CPU load, due to this program, while remaining suspended,
    >> is non-existant or, failing that, minimal?


    Quick answer: No.

    > It's not [completely] OS-independent
    > but POSIX compliant systems will provide a mechanism:


    It's not OS-independent at all.

    [snip]
    > > kill -s STOP 7053

    > [1] + Suspended (signal) xclock -update 1
    > > kill -s CONT 7053


    And that has nothing to do with C. I suspect the OP was looking for
    something that could be invoked from within a C program. You've also
    ignored the "for some specified amount of time" requirement.

    If you're going to assume POSIX (contrary to the OP's request for an
    OS-independent method), there are much better ways to do this.

    <OT>The sleep() function is OS-specific but widely available.
    Variants with sub-second resolution are not quite as widely available.
    For details, try your system documentation or a system-specific
    newsgroup.</OT>

    --
    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, Feb 21, 2005
    #7
  8. In article <>,
    Keith Thompson <> wrote:
    >"Gibby Koldenhof" <flux@> writes:

    [quoting me:]
    >>> /*You may also want to include a fallback to use clock() to spin
    >>> until the requested time has elapsed if you can't find any more
    >>> multitasking-friendly API calls to do it.
    >>> */

    >>
    >> As mentioned there's no real portable way that's safe and does what you
    >> want, yet you probably could do something like:


    >The clock() function returns an indication of CPU time, not wall clock
    >time. Using clock() guarantees that the program will consume CPu
    >resources.


    Gah. That was my mistake - to time the suspension according to a wall
    clock and not according to CPU time taken by the program, you'd want to
    use time() and not clock() as I suggested.

    A really smart optimizer could even notice that you're doing nothing but
    call time() in your loop and insert an appropriate sleep call for you.
    (Though I suspect if you had an optimizer that smart you'd also have an
    OS API call that you'd want to use instead...)


    dave

    --
    Dave Vandervies
    Are we supposed to trust a page that states, amongst others, "Microsoft
    absolutely respects the privacy of its customers"? 'Cause I don't, no matter
    how pleasantly they smirk. --Richard Bos in comp.lang.c
    Dave Vandervies, Feb 21, 2005
    #8
  9. Keith Thompson wrote:

    > E. Robert Tisdale writes:
    >
    >>Thomas Carter wrote:
    >>
    >>>Is it possible, in an OS-independent way,
    >>>to suspend a C program for some specified amount of time,
    >>>in such a way that CPU load, due to this program, while remaining suspended,
    >>>is non-existant or, failing that, minimal?

    >
    > Quick answer: No.
    >
    >>It's not [completely] OS-independent
    >>but POSIX compliant systems will provide a mechanism:

    >
    > It's not OS-independent at all.
    >
    > [snip]
    >
    >> > kill -s STOP 7053

    >> [1] + Suspended (signal) xclock -update 1
    >> > kill -s CONT 7053

    >
    > And that has nothing to do with C. I suspect that
    > the OP was looking for something that could be invoked from within a C program.
    > You've also ignored the "for some specified amount of time" requirement.
    >
    > If you're going to assume that
    > POSIX (contrary to the OP's request for an OS-independent method),
    > there are much better ways to do this.
    >
    > <OT>The sleep() function is OS-specific but widely available.
    > Variants with sub-second resolution are not quite as widely available.
    > For details,
    > try your system documentation or a system-specific newsgroup.</OT>


    I'm not sure that this will be useful to Thomas Carter.
    There is no way to put the program to sleep asynchronously.
    He will almost certainly need a way to signal the program
    to put it to sleep anyway so he might as well send signals
    to suspend then continue after a specified amount of time.
    E. Robert Tisdale, Feb 21, 2005
    #9
  10. In article <>,
    Keith Thompson <> wrote:

    >> It's not [completely] OS-independent
    >> but POSIX compliant systems will provide a mechanism:

    >
    >It's not OS-independent at all.


    You could say the same about C. It's only available on OSes that have
    a C compiler.

    -- Richard
    Richard Tobin, Feb 21, 2005
    #10
  11. "E. Robert Tisdale" <> writes:
    > Keith Thompson wrote:

    [...]
    >> <OT>The sleep() function is OS-specific but widely available.
    >> Variants with sub-second resolution are not quite as widely available.
    >> For details,
    >> try your system documentation or a system-specific newsgroup.</OT>

    >
    > I'm not sure that this will be useful to Thomas Carter.
    > There is no way to put the program to sleep asynchronously.
    > He will almost certainly need a way to signal the program
    > to put it to sleep anyway so he might as well send signals
    > to suspend then continue after a specified amount of time.


    He didn't ask about putting the program to sleep asynchronously.

    If the sleep() function were part of ISO C, it would exactly satisfy
    what the OP was asking about (re-read his question if you don't
    believe me).

    Note that he specifically asked for something OS-independent.

    --
    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, Feb 22, 2005
    #11
  12. On Mon, 21 Feb 2005 23:05:50 +0000, Richard Tobin wrote:

    > In article <>,
    > Keith Thompson <> wrote:
    >
    >>> It's not [completely] OS-independent
    >>> but POSIX compliant systems will provide a mechanism:

    >>
    >>It's not OS-independent at all.

    >
    > You could say the same about C. It's only available on OSes that have
    > a C compiler.


    That isn't a dependency of the characteristics of the OS. The only issue
    there would be if the OS made it impossible to create a C compiler for it.
    And remember a C compiler for an OS doesn't have to run under the OS, you
    can typically use a cross-compiler for environments too limited in
    themselves to run a C compiler.

    POSIX on the other hand is essentially an OS level specification so
    anything that depends on it is inherently OS-specific, even though it
    might be emulated.

    Lawrence
    Lawrence Kirby, Feb 23, 2005
    #12
  13. Thomas Carter

    Richard Bos Guest

    (Richard Tobin) wrote:

    > In article <>,
    > Keith Thompson <> wrote:
    >
    > >> It's not [completely] OS-independent
    > >> but POSIX compliant systems will provide a mechanism:

    > >
    > >It's not OS-independent at all.

    >
    > You could say the same about C. It's only available on OSes that have
    > a C compiler.


    But the context of this newsgroup is OSes (or other environments) that
    _do_ have a C implementation (not necessarily compiler). On all of
    those, C is available (tautologically); not all of them do have POSIX.

    Richard
    Richard Bos, Feb 23, 2005
    #13
    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. Harsh Vardhan Singh

    Temporary Files

    Harsh Vardhan Singh, Mar 3, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    1,216
    Amit Bahree
    Mar 9, 2005
  2. Natty Gur
    Replies:
    2
    Views:
    4,256
    Yan-Hong Huang[MSFT]
    Jul 3, 2003
  3. sudha
    Replies:
    1
    Views:
    301
  4. Replies:
    7
    Views:
    3,178
    James Kanze
    Feb 12, 2008
  5. Chris Forone

    possibly ot: thread suspension

    Chris Forone, May 12, 2008, in forum: C++
    Replies:
    7
    Views:
    380
    Chris Forone
    May 15, 2008
Loading...

Share This Page