Timer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gaijinco

    Timer

    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?

  • Victor Bazarov

    #2
    Re: Timer

    Gaijinco wrote:[color=blue]
    > 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?[/color]

    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • invisal@gmail.com

      #3
      Re: Timer


      Gaijinco wrote:[color=blue]
      > 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?[/color]

      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

      Comment

      • Vladimir Oka

        #4
        Re: Timer


        Gaijinco wrote:[color=blue]
        > 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?[/color]

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

        Comment

        • Vladimir Oka

          #5
          Re: Timer


          invi...@gmail.c om wrote:[color=blue]
          > Gaijinco wrote:[color=green]
          > > 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?[/color]
          >
          > 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>[/color]
          [color=blue]
          >From this point on, it belongs to comp.lang.c++ (although I guess[/color]
          platform specific stuff is off-topic there as well).

          Followups-to set...
          [color=blue]
          > #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[/color]

          Comment

          • Keith Thompson

            #6
            Re: Timer

            "Vladimir Oka" <novine@btopenw orld.com> writes:[color=blue]
            > Gaijinco wrote:[color=green]
            >> 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?[/color]
            >
            > 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.[/color]

            <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.
            [color=blue]
            > 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).[/color]

            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_Keit h) 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.

            Comment

            • siska

              #7
              Re: Timer


              Keith Thompson wrote:[color=blue]
              > "Vladimir Oka" <novine@btopenw orld.com> writes:[color=green]
              > > Gaijinco wrote:[color=darkred]
              > >> 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?[/color]
              > >
              > > 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.[/color]
              >
              > <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.
              >[color=green]
              > > 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).[/color]
              >
              > 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_Keit h) 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.[/color]

              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_someth ing == 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

              Comment

              • Keith Thompson

                #8
                Re: Timer

                "siska" <stephenwvicker s@hotmail.com> writes:[color=blue]
                > Keith Thompson wrote:[/color]
                [...][color=blue][color=green]
                >> 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.[/color][/color]
                [...]

                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.
                [color=blue]
                > 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_someth ing == 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.[/color]

                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_someth ing == 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_somet hing)
                {
                ...
                }

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

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

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

                --
                Keith Thompson (The_Other_Keit h) 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.

                Comment

                • Jim Langston

                  #9
                  Re: Timer

                  "Keith Thompson" <kst-u@mib.org> wrote in message
                  news:ln64kvucok .fsf@nuthaus.mi b.org...[color=blue]
                  > "siska" <stephenwvicker s@hotmail.com> writes:[color=green]
                  >> Keith Thompson wrote:[/color]
                  > And if you think that "wait_for_somet hing == true" is clearer than
                  > "wait_for_somet hing", wouldn't "(wait_for_some thing == true) == true"
                  > be better yet?
                  >
                  > See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.[/color]

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

                  The biggest difference is that me, looking at this code, immediately know in
                  the second form that wait_for_someth ing 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_someth ing )
                  only if wait_for_someth ing was obviously a boolean value.



                  Comment

                  • CBFalconer

                    #10
                    Re: Timer

                    Gaijinco wrote:[color=blue]
                    >
                    > 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?[/color]

                    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.c om, 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/googlegroupsrep ly/>


                    Comment

                    • Keith Thompson

                      #11
                      Re: Timer

                      "Jim Langston" <tazmaster@rock etmail.com> writes:[color=blue]
                      > "Keith Thompson" <kst-u@mib.org> wrote in message
                      > news:ln64kvucok .fsf@nuthaus.mi b.org...[color=green]
                      >> "siska" <stephenwvicker s@hotmail.com> writes:[color=darkred]
                      >>> Keith Thompson wrote:[/color]
                      >> And if you think that "wait_for_somet hing == true" is clearer than
                      >> "wait_for_somet hing", wouldn't "(wait_for_some thing == true) == true"
                      >> be better yet?
                      >>
                      >> See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.[/color]
                      >
                      > I treat that as preference. What is the difference between:
                      > if ( wait_for_someth ing )
                      > and
                      > if ( wait_for_someth ing == true )
                      >
                      > The biggest difference is that me, looking at this code, immediately know in
                      > the second form that wait_for_someth ing 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_someth ing )
                      > only if wait_for_someth ing was obviously a boolean value.[/color]

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

                      This:
                      if (wait_for_somet hing)
                      is *not* equivalent to this:
                      if (wait_for_somet hing == true)
                      It's equivalent to this:
                      if (wait_for_somet hing != 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_someth ing 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.

                      --
                      Keith Thompson (The_Other_Keit h) 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.

                      Comment

                      • Jim Langston

                        #12
                        Re: Timer

                        "Keith Thompson" <kst-u@mib.org> wrote in message
                        news:lnpsj3su0g .fsf@nuthaus.mi b.org...[color=blue]
                        > "Jim Langston" <tazmaster@rock etmail.com> writes:[color=green]
                        >> "Keith Thompson" <kst-u@mib.org> wrote in message
                        >> news:ln64kvucok .fsf@nuthaus.mi b.org...[color=darkred]
                        >>> "siska" <stephenwvicker s@hotmail.com> writes:
                        >>>> Keith Thompson wrote:
                        >>> And if you think that "wait_for_somet hing == true" is clearer than
                        >>> "wait_for_somet hing", wouldn't "(wait_for_some thing == true) == true"
                        >>> be better yet?
                        >>>
                        >>> See section 9 of the comp.lang.c FAQ, <http://www.c-faq.com/>.[/color]
                        >>
                        >> I treat that as preference. What is the difference between:
                        >> if ( wait_for_someth ing )
                        >> and
                        >> if ( wait_for_someth ing == true )
                        >>
                        >> The biggest difference is that me, looking at this code, immediately know
                        >> in
                        >> the second form that wait_for_someth ing 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_someth ing )
                        >> only if wait_for_someth ing was obviously a boolean value.[/color]
                        >
                        > The difference is that they don't mean the same thing.
                        >
                        > This:
                        > if (wait_for_somet hing)
                        > is *not* equivalent to this:
                        > if (wait_for_somet hing == true)
                        > It's equivalent to this:
                        > if (wait_for_somet hing != 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_someth ing 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.[/color]

                        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_someth ing != 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.


                        Comment

                        • Vladimir Oka

                          #13
                          Re: Timer

                          Keith Thompson opined:
                          [color=blue]
                          > "Vladimir Oka" <novine@btopenw orld.com> writes:[color=green]
                          >> Gaijinco wrote:[color=darkred]
                          >>> 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?[/color]
                          >>
                          >> 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.[/color]
                          >
                          > <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.[/color]

                          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>

                          Comment

                          • Charles Richmond

                            #14
                            Re: Timer

                            CBFalconer wrote:[color=blue]
                            >
                            > Gaijinco wrote:[color=green]
                            > >
                            > > 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?[/color]
                            >
                            > 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.
                            >[/color]
                            Want to see my fast draw??? Want to see it again???

                            --
                            +----------------------------------------------------------------+
                            | Charles and Francis Richmond richmond at plano dot net |
                            +----------------------------------------------------------------+

                            Comment

                            • siska

                              #15
                              Re: Timer

                              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_somth ing" 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

                              Comment

                              Working...