Timer

G

Gaijinco

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?
 
V

Victor Bazarov

Gaijinco said:
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?

It is possible if it's possible in the OS you're using. You need to see
what "sleep" functionality your OS provides. Ask in the newsgroup
dedicated to your OS.

In C or C++ "doesn't do anything" is impossible. The only "delay" you
can implement using standard means of either language is in line with

/* take time reading, use 'time' */
for (;;)
{
/* take another time reading */
/* check if time isn't over, use 'difftime' */
/* if it's over, break; */
}

but it definitely does not qualify as "doing nothing".

V
 
I

invisal

Gaijinco said:
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?

Hi I am new here. If you are programming with window platform. you can
used Sleep function from the windows.h heander. Which look something
like this:

#include <iostream>
#include <windows.h>

int main()
{
std::cout << "Timer started! \n";
Sleep(120000);
std:;cout << "2 Minutes is over! \n"
}

Sleep will pause the whole program. Sleep(1000) mean pause for 1
second, Sleep(1) mean pause for 1milli-second.

I hope this information might help you.

From
Visal .In
 
V

Vladimir Oka

Gaijinco said:
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.

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

Vladimir Oka

Hi I am new here. If you are programming with window platform. you can
used Sleep function from the windows.h heander. Which look something
like this:

#include <iostream>
From this point on, it belongs to comp.lang.c++ (although I guess
platform specific stuff is off-topic there as well).

Followups-to set...
 
C

CBFalconer

Gaijinco said:
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?

Invoke the 'as if' rule from the standard. Do nothing. Stare at
the terminal for two minutes, then repeat doing nothing. QED.
Very economical and environmentally friendly.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
 
K

Keith Thompson

Vladimir Oka said:
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.
 
S

siska

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


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.

I use this sometimes when writing short programs for
AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
Windows (cl ... /D"WIN32 ..."):

***CODE EXAMPLE***

/* file to include - in whatever file it is needed */
#ifdef WIN32
#include <windows.h>
#else /* WIN32 */
#include <unistd.h>
#endif /* WIN32 */

....

/* define the function to use */
#ifdef WIN32
#define SLEEP_TIME 5000
#define SLEEP_FUNC Sleep
#else /* WIN32 */
#define SLEEP_TIME 5
#define SLEEP_FUNC sleep
#endif /* WIN32 */

....

/* someone deep in the code the function is used */
if( wait_for_something == true )
{
SLEEP_FUNC ( SLEEP_TIME );
}

***CODE EXAMPLE***

Of course this may not be the safest or best way to do it but it works
for simple programs.

Stephen W. Vickers
 
K

Keith Thompson

siska said:
Keith Thompson wrote: [...]
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.
[...]

Thank you for quoting context in spite of the Google Groups interface.

But it's seldom necessary to quote the entire article to which you're
replying. Please trim anything that's not relevant to your reply. In
particular, don't quote signatures unless you're commenting on them.
I use this sometimes when writing short programs for
AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
Windows (cl ... /D"WIN32 ..."):

***CODE EXAMPLE***

/* file to include - in whatever file it is needed */
#ifdef WIN32
#include <windows.h>
#else /* WIN32 */
#include <unistd.h>
#endif /* WIN32 */

...

/* define the function to use */
#ifdef WIN32
#define SLEEP_TIME 5000
#define SLEEP_FUNC Sleep
#else /* WIN32 */
#define SLEEP_TIME 5
#define SLEEP_FUNC sleep
#endif /* WIN32 */

...

/* someone deep in the code the function is used */
if( wait_for_something == true )
{
SLEEP_FUNC ( SLEEP_TIME );
}

***CODE EXAMPLE***

Of course this may not be the safest or best way to do it but it works
for simple programs.

That will work (I presume) if you happen to compile your program on a
Windows or Unix-like system. It's not portable to any other systems,
so it's off-topic here.

Also, why do you write

if( wait_for_something == true )

? Where is the identifier "true" defined? If you're using C99, it's
in <stdbool.h>; if you're using C++, it's keyword, but C++ is
off-topic here in comp.lang.c. (I just noticed that this is
cross-posted to comp.lang.c and comp.lang.c++; that's hardly ever a
good idea.)

If a variable represents a condition, it's far better to use it
directly as a condition:

if (wait_for_something)
{
...
}

Consider what happens if wait_for_something is an int with a value
other than 0 or 1.

And if you think that "wait_for_something == true" is clearer than
"wait_for_something", wouldn't "(wait_for_something == true) == true"
be better yet?

See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.
 
J

Jim Langston

Keith Thompson said:
And if you think that "wait_for_something == true" is clearer than
"wait_for_something", wouldn't "(wait_for_something == true) == true"
be better yet?

See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

I treat that as preference. What is the difference between:
if ( wait_for_something )
and
if ( wait_for_something == true )

The biggest difference is that me, looking at this code, immediately know in
the second form that wait_for_something is a boolean value. In the first
form I don't know if it's an integer or char or perhaps even something else.
I'm one who believe in self documenting code.

Yes, I would probably write
if ( wait_for_something )
only if wait_for_something was obviously a boolean value.
 
K

Keith Thompson

Jim Langston said:
I treat that as preference. What is the difference between:
if ( wait_for_something )
and
if ( wait_for_something == true )

The biggest difference is that me, looking at this code, immediately know in
the second form that wait_for_something is a boolean value. In the first
form I don't know if it's an integer or char or perhaps even something else.
I'm one who believe in self documenting code.

Yes, I would probably write
if ( wait_for_something )
only if wait_for_something was obviously a boolean value.

The difference is that they don't mean the same thing.

This:
if (wait_for_something)
is *not* equivalent to this:
if (wait_for_something == true)
It's equivalent to this:
if (wait_for_something != 0)

If you restrict yourself to _Bool (C99) or bool (C++), you can
probably get away with assuming that the value will always be 0 or 1.
But if wait_for_something is an int being used to indicate a
condition, any non-zero value is treated as true; the is*() function
in <ctype.h>, for example, can return any non-zero value for true.

Note that this is cross-posted to comp.lang.c and comp.lang.c++.
Since C++ has had a built-in bool type longer than C has, this
argument may not be as strong for C++ as it is for C. But equality
comparisons to true or false are still a bad idea.
 
J

Jim Langston

Keith Thompson said:
The difference is that they don't mean the same thing.

This:
if (wait_for_something)
is *not* equivalent to this:
if (wait_for_something == true)
It's equivalent to this:
if (wait_for_something != 0)

If you restrict yourself to _Bool (C99) or bool (C++), you can
probably get away with assuming that the value will always be 0 or 1.
But if wait_for_something is an int being used to indicate a
condition, any non-zero value is treated as true; the is*() function
in <ctype.h>, for example, can return any non-zero value for true.

Note that this is cross-posted to comp.lang.c and comp.lang.c++.
Since C++ has had a built-in bool type longer than C has, this
argument may not be as strong for C++ as it is for C. But equality
comparisons to true or false are still a bad idea.

Yes. In C I wouldn't compare against anything, since C doesn't have a true
boolean value. I responded to this in c.l.c++ and didnt' realize it was
cross posted.

I *might* do if ( wait_for_something != 0 ) because of a history I've had
with comparing integers as boolean in a language that didn't have a boolean
and, only a bitwise and.
 
V

Vladimir Oka

Keith Thompson opined:
<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.

OP's question was ambiguous. He asked for both "a program to be
executed for a given amount of time", and "a program that once started
it doesn't do anything for like 2 minutes and then exits".

The former one is well catered for by the <time.h> and what it
provides. The latter depends on what exactly the OP means by "doesn't
do anything". At the assumed level OP's of experience, "sits in a
tight loop" I though was a good guess.

--
"Are [Linux users] lemmings collectively jumping off of the cliff of
reliable, well-engineered commercial software?"
(By Matt Welsh)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
 
C

Charles Richmond

CBFalconer said:
Invoke the 'as if' rule from the standard. Do nothing. Stare at
the terminal for two minutes, then repeat doing nothing. QED.
Very economical and environmentally friendly.
Want to see my fast draw??? Want to see it again???
 
S

siska

Wow I had no idea so many people cared.

The code was just a snippet - so:

1. I did not imply what type "wait_for_somthing" was.
2. What the type or value of "true" was.

This was merely an example of "if we should wait for something then
sleep for some time" - no full program/code was given so why so many
people take it so literally I don't know.

For all of those who can't wait to post and make others feel like they
know nothing - ***just chill*** I'm just trying to help other newbies
learn the great program of "c".

***GIVE EVERYONE A BREAK***

Assumptions ... assumptions, if "true" is this then blah blah blah, and
if "true" is this then blah blah......

Did I say it was completely portable - NO - I said:

***QUOTE***

I use this sometimes when writing short programs for
AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
Windows (cl ... /D"WIN32 ..."):

***QUOTE***

So:

***QUOTE***

That will work (I presume) if you happen to compile your program on a
Windows or Unix-like system. It's not portable to any other systems,
so it's off-topic here.

***QUOTE***

While being true is a bit irrelevent - the part about portability that
is. What about this:

***QUOTE***

? Where is the identifier "true" defined? If you're using C99, it's
in <stdbool.h>; if you're using C++, it's keyword, but C++ is
off-topic here in comp.lang.c. (I just noticed that this is
cross-posted to comp.lang.c and comp.lang.c++; that's hardly ever a
good idea.)

***QUOTE***

Who the hell said anything about c++? If I was talking c++ I would
have posted in the c++ forum! Just because I used "true" doesnt' meen
I am talking c++, what if that of "true" is defined as 26?

Steve
 
F

Flash Gordon

siska said:
Wow I had no idea so many people cared.

People in comp.lang.c generally care about accuracy.
The code was just a snippet - so:

1. I did not imply what type "wait_for_somthing" was.

By naming it as you did you implied that it acts as a boolean function
returning some indication of whether "something" has happened.
2. What the type or value of "true" was.

The implication is in it's name.

Had you use "x == y" there would have been no implication that you were
dealing with some form of boolean.
This was merely an example of "if we should wait for something then
sleep for some time" - no full program/code was given so why so many
people take it so literally I don't know.

Why should a short snippet be allowed to have more problems than a large
snippet or complete program?
For all of those who can't wait to post and make others feel like they
know nothing - ***just chill*** I'm just trying to help other newbies
learn the great program of "c".

No, people don't want to make others think they know nothing. They want
to help people become better programmers, and unless someone knows what
they do wrong how will they learn not to do it?
***GIVE EVERYONE A BREAK***

Assumptions ... assumptions, if "true" is this then blah blah blah, and
if "true" is this then blah blah......

What people made were very important points. People here have come
across *real* situations where the type of code you showed was use and
it FAILED because the author did not understand the problems.
Did I say it was completely portable - NO - I said:

***QUOTE***

I use this sometimes when writing short programs for
AIX/HPUX/Linux/Solaris and Windows, I define "WIN32" when compiling on
Windows (cl ... /D"WIN32 ..."):

***QUOTE***

So:

***QUOTE***

That will work (I presume) if you happen to compile your program on a
Windows or Unix-like system. It's not portable to any other systems,
so it's off-topic here.

***QUOTE***

While being true is a bit irrelevent - the part about portability that
is. What about this:

No, the part about portability is COMPLETELY relevant. There is a vast
amout of SW development done in C for systems other than Windows and *nix.
***QUOTE***

? Where is the identifier "true" defined? If you're using C99, it's
in <stdbool.h>; if you're using C++, it's keyword, but C++ is
off-topic here in comp.lang.c. (I just noticed that this is
cross-posted to comp.lang.c and comp.lang.c++; that's hardly ever a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
good idea.)

***QUOTE***

Who the hell said anything about c++? If I was talking c++ I would
have posted in the c++ forum!

You *did* post in a C++ news group as is pointed out in the text you
quoted above.
> Just because I used "true" doesnt' meen
I am talking c++, what if that of "true" is defined as 26?

Then you should let people know what it is defined as, because if it is
26 then your SW is even worse.

If you don't like people pointing out problems with code you post (and
the problem you are complaining about being pointed out is a very real
problem), then I would advise against posting code to comp.lang.c since
one of the valuable services people provide is analysing code and
pointing out all the problems they can in it.
 
K

Keith Thompson

siska said:
Wow I had no idea so many people cared.

The code was just a snippet - so:

1. I did not imply what type "wait_for_somthing" was.
2. What the type or value of "true" was.

[big snip]
***QUOTE***

? Where is the identifier "true" defined? If you're using C99, it's
in <stdbool.h>; if you're using C++, it's keyword, but C++ is
off-topic here in comp.lang.c. (I just noticed that this is
cross-posted to comp.lang.c and comp.lang.c++; that's hardly ever a
good idea.)

***QUOTE***

Who the hell said anything about c++? If I was talking c++ I would
have posted in the c++ forum! Just because I used "true" doesnt' meen
I am talking c++, what if that of "true" is defined as 26?

It would be polite to indicate who you're quoting.

You *did post to the c++ forum (perhaps not intentionally). Take a
look at the Newsgroups header; this thread is cross-posted to
comp.lang.c and comp.lang.c++. Assuming that you might be using C++
(note that I wrote "*if* you're using C++) isn't much of a stretch.
Even if you had posted only to comp.lang.c, it still wouldn't have
been much of a stretch; many people (inappropriately) post C++ code to
comp.lang.c.

And if you're using C, defining the identifier "true" yourself is
still a bad idea, since C99's <stdbool.h> header defines "true" as a
macro, which expands to the integer constant 1. It won't cause any
real problems if you use a C90 implementation, or if you don't use
"#include <stdbool.h>", but it's still best to avoid it. If you want
to define your own Boolean type in C90, you can do something like
this:
typedef enum { FALSE, TRUE } BOOL;
or this:
typedef int BOOL;
#define FALSE 0
#define FALSE 1
See also section 9 of the comp.lang.c FAQ.

(C++, building on the experience of C, avoided this problem by
defining bool, false, and true as keywords from the very beginning.
Unfortunately, making such a change in C would have broken existing
code.)

You posted code (possibly pseudo-code) that included the line

if( wait_for_something == true )

I pointed out that that's a bad idea, and explained why. I don't know
why you seem to have a problem with that; somebody else might learn
from it, even if you don't.

If you post code here, people will comment on it. If don't want
people commenting on your code, don't post it.
 

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

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,511
Latest member
svareza

Latest Threads

Top