Making a program pause

Discussion in 'C Programming' started by dave.zoltan@gmail.com, Dec 24, 2007.

  1. Guest

    Hi everybody,

    I am awfully new to programming in C and all I have had to work with
    so far have been tutorials that I've found online. In my searching,
    however, I have not found a solution to a problem I have been facing.
    Ideally, I would like the program to wait 5 minutes before executing
    the rest of the code. Is there some way that I can use time.h and just
    run a for loop that waits until 5 minutes has passed to continue down
    the program?

    Any help that you experts could give would be greatly appreciated.
    Also, I apologize in advance if there is an obvious solution I have
    overlooked.

    Thanks,

    Dave
     
    , Dec 24, 2007
    #1
    1. Advertising

  2. said:

    > Hi everybody,
    >
    > I am awfully new to programming in C and all I have had to work with
    > so far have been tutorials that I've found online. In my searching,
    > however, I have not found a solution to a problem I have been facing.
    > Ideally, I would like the program to wait 5 minutes before executing
    > the rest of the code. Is there some way that I can use time.h and just
    > run a for loop that waits until 5 minutes has passed to continue down
    > the program?


    There *is* a way you can do that in standard C, yes - but it's not a very
    good way. It will work, but it will be a "busy" loop, taking up valuable
    processing time. You do it like this:

    #include <stddef.h>
    #include <time.h>

    void delay(unsigned int seconds)
    {
    time_t start = time(NULL);
    double diff = 0.0;

    while(diff < seconds)
    {
    diff = difftime(time(NULL), start);
    }

    return;
    } /* to delay for five seconds, call delay(5) */

    but, like I said, this is not a very good way to do it. Unfortunately, it's
    the best way available in standard C. Your C implementation is very likely
    to provide a library function that can do basically the same job except
    that the processor will be released to do other things. For example, Win32
    provides Sleep(), and POSIX(?) provides sleep() and usleep(). If you want
    to discuss such functions, I suggest comp.os.ms-windows.programmer.win32
    and comp.unix.programmer respectively.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 24, 2007
    #2
    1. Advertising

  3. Ivan Novick Guest

    Ivan Novick, Dec 25, 2007
    #3
  4. CBFalconer Guest

    Ivan Novick wrote:
    > Richard Heathfield <> wrote:
    >
    >>> Ideally, I would like the program to wait 5 minutes before
    >>> executing the rest of the code. Is there some way that I can use
    >>> time.h and just run a for loop that waits until 5 minutes has
    >>> passed to continue down the program?

    >
    > There is a sleep function in the GNU C library.
    >
    > http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping


    GNU C is not ISO standard C. Extensions are off-topic here. There
    would have been no objection if you had made this clear.

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee, Frohe Weihnachten
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 25, 2007
    #4
  5. In article <>,
    CBFalconer <> wrote:
    >Ivan Novick wrote:

    ....
    >> There is a sleep function in the GNU C library.
    >>
    >> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping

    >
    >GNU C is not ISO standard C. Extensions are off-topic here. There
    >would have been no objection if you had made this clear.


    Wanna bet?

    No matter how many caveats he had included, someone (probably you or one
    of your cabal) would still have had a go at it.

    It's what we do here. The point is that at least one someone would have
    found it too hard to resist. The egoboo, ya know...
     
    Kenny McCormack, Dec 25, 2007
    #5
  6. Geoff Guest

    On Mon, 24 Dec 2007 10:18:13 -0800 (PST), ""
    <> wrote:

    >Hi everybody,
    >
    >I am awfully new to programming in C and all I have had to work with
    >so far have been tutorials that I've found online. In my searching,
    >however, I have not found a solution to a problem I have been facing.
    >Ideally, I would like the program to wait 5 minutes before executing
    >the rest of the code. Is there some way that I can use time.h and just
    >run a for loop that waits until 5 minutes has passed to continue down
    >the program?
    >
    >Any help that you experts could give would be greatly appreciated.
    >Also, I apologize in advance if there is an obvious solution I have
    >overlooked.
    >
    >Thanks,
    >
    >Dave


    This functionality depends on the operating system (if any) and C
    library functions (if any) that are available on your target system.

    Sleep() in Windows.
    sleep() in Linux/Unix/POSIX (but not Windows POSIX)

    These functions pass control to the OS and the OS does not pass
    control back to your program until the specified time has elapsed.
    There are limitations on how long you can sleep and there are also
    some extensions that allow the program be awakened before the time
    expires.

    A loop that would simply check time or spin on some event is called
    "polling" and is usually not recommended in multitasking environments
    like Windows or Linux since it consumes CPU cycles for nothing.

    A loop that would sleep for 10 second intervals and resume after 5
    minutes in Windows might look like this:

    for (i=0; i < 30; i++)
    Sleep(10000);

    Or just Sleep(300000); for one big 5 minute nap.

    Warning: Timers like this are usually not very accurate.

    You should research your target and development environments.
     
    Geoff, Dec 26, 2007
    #6
  7. "CBFalconer" <> schrieb im Newsbeitrag
    news:...
    > Ivan Novick wrote:
    >> Richard Heathfield <> wrote:
    >>
    >>>> Ideally, I would like the program to wait 5 minutes before
    >>>> executing the rest of the code. Is there some way that I can use
    >>>> time.h and just run a for loop that waits until 5 minutes has
    >>>> passed to continue down the program?

    >>
    >> There is a sleep function in the GNU C library.
    >>
    >> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping

    >
    > GNU C is not ISO standard C. Extensions are off-topic here. There
    > would have been no objection if you had made this clear.

    He did make it very clear that this is in the GNU C library. How could it
    possibly get any clearer?

    Bye, Jojo
     
    Joachim Schmitz, Dec 26, 2007
    #7
  8. Ivan Novick said:

    > On Dec 24, 10:34 am, Richard Heathfield <> wrote:
    >> > Ideally, I would like the program to wait 5 minutes before executing
    >> > the rest of the code. Is there some way that I can use time.h and just
    >> > run a for loop that waits until 5 minutes has passed to continue down
    >> > the program?


    No, I didn't write any of that. I merely quoted it. It was written by the
    OP.

    > There is a sleep function in the GNU C library.


    Yes. My reply (which you snipped) mentioned sleep().

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Dec 26, 2007
    #8
  9. "Joachim Schmitz" <> writes:
    > "CBFalconer" <> schrieb im Newsbeitrag
    > news:...
    >> Ivan Novick wrote:
    >>> Richard Heathfield <> wrote:
    >>>
    >>>>> Ideally, I would like the program to wait 5 minutes before
    >>>>> executing the rest of the code. Is there some way that I can use
    >>>>> time.h and just run a for loop that waits until 5 minutes has
    >>>>> passed to continue down the program?
    >>>
    >>> There is a sleep function in the GNU C library.
    >>>
    >>> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping

    >>
    >> GNU C is not ISO standard C. Extensions are off-topic here. There
    >> would have been no objection if you had made this clear.

    > He did make it very clear that this is in the GNU C library. How could it
    > possibly get any clearer?


    The GNU C library also has a strlen function.
    http://www.gnu.org/software/libc/manual/html_node/String-Length.html

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 26, 2007
    #9
  10. "Keith Thompson" <> schrieb im Newsbeitrag
    news:...
    > "Joachim Schmitz" <> writes:
    >> "CBFalconer" <> schrieb im Newsbeitrag
    >> news:...
    >>> Ivan Novick wrote:
    >>>> Richard Heathfield <> wrote:
    >>>>
    >>>>>> Ideally, I would like the program to wait 5 minutes before
    >>>>>> executing the rest of the code. Is there some way that I can use
    >>>>>> time.h and just run a for loop that waits until 5 minutes has
    >>>>>> passed to continue down the program?
    >>>>
    >>>> There is a sleep function in the GNU C library.
    >>>>
    >>>> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping
    >>>
    >>> GNU C is not ISO standard C. Extensions are off-topic here. There
    >>> would have been no objection if you had made this clear.

    >> He did make it very clear that this is in the GNU C library. How could it
    >> possibly get any clearer?

    >
    > The GNU C library also has a strlen function.
    > http://www.gnu.org/software/libc/manual/html_node/String-Length.html

    So what? Does that make strlen off topic here?

    Bse, Jojo
     
    Joachim Schmitz, Dec 26, 2007
    #10
  11. Guest

    Hey everybody,

    I attempted to use the sleep() function, but I am using Pelles C
    compiler and unistd.h has sleep() commented out. I guess I will have
    to figure out something else to try to get my program to pause.

    Thanks for your help everyone,

    Dave
     
    , Dec 26, 2007
    #11
  12. CBFalconer Guest

    Joachim Schmitz wrote:
    > "Keith Thompson" <> schrieb im Newsbeitrag
    >> "Joachim Schmitz" <> writes:
    >>> "CBFalconer" <> schrieb im Newsbeitrag
    >>>> Ivan Novick wrote:
    >>>>> Richard Heathfield <> wrote:
    >>>>>
    >>>>>> Ideally, I would like the program to wait 5 minutes before
    >>>>>> executing the rest of the code. Is there some way that I can
    >>>>>> use time.h and just run a for loop that waits until 5 minutes
    >>>>>> has passed to continue down the program?
    >>>>>
    >>>>> There is a sleep function in the GNU C library.
    >>>>>
    >>>>> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping
    >>>>
    >>>> GNU C is not ISO standard C. Extensions are off-topic here.
    >>>> There would have been no objection if you had made this clear.
    >>>
    >>> He did make it very clear that this is in the GNU C library.
    >>> How could it possibly get any clearer?

    >>
    >> The GNU C library also has a strlen function.

    >
    > So what? Does that make strlen off topic here?


    No. But 'sleep' is off-topic. Have you caught on to the
    difference yet?

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee, Frohe Weihnachten
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 26, 2007
    #12
  13. "Joachim Schmitz" <> writes:
    > "Keith Thompson" <> schrieb im Newsbeitrag
    > news:...
    >> "Joachim Schmitz" <> writes:
    >>> "CBFalconer" <> schrieb im Newsbeitrag
    >>> news:...
    >>>> Ivan Novick wrote:

    [...]
    >>>>> There is a sleep function in the GNU C library.
    >>>>>
    >>>>> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping
    >>>>
    >>>> GNU C is not ISO standard C. Extensions are off-topic here. There
    >>>> would have been no objection if you had made this clear.
    >>> He did make it very clear that this is in the GNU C library. How could it
    >>> possibly get any clearer?

    >>
    >> The GNU C library also has a strlen function.
    >> http://www.gnu.org/software/libc/manual/html_node/String-Length.html

    > So what? Does that make strlen off topic here?


    Of course not.

    Sorry, apparently I wasn't as clear as I thought I was. My point is
    that saying that the GNU C library has a sleep function does not by
    itself make it perfectly clear that the sleep function is
    non-standard.

    The GNU C library provides implementations of both sleep() and
    strlen(). strlen() is topical here; sleep() is not (though a brief
    mention of sleep() with a redirection to a more appropriate forum is
    IMHO perfectly ok).

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 26, 2007
    #13
  14. SM Ryan Guest

    CBFalconer <> wrote:
    # Ivan Novick wrote:
    # > Richard Heathfield <> wrote:
    # >
    # >>> Ideally, I would like the program to wait 5 minutes before
    # >>> executing the rest of the code. Is there some way that I can use
    # >>> time.h and just run a for loop that waits until 5 minutes has
    # >>> passed to continue down the program?
    # >
    # > There is a sleep function in the GNU C library.
    # >
    # > http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping
    #
    # GNU C is not ISO standard C. Extensions are off-topic here. There
    # would have been no objection if you had made this clear.

    system("sleep 300");

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    Raining down sulphur is like an endurance trial, man. Genocide is the
    most exhausting activity one can engage in. Next to soccer.
     
    SM Ryan, Dec 26, 2007
    #14
  15. SM Ryan <> writes:
    > CBFalconer <> wrote:
    > > Ivan Novick wrote:
    > > > Richard Heathfield <> wrote:
    > > >
    > > >>> Ideally, I would like the program to wait 5 minutes before
    > > >>> executing the rest of the code. Is there some way that I can use
    > > >>> time.h and just run a for loop that waits until 5 minutes has
    > > >>> passed to continue down the program?
    > > >
    > > > There is a sleep function in the GNU C library.
    > > >
    > > > http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping

    > >
    > > GNU C is not ISO standard C. Extensions are off-topic here. There
    > > would have been no objection if you had made this clear.

    >
    > system("sleep 300");


    This is, of course, portable only to systems that provide a command
    called "sleep" that does what you expect it to do.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 27, 2007
    #15
  16. user923005 Guest

    On Dec 24, 10:18 am, "" <>
    wrote:
    > Hi everybody,
    >
    > I am awfully new to programming in C and all I have had to work with
    > so far have been tutorials that I've found online. In my searching,
    > however, I have not found a solution to a problem I have been facing.
    > Ideally, I would like the program to wait 5 minutes before executing
    > the rest of the code. Is there some way that I can use time.h and just
    > run a for loop that waits until 5 minutes has passed to continue down
    > the program?
    >
    > Any help that you experts could give would be greatly appreciated.
    > Also, I apologize in advance if there is an obvious solution I have
    > overlooked.


    The obvious solution that you overlooked was to read the C-FAQ:

    19.37: How can I implement a delay, or time a user's response,
    with sub-second resolution?

    A: Unfortunately, there is no portable way. Routines you might
    look for on your system include clock(), delay(), ftime(),
    gettimeofday(), msleep(), nap(), napms(), nanosleep(),
    setitimer(), sleep(), Sleep(), times(), and usleep().
    (A function called wait(), however, is at least under Unix
    *not*
    what you want.) The select() and poll() calls (if available)
    can be pressed into service to implement simple delays.
    On MS-DOS machines, it is possible to reprogram the system
    timer
    and timer interrupts.

    Of these, only clock() is part of the ANSI Standard. The
    difference between two calls to clock() gives elapsed
    execution
    time, and may even have subsecond resolution, if
    CLOCKS_PER_SEC
    is greater than 1. However, clock() gives elapsed processor
    time used by the current program, which on a multitasking
    system
    may differ considerably from real time.

    If you're trying to implement a delay and all you have
    available
    is a time-reporting function, you can implement a CPU-
    intensive
    busy-wait, but this is only an option on a single-user,
    single-
    tasking machine, as it is terribly antisocial to any other
    processes. Under a multitasking operating system, be sure to
    use a call which puts your process to sleep for the duration,
    such as sleep() or select(), or pause() in conjunction with
    alarm() or setitimer().

    For really brief delays, it's tempting to use a do-nothing
    loop
    like

    long int i;
    for(i = 0; i < 1000000; i++)
    ;

    but resist this temptation if at all possible! For one thing,
    your carefully-calculated delay loops will stop working
    properly
    next month when a faster processor comes out. Perhaps worse,
    a
    clever compiler may notice that the loop does nothing and
    optimize it away completely.

    References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp.
    197-8,215-6; POSIX Sec. 4.5.2.
     
    user923005, Dec 27, 2007
    #16
  17. "CBFalconer" <> schrieb im Newsbeitrag
    news:...
    > Joachim Schmitz wrote:
    >> "Keith Thompson" <> schrieb im Newsbeitrag
    >>> "Joachim Schmitz" <> writes:
    >>>> "CBFalconer" <> schrieb im Newsbeitrag
    >>>>> Ivan Novick wrote:
    >>>>>> Richard Heathfield <> wrote:
    >>>>>>
    >>>>>>> Ideally, I would like the program to wait 5 minutes before
    >>>>>>> executing the rest of the code. Is there some way that I can
    >>>>>>> use time.h and just run a for loop that waits until 5 minutes
    >>>>>>> has passed to continue down the program?
    >>>>>>
    >>>>>> There is a sleep function in the GNU C library.
    >>>>>>
    >>>>>> http://www.gnu.org/software/libc/manual/html_node/Sleeping.html#Sleeping
    >>>>>
    >>>>> GNU C is not ISO standard C. Extensions are off-topic here.
    >>>>> There would have been no objection if you had made this clear.
    >>>>
    >>>> He did make it very clear that this is in the GNU C library.
    >>>> How could it possibly get any clearer?
    >>>
    >>> The GNU C library also has a strlen function.

    >>
    >> So what? Does that make strlen off topic here?

    >
    > No. But 'sleep' is off-topic. Have you caught on to the
    > difference yet?

    Of course I do and did. I was only pointing out that Ivan clearly stated
    where his sleep is from.
    Nothing else.

    Bye, Jojo
     
    Joachim Schmitz, Dec 27, 2007
    #17
  18. [snips]

    On Wed, 26 Dec 2007 23:09:13 +0000, SM Ryan wrote:

    > system("sleep 300");


    Wonder what effect that would have on systems:

    a) That have no sleep command
    b) That have a sleep, but the parameter is in minutes
    c) That have a sleep, but the parameter is in milliseconds
    d) That have a sleep, but the parameter is a resource or device ID and
    sleep waits for the resource to become available

    FWIW, Windows XP - arguably the most populous desktop OS - seems to have
    no bundled sleep command.
     
    Kelsey Bjarnason, Dec 30, 2007
    #18
  19. Not sure what you mean by bundled but the Windows API has a Sleep() function
    that has been available at least since Windows 95.

    Regards
    Chris Saunders

    "Kelsey Bjarnason" <> wrote in message
    news:...
    > [snips]
    >
    > On Wed, 26 Dec 2007 23:09:13 +0000, SM Ryan wrote:
    >
    >> system("sleep 300");

    >
    > Wonder what effect that would have on systems:
    >
    > a) That have no sleep command
    > b) That have a sleep, but the parameter is in minutes
    > c) That have a sleep, but the parameter is in milliseconds
    > d) That have a sleep, but the parameter is a resource or device ID and
    > sleep waits for the resource to become available
    >
    > FWIW, Windows XP - arguably the most populous desktop OS - seems to have
    > no bundled sleep command.
     
    Chris Saunders, Dec 31, 2007
    #19
  20. "Chris Saunders" <> writes:
    > "Kelsey Bjarnason" <> wrote in message

    [...]
    >> FWIW, Windows XP - arguably the most populous desktop OS - seems to have
    >> no bundled sleep command.

    >
    > Not sure what you mean by bundled but the Windows API has a Sleep()
    > function that has been available at least since Windows 95.


    Please don't top-post (corrected here).

    A Sleep() function is not the same as a "sleep" command.

    --
    Keith Thompson (The_Other_Keith) <>
    [...]
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 31, 2007
    #20
    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. Jason Shohet
    Replies:
    10
    Views:
    1,912
    Kevin Spencer
    Jun 18, 2004
  2. Rick
    Replies:
    4
    Views:
    41,593
    Roedy Green
    Oct 8, 2003
  3. Amit Nath

    Pause a program while running

    Amit Nath, Jun 29, 2004, in forum: C Programming
    Replies:
    10
    Views:
    608
    Wynand Winterbach
    Jul 21, 2004
  4. still me

    Pause Perl Program?

    still me, Jul 8, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    163
    Lambik
    Jul 8, 2007
  5. Tuxedo
    Replies:
    7
    Views:
    425
    J. Gleixner
    Aug 16, 2012
Loading...

Share This Page