Thread: Setting time length of program

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    17

    Setting time length of program

    I know there is a sleep() function that lets you "idle" the program for a specified length of time but I was wondering if there was a function that lets you run the program for a specified length of time. My program will loop for that length of time and then terminate.

    gettimeofday() is in my program and I was thinking of utilizing this but the user will essentially set the rate at which my program calls gettimeofday(), so if the user sets the program length to 500 secs and sets the interval to 400 secs, I would not end the program at the right time.

    What function is there to set the length of a program or what way is there to do this?

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    so if the user sets the program length to 500 secs and sets the interval to 400 secs, I would not end the program at the right time.
    I don't think it is really possible to stupid-proof your programs ^_^

    How were you planning on getting your program to get the time at a user specified interval? Ie, how were you planning on your program knowing when that interval had passed? Couldn't you just use that same method to cut the program off at a user specified time?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    The user will enter the number of milliseconds between each packet generated. My program will essentially sleep for that specified time then return to the loop.

    I could use gettimeofday() to determine the initial time the program started which will then be subtracted from the time a packet gets sent. I could then use a conditional statement to determine if the program exceeded the allotted time or maybe a conditional statement to check if the (time passed + time interval) >= duration.

    I could do this but was wondering if there a simple function like sleep()

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look up the time() function. It returns the number of seconds since Jan 1,1970 and increments every second...
    Code:
    unsigned long RunTime;
    unsigned long EndTime;
    
    // run for 60 seconds then quit
    EndTime = time(null) + 60;
    
    While (EndTime > RunTime)
     { RunTime = Time(null);
    
        // do a mess of stuf
    
      }

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    That's what I'm getting it but there will be a sleep() within the while loop so it won't exactly end at the endtime depending on the sleep argument.

    I'll just do it the way I thought of.



    Thanks for the suggestions guys.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    > so if the user sets the program length to 500 secs and sets the interval to 400 secs, I would not end the program at the right time.
    So rather than sleep(400), you would do for(i=0;i<400;i++)sleep(1);

    Along with some kind of state machine (isSleeping,isWorking), you would be able to check whether the overall time allowance had been reached.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SeriousTyro View Post
    so if the user sets the program length to 500 secs and sets the interval to 400 secs, I would not end the program at the right time.
    This is where you use counters... rather than setting a single time increment of 500 seconds, set a time increment of 1 second and count to 500 before firing the event.
    Code:
    int interval = 500;
    int ticks = 0;
    
    While( ticks < interval)
       { sleep(1000);  // assuming milliseconds
    
           // do "while waiting" stuff here 
    
          ++ ticks; }
    
    // do timed event stuff here
    If there is a concern about two intervals needing to be in a specific relationship, such as the program has to run long enough for at least one event to occur... then you check the values entered and adjust accordingly....
    Code:
    If (interval >  duration)
      duration = interval;

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SeriousTyro View Post
    I could use gettimeofday() to determine the initial time the program started which will then be subtracted from the time a packet gets sent.
    I think you'll find time() is better in this case. Time is an unsigned long that represents the number of seconds since Jan 1, 1970 which is updated each time you call it. Time of day functions repeat every 12 or 24 hours and don't lend themselves well to simple math with events that pass midnight or even traverse multiple days...

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    gettimeofday() returns the number of seconds since the beginning of time as well, it just includes fractional seconds in addition to the seconds returned by time().

    But if you want to sleep for 500 seconds, it won't matter if you know you've slept for 500 seconds or 500.0001 seconds - you'll exit the loop in either case.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by KCfromNC View Post
    gettimeofday() returns the number of seconds since the beginning of time as well, it just includes fractional seconds in addition to the seconds returned by time().

    But if you want to sleep for 500 seconds, it won't matter if you know you've slept for 500 seconds or 500.0001 seconds - you'll exit the loop in either case.
    LOL... I'm pretty sure time existed before 1970... but thanks for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Grouping Question
    By baikal_m in forum C Programming
    Replies: 7
    Last Post: 10-26-2010, 04:41 PM
  2. im a noob at c++, do you think so?
    By belRasho in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2010, 11:02 PM
  3. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  4. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  5. write a program that will set the time to 5:58
    By jupimako in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2003, 03:51 AM