Making a program pause

D

dave.zoltan

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
 
R

Richard Heathfield

(e-mail address removed) 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.
 
K

Kenny McCormack

Ivan Novick wrote: ....

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...
 
G

Geoff

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.
 
J

Joachim Schmitz

CBFalconer said:
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
 
R

Richard Heathfield

Ivan Novick said:

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().
 
D

dave.zoltan

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
 
K

Keith Thompson

Joachim Schmitz said:
Keith Thompson said:
Joachim Schmitz said:
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).
 
S

SM Ryan

# Ivan Novick 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");
 
K

Keith Thompson

SM Ryan said:
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.
 
U

user923005

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.
 
J

Joachim Schmitz

CBFalconer said:
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
 
K

Kelsey Bjarnason

[snips]

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.
 
C

Chris Saunders

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 said:
[snips]

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.
 
K

Keith Thompson

Chris Saunders said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top