471,575 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,575 software developers and data experts.

How to "sleep" or "delay"?

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
8 14551

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

Similar topics

4 posts views Thread by Shuo Xiang | last post: by
7 posts views Thread by John | last post: by
9 posts views Thread by John Walker | last post: by
5 posts views Thread by Sweet.mhtrq | last post: by
2 posts views Thread by Lou | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
1 post views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.