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

How to "sleep" or "delay"?

P: n/a
Hi all:

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

Thanks
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

As far as I remember: There is one, but I don't remember its name.
It might be wait(\\).
Any reference on C should list it in one of the libraries possibly.

amit.

John wrote:
Hi all:

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

Thanks


Nov 14 '05 #2

P: n/a
On 2 Mar 2004 22:32:34 -0800, jo*********@yahoo.com (John) wrote in
comp.lang.c:
Hi all:

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

Thanks


There is no such function in standard C.

For Linux questions, news:comp.os.linux.development.apps.

Or news:comp.unix.programmer.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3

P: n/a
John wrote:
Hi all:

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

Thanks


You can use the localtime() function combined with the time() function
to get the current time. You can poll on those until the delay is up.

Be aware that both those functions are allowed to fail.

--
Pull out a splinter to reply.
Nov 14 '05 #4

P: n/a
Amit Bhatia <bh*****@nospam.co> writes:
John wrote:
Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

As far as I remember: There is one, but I don't remember its name.
It might be wait(\\).


Misleading and incorrect answers like this are an excellent
reason not to post off-topic questions.
Nov 14 '05 #5

P: n/a
Thanks.

Peter Ammon <ge******@splintermac.com> wrote in message news:<9y******************@newssvr29.news.prodigy. com>...
John wrote:
Hi all:

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?

Thanks


You can use the localtime() function combined with the time() function
to get the current time. You can poll on those until the delay is up.

Be aware that both those functions are allowed to fail.

Nov 14 '05 #6

P: n/a
My sincere apologies for the same.

--A.
As far as I remember: There is one, but I don't remember its name.
It might be wait(\\).


Misleading and incorrect answers like this are an excellent
reason not to post off-topic questions.

Nov 14 '05 #7

P: n/a
Peter Ammon <ge******@splintermac.com> writes:
John wrote:
Hi all:
Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?
Thanks


You can use the localtime() function combined with the time() function
to get the current time. You can poll on those until the delay is up.

Be aware that both those functions are allowed to fail.


Something like that is probably the only *portable* way to delay for a
specified time interval. You don't really need localtime(); you can
do it with time() and difftime(). For example, the following program
should sleep for about 3 seconds:

#include <stdlib.h>
#include <time.h>

int main(void)
{
time_t start_time = time(NULL);
while (difftime(time(NULL), start_time) < 3.0) {
;
}
return 0;
}

I've omitted any error checking. Even if it works, the amount of time
it sleeps is likely to be imprecise; the standard doesn't specify the
precision of the time() function, and one second is typical.

On many systems, this technique has some severe drawbacks. On a Linux
or other Unix-like system, or on any multi-process system, the
continuous polling is likely to cause the program to gobble up nearly
100% of the CPU time. What you almost certainly really want your
program to do is go to sleep for a specified interval, allowing other
processes to run until your program wakes up.

This concern is not addressed by the C standard because the standard
is not concerned with multi-processing systems, but it's something you
should think about.

There are system-specific functions that will do exactly what you
want. You can probably get more information about such functions from
your system's documentation or, if that fails, by asking in a
system-specific newsgroup such as comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #8

P: n/a
jo*********@yahoo.com (John) wrote in message news:<c3**************************@posting.google. com>...

Is there a C function to make a procedure sleep or delay for a few
seconds/minutes on Linux and Sun OS platform?


Look up sleep(), usleep() and nanosleep().

On Windows see Sleep().
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.