|  | pause for some secs? |  | |
| | | Kapteyn's Star |  |
| Posted: Fri Jul 18, 2008 10:39 am Post subject: pause for some secs? |  |
Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
-- Kapteyn's Star |
| |
| | | Joachim Schmitz |  |
| Posted: Fri Jul 18, 2008 10:39 am Post subject: Re: pause for some secs? |  |
Kapteyn's Star wrote:
| Quote: | Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
|
Not possible in Standard C. In POSIX you could use sleep(3)
Bye, Jojo |
| |
| | | Richard Heathfield |  |
| Posted: Fri Jul 18, 2008 12:38 pm Post subject: Re: pause for some secs? |  |
Joachim Schmitz said:
| Quote: | Kapteyn's Star wrote: Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C.
|
Actually, you can do it, albeit rather lamely in a busy loop, using time().
<snip>
-- 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 |
| |
| | | Kapteyn's Star |  |
| Posted: Fri Jul 18, 2008 1:32 pm Post subject: Re: pause for some secs? |  |
Joachim Schmitz wrote:
| Quote: | Kapteyn's Star wrote: Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C. In POSIX you could use sleep(3)
Bye, Jojo
|
Thanks Joachim.
-- Kapteyn's Star |
| |
| | | Kapteyn's Star |  |
| Posted: Fri Jul 18, 2008 1:33 pm Post subject: Re: pause for some secs? |  |
Richard Heathfield wrote:
| Quote: | Joachim Schmitz said:
Kapteyn's Star wrote: Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C.
Actually, you can do it, albeit rather lamely in a busy loop, using time().
snip
|
Is this related to what you say?
#include <stdlib.h> #include <time.h>
int delay(int sec) { time_t t0; time_t t1; time_t inv = -1;
for(t0= t1= time(0); t0 != inv && t1 != inv && sec > difftime(t1, t0); t1= time(0)) { ; } return t0 == inv || t1 == inv; }
int main() { if(delay(3)) { return EXIT_FAILURE; } return 0; }
It seems to work...
-- Kapteyn's Star |
| |
| | | santosh |  |
| Posted: Fri Jul 18, 2008 2:24 pm Post subject: Re: pause for some secs? |  |
| |  | |
Kapteyn's Star wrote:
| Quote: | Richard Heathfield wrote:
Joachim Schmitz said:
Kapteyn's Star wrote: Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C.
Actually, you can do it, albeit rather lamely in a busy loop, using time().
snip
Is this related to what you say?
#include <stdlib.h #include <time.h
int delay(int sec) { time_t t0; time_t t1; time_t inv = -1;
|
You should cast the -1 to time_t before initialisation here:
time_t inv = (time_t) -1;
| Quote: | for(t0= t1= time(0); t0 != inv && t1 != inv && sec > difftime(t1, t0);
|
I would move some of this logic into the loop body, seeing as it is currently empty. Specifically, I would move the comparison involving difftime into the loop, with a break after the required time has elapsed.
| Quote: | t1= time(0)) { ; } return t0 == inv || t1 == inv; }
|
Ugh, not very readable IMHO. I would test t0 and t1 and set a dedicated return code object, which would later be returned.
if (t0 == inv || t1 == inv) retcode = FAILURE; /* ... */ return retcode;
| Quote: | int main() { if(delay(3)) { return EXIT_FAILURE; } return 0; }
It seems to work...
|
Yes. It should work, but it's a busy loop and computationally very wasteful. To sleep for some period, especially if it's longer than a second or so, it's best to use an implementation specific function. For POSIX, among others there are sleep and nanosleep. |
| |
| | | Richard Heathfield |  |
| Posted: Fri Jul 18, 2008 2:34 pm Post subject: Re: pause for some secs? |  |
| |  | |
santosh said:
| Quote: | Kapteyn's Star wrote:
snip |
| Quote: | #include <stdlib.h #include <time.h
int delay(int sec) { time_t t0; time_t t1; time_t inv = -1;
You should cast the -1 to time_t before initialisation here:
|
Why not just write it so that a cast is not required?
#include <stdio.h> #include <time.h>
void delay(int sec) { time_t t0 = time(NULL); time_t t1 = time(NULL); while(difftime(t1, t0) < sec) { t1 = time(NULL); } }
<snip>
| Quote: | I would move some of this logic into the loop body, seeing as it is currently empty. Specifically, I would move the comparison involving difftime into the loop, with a break after the required time has elapsed.
|
Why?
<snip>
| Quote: | Yes. It should work, but it's a busy loop and computationally very wasteful. To sleep for some period, especially if it's longer than a second or so, it's best to use an implementation specific function. For POSIX, among others there are sleep and nanosleep.
|
Agreed, iff POSIX is available on all target platforms.
-- 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 |
| |
| | | Eric Sosman |  |
| Posted: Fri Jul 18, 2008 2:37 pm Post subject: Re: pause for some secs? |  |
santosh wrote:
| Quote: | Kapteyn's Star wrote: [...] time_t t0; time_t t1; time_t inv = -1;
You should cast the -1 to time_t before initialisation here:
time_t inv = (time_t) -1;
|
Why?
-- Eric.Sosman@sun.com |
| |
| | | Kapteyn's Star |  |
| Posted: Fri Jul 18, 2008 3:44 pm Post subject: Re: pause for some secs? |  |
| |  | |
santosh wrote:
| Quote: | Kapteyn's Star wrote:
Richard Heathfield wrote:
Joachim Schmitz said:
Kapteyn's Star wrote: Hello all,
I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C.
Actually, you can do it, albeit rather lamely in a busy loop, using time().
snip
Is this related to what you say?
#include <stdlib.h #include <time.h
int delay(int sec) { time_t t0; time_t t1; time_t inv = -1;
You should cast the -1 to time_t before initialisation here:
time_t inv = (time_t) -1;
for(t0= t1= time(0); t0 != inv && t1 != inv && sec > difftime(t1, t0);
I would move some of this logic into the loop body, seeing as it is currently empty. Specifically, I would move the comparison involving difftime into the loop, with a break after the required time has elapsed.
|
okay thanks. But Richard's version is so much more elegant! I wish i had thought of that... :)
| Quote: | t1= time(0)) { ; } return t0 == inv || t1 == inv; }
Ugh, not very readable IMHO. I would test t0 and t1 and set a dedicated return code object, which would later be returned.
if (t0 == inv || t1 == inv) retcode = FAILURE; /* ... */ return retcode;
int main() { if(delay(3)) { return EXIT_FAILURE; } return 0; }
It seems to work...
Yes. It should work, but it's a busy loop and computationally very wasteful. To sleep for some period, especially if it's longer than a second or so, it's best to use an implementation specific function. For POSIX, among others there are sleep and nanosleep.
|
I will consult the man pages for sleep/naosleep. Thanks.
-- Kapteyn's Star |
| |
| | | Keith Thompson |  |
| Posted: Fri Jul 18, 2008 4:09 pm Post subject: Re: pause for some secs? |  |
| |  | |
Kapteyn's Star <remove_digits_for_email_7kapteyns3.star@g0m8ai2l.9com> writes:
| Quote: | Richard Heathfield wrote: Joachim Schmitz said: Kapteyn's Star wrote: I want a function to make the program pause for a few seconds but K&R have not listed anything for this i even looked in index. can anyone help? Thank you very much.
Not possible in Standard C.
Actually, you can do it, albeit rather lamely in a busy loop, using time().
snip
Is this related to what you say?
#include <stdlib.h #include <time.h
int delay(int sec) { time_t t0; time_t t1; time_t inv = -1;
for(t0= t1= time(0); t0 != inv && t1 != inv && sec > difftime(t1, t0); t1= time(0)) { ; } return t0 == inv || t1 == inv; }
int main() { if(delay(3)) { return EXIT_FAILURE; } return 0; }
It seems to work...
|
Yes, it *seems* to work, but it has several practical problems.
The resolution of the time() function is not specified by the standard. If it's 1 second (as is typical), then delay(2) could delay for any length of time from just over 2 seconds to just over 3 seconds ("just over" because of overhead). If the resolution is anything else, that will affect the behavior.
Furthermore, if you call diff(3), then your program will spend roughly 3 seconds doing nothing useful, but doing it in an extraordinarily inefficient manner. It will repeatedly call the time() and difftime() functions just as quickly as it can, consuming CPU time to do it. This might be acceptable on an embedded system with no other programs running, but on a typical multi-user system, or even on a single-user multi-processing system, it could have serious effects on system performance.
Sleeping for a specified length of time is something that *can* be done in portable standard C, but only very badly. Or it can (almost always) be done very simply and straightforwardly with some non-portable call, with minimal overhead and much better precision.
If you have a real need to sleep for 3 seconds, it's likely (but not certain) that your program is performing some other system-specific action as well; in that case, using a system-specific call won't result in any additional loss of portability.
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|