pause for some secs?

K

Kapteyn's Star

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

Joachim Schmitz

Kapteyn's Star said:
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
 
K

Kapteyn's Star

Richard said:
Joachim Schmitz said:


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

santosh

Kapteyn's Star said:
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.
 
K

Kapteyn's Star

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

time_t inv = (time_t) -1;


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... :)
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;


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

Keith Thompson

Kapteyn's Star said:
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.
 
J

Joachim Schmitz

Richard said:
Joachim Schmitz said:


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
 
J

Joachim Schmitz

Joachim said:
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"
 
K

Keith Thompson

Richard Heathfield said:
santosh said:


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

Keith Thompson

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

time_t inv = (time_t) -1;

Not necessary.
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)".)
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>.
 
K

Kapteyn's Star

Keith said:
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.h> would 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.
 
K

Keith Thompson

Kapteyn's Star said:
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>.
 
K

Kapteyn's Star

Keith said:
Kapteyn's Star <[email protected]>
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.
 

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,770
Messages
2,569,586
Members
45,084
Latest member
HansGeorgi

Latest Threads

Top