"Vladimir Oka" <novine@btopenworld.com> writes:
Gaijinco wrote:
How can I write a program to be executed for a given ammount of time.
I mean I want to write a program that once started it doesn't do
anything for like 2 minutes and then exits.
It is possible?
Maybe. Depends on your exact requirements. Standard C has <time.h>
header which shoudl contain stuff you'd need. Look it up, but beware:
not many things are guaranteed. The stuff you'd likely need is:
CLOCKS_PER_SEC
and
clock_t clock(void);
which returns the number of clock cycles since the start of your
program. Divide by CLOCKS_PER_SEC to get to the seconds. The function
returns `(clock_t)(-1)` if the information is not available or can't be
represented.
<time.h> doesn't provide a function that sleeps for a specified amount
of time. You could write a busy loop that runs until the current time
reaches a specified value, but that's a really bad idea on a
multi-processing system; while it's looping, your program will consume
CPU time at the expense of other processes on the system.
The clock() function returns an indication of the amount of CPU time
your program has consumed; it doesn't indicate real time.
Of course, if you don't care about portability, you can always use
whatever's available and specific to your platform. You should ask
about these in an appropriate group (this one not being appropriate for
platform specific questions).
Sleeping for a specified number of seconds is one of those things that
can be done much better using non-portable code. Most systems will
provide something like a sleep() function.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Windows (cl ... /D"WIN32 ..."):
....
....
for simple programs.
Stephen W. Vickers