By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,738 Members | 1,730 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,738 IT Pros & Developers. It's quick & easy.

pause for some secs?

P: n/a
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

Jul 18 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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
Jul 18 '08 #2

P: n/a
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>

--
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
Jul 18 '08 #3

P: n/a
Joachim Schmitz wrote:
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

Jul 18 '08 #4

P: n/a
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;

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

Jul 18 '08 #5

P: n/a
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.
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.

Jul 18 '08 #6

P: n/a
santosh said:
Kapteyn's Star wrote:
<snip>
>#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>
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>
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
Jul 18 '08 #7

P: n/a
santosh wrote:
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?

--
Er*********@sun.com
Jul 18 '08 #8

P: n/a
santosh wrote:
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... :)
> 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

Jul 18 '08 #9

P: n/a
Kapteyn's Star <re*************************************@g0m8ai2l. 9comwrites:
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) ks***@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"
Jul 18 '08 #10

P: n/a
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().
Hmm, 'busy loop' and 'pause' don't go together too well, do they? The
program will not pause, just spend it'd someplace else, which isn't quite
the same thing IMHO.
POSIX sleep will _suspend_ the process, allowing others (Users, processes)
to utilize the CPU. Comes much closer to the concept of a pause...

Bye, Jojo
Jul 18 '08 #11

P: n/a
Joachim Schmitz 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().

Hmm, 'busy loop' and 'pause' don't go together too well, do they? The
program will not pause, just spend it'd someplace else, which isn't
"it'd" - "it's time"
quite the same thing IMHO.
POSIX sleep will _suspend_ the process, allowing others (Users,
processes) to utilize the CPU. Comes much closer to the concept of a
pause...
Bye, Jojo

Jul 18 '08 #12

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:
>Kapteyn's Star wrote:
<snip>
>>#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?
As Kapteyn's Star already did.

Given:

time_t inv = -1;

-1 is of type int, which is converted to time_t. A cast would specify
exactly the same conversion, and is (like most casts) unnecessary.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jul 18 '08 #13

P: n/a
santosh <sa*********@gmail.comwrites:
Kapteyn's Star wrote:
>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;

You should cast the -1 to time_t before initialisation here:

time_t inv = (time_t) -1;
Not necessary.
> 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.
I don't think I would. This is a busy loop; it makes sense for the
body to be empty, and for the termination condition to be in the loop
header.

(But I'd write "time(NULL)" rather than "time(0)".)
> 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;
The only problem is that the function returns a true value on failure,
and a false value on success. That doesn't match the function's name
very well. But it's probably ok if (and only if) the convention is
clearly documented.

Given that convention, I think that

return t0 == inv || t1 == inv;

is sufficiently clear -- except that I'd probably call it "invalid"
rather than "inv".

[snip]

But of course the whole thing is almost certainly a bad idea, as you
pointed out in the text that I cruelly snipped. A system small enough
for this kind of busy loop to make sense is likely to be a
freestanding implementation, and might not provide <time.h>.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jul 18 '08 #14

P: n/a
Keith Thompson wrote:
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.
Actually im trying to write a program that will diconnect and then
redial my dsl connection. So i want to pause for 3 secs between
disconnect and reconnect. I thought <time.hwould have something but
after the advices im now using sleep(3) as suggested.

Now if i can ask something not related to the thread subject... in this
program I call the system commands 'poff -a' and 'pon dsl-provider' to
do the job. But the problem is the very first disconnect after systme
bootup cant be done by regular user and i actually need to issue the
command 'sudo poff -a' and for all these commands im using the system()
function. But sudo asks for a password and i need to type it from the
keyboard but i want this program to run even when im away from the
computer. Is there a portable way to specify the password to sudo
without it waiting on the keyboard.

The only thing i can think is to write the password to a temporary file
created with tempnam() and the do system("sudo poff -a < tempfile) and
then delete the file afterwards.

Is there a better ANSI C way?? thanks in advance and sorry for the topic
change.
--
Kapteyn's Star

Jul 19 '08 #15

P: n/a
Kapteyn's Star <re*************************************@g0m8ai2l. 9comwrites:
[...]
Now if i can ask something not related to the thread subject... in this
program I call the system commands 'poff -a' and 'pon dsl-provider' to
do the job. But the problem is the very first disconnect after systme
bootup cant be done by regular user and i actually need to issue the
command 'sudo poff -a' and for all these commands im using the system()
function. But sudo asks for a password and i need to type it from the
keyboard but i want this program to run even when im away from the
computer. Is there a portable way to specify the password to sudo
without it waiting on the keyboard.

The only thing i can think is to write the password to a temporary file
created with tempnam() and the do system("sudo poff -a < tempfile) and
then delete the file afterwards.

Is there a better ANSI C way?? thanks in advance and sorry for the topic
change.
No, there isn't. Standard C (btw, it's more precisely referred to as
ISO c rather than ANSI C) doesn't have any concept of users.

What you're doing is very system-specific. I suggest asking in
comp.unix.programmer <OT>where somebody might mention "cron"</OT>.

--
Keith Thompson (The_Other_Keith) ks***@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"
Jul 19 '08 #16

P: n/a
Keith Thompson wrote:
Kapteyn's Star <re*************************************@g0m8ai2l. 9com>
writes:
[...]
>Now if i can ask something not related to the thread subject... in
this program I call the system commands 'poff -a' and 'pon
dsl-provider' to do the job. But the problem is the very first
disconnect after systme bootup cant be done by regular user and i
actually need to issue the command 'sudo poff -a' and for all these
commands im using the system() function. But sudo asks for a password
and i need to type it from the keyboard but i want this program to
run even when im away from the computer. Is there a portable way to
specify the password to sudo without it waiting on the keyboard.

The only thing i can think is to write the password to a temporary
file created with tempnam() and the do system("sudo poff -a <
tempfile) and then delete the file afterwards.

Is there a better ANSI C way?? thanks in advance and sorry for the
topic change.

No, there isn't. Standard C (btw, it's more precisely referred to as
ISO c rather than ANSI C) doesn't have any concept of users.

What you're doing is very system-specific. I suggest asking in
comp.unix.programmer <OT>where somebody might mention "cron"</OT>.
Thank you, I will do that.
--
Kapteyn's Star

Jul 19 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.