473,390 Members | 901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 3963
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Spencer | last post by:
Hello again Everyone, I spent a small amount of time recently writing a program that "blinks" text. I did it by first having a user input a string, sys.stdout.write-ing the string, then backing...
1
by: dan | last post by:
I need help in figuring out what to do for this program, it is a program that reads book id, unit price, book purchased, book sold, book returned, and city(respectively). It is a control break...
3
by: Thomas Matthews | last post by:
Is there anything technically wrong with the code below? I compiled it with g++ 3.3.1 and it executes with no errors. I compiled it with bcc32 5.6 and it executes with "This program has performed...
10
by: Amit Nath | last post by:
Hi! I am running a C program and need to pause the program and change some of the variables. Is there any function that checks if there is a character in the Standard Input Buffer, else the...
0
by: wayne hamilton | last post by:
I'm having a problem Interacting with Command Line programs. I can read and write anything I want as long as I don't have to interact with a process once it's started. Originally I had been calling...
0
by: FaroeIslander | last post by:
Hello all. I am currently working on a program in VB.NET where the user can run a "hidden" DOS program and as the program is running the user can see how many computations have been performed by...
8
by: Lloydm | last post by:
I've used the command "pause" in batch files which works ok but I guess it's applied dirrently in c++. Using Dev C++ 4.0 and the following is my code #include<iostream> #include<iomanip>...
12
by: greg | last post by:
Hi, Can anyone help me with the following issue: How can I pause the execution of a program until a given file is created (by another process) in a specified directory? Any ideas would be...
4
by: Break2 | last post by:
I am trying to write a simple program which asks a user to enter 5 integers after which the average will be computed and written to the screen. That simple. However I want to do it according to a...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.