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?