468,491 Members | 1,991 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Making a program pause

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 '07 #1
19 3505
da*********@gmail.com 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
Dec 24 '07 #2
On Dec 24, 10:34 am, Richard Heathfield <r...@see.sig.invalidwrote:
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/man....html#Sleeping

Regards,
Ivan Novick
http://www.0x4849.net
Dec 25 '07 #3
Ivan Novick wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
>>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/man....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

Dec 25 '07 #4
In article <47***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Ivan Novick wrote:
....
>There is a sleep function in the GNU C library.

http://www.gnu.org/software/libc/man....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...

Dec 25 '07 #5
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:47***************@yahoo.com...
Ivan Novick wrote:
>Richard Heathfield <r...@see.sig.invalidwrote:
>>>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/man....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
Dec 26 '07 #6
Ivan Novick said:
On Dec 24, 10:34 am, Richard Heathfield <r...@see.sig.invalidwrote:
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
Dec 26 '07 #7
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:47***************@yahoo.com...
>Ivan Novick wrote:
>>Richard Heathfield <r...@see.sig.invalidwrote:

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/man....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/man...ng-Length.html

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
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"
Dec 26 '07 #8
"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
news:87************@kvetch.smov.org...
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:47***************@yahoo.com...
>>Ivan Novick wrote:
Richard Heathfield <r...@see.sig.invalidwrote:

>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/man....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/man...ng-Length.html
So what? Does that make strlen off topic here?

Bse, Jojo
Dec 26 '07 #9
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 '07 #10
"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
news:87************@kvetch.smov.org...
>"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>>"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:47***************@yahoo.com...
Ivan Novick wrote:
[...]
>>>>There is a sleep function in the GNU C library.
>
http://www.gnu.org/software/libc/man....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/man...ng-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) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 26 '07 #11
Joachim Schmitz wrote:
"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
>"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
>>"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
Ivan Novick wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
>
>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/man....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

Dec 26 '07 #12
CBFalconer <cb********@yahoo.comwrote:
# Ivan Novick wrote:
# Richard Heathfield <r...@see.sig.invalidwrote:
# >
# >>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/man....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.
Dec 26 '07 #13
SM Ryan <wy*****@tango-sierra-oscar-foxtrot-tango.fake.orgwrites:
CBFalconer <cb********@yahoo.comwrote:
Ivan Novick wrote:
Richard Heathfield <r...@see.sig.invalidwrote:
>
>>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/man....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) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 27 '07 #14
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
news:47***************@yahoo.com...
Joachim Schmitz wrote:
>"Keith Thompson" <ks***@mib.orgschrieb im Newsbeitrag
>>"Joachim Schmitz" <no*********@schmitz-digital.dewrites:
"CBFalconer" <cb********@yahoo.comschrieb im Newsbeitrag
Ivan Novick wrote:
>Richard Heathfield <r...@see.sig.invalidwrote:
>>
>>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/man....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
Dec 27 '07 #15
[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.
Dec 30 '07 #16
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" <kb********@gmail.comwrote in message
news:ho************@spanky.localhost.net...
[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.
Dec 31 '07 #17
"Chris Saunders" <ev**@mountaincable.netwrites:
"Kelsey Bjarnason" <kb********@gmail.comwrote 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) <ks***@mib.org>
[...]
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 31 '07 #18
On Dec 30, 10:12*pm, Keith Thompson <ks...@mib.orgwrote:
"Chris Saunders" <e...@mountaincable.netwrites:
"Kelsey Bjarnason" <kbjarna...@gmail.comwrote 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.
Besides which, there is a major distinction between a sleep command
and a sleep function.
There is a sleep command on my Windows machine, because I wrote it.
But for most Windows systems, if you execute this code snippet:

system("sleep 300");

there won't be any 3 second pause.
Dec 31 '07 #19
[snips]

On Sun, 30 Dec 2007 23:17:10 -0500, Chris Saunders wrote:
Not sure what you mean by bundled but the Windows API has a Sleep() function
that has been available at least since Windows 95.
Context:

system("sleep 300");

Explain to us how to invoke the Sleep() function, in a standard XP box,
using the code above.

Oh, right, you don't. XP has no sleep command, exactly as I said.
Jan 2 '08 #20

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ryan Spencer | last post: by
1 post views Thread by dan | last post: by
3 posts views Thread by Thomas Matthews | last post: by
10 posts views Thread by Amit Nath | last post: by
reply views Thread by FaroeIslander | last post: by
8 posts views Thread by Lloydm | last post: by
12 posts views Thread by greg | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.