Click to See Complete Forum and Search --> : timed loop in c


f4_yellowjacket
03-01-2003, 04:43 PM
hey everyone, I'm fairly new to c, but I love it (way more than c++). I'm doing a lab where I'd like to have a loop for 5 second. How do I set up a timed loop for mandrake and if it's any different how do i do it for redhat.

I think is want i have so far it compiles but doesn't run.

#define LOOP_TIME 5
time_t startTime;
startTime = time(NULL);
while ((time(NULL) - startTime) <= LOOP_TIME) {}

I don't have it in front of me but i think that's what I have so far.

Thanks very much
Mike

bastard23
03-01-2003, 05:24 PM
f4_yellowjacket,

Works for me. Are you getting an error? Or is just not pausing for 5 seconds?

time() should work on any UNIX. You are wasting CPU time w/ this. Use the 'time' command to see. Consider using sleep() (man 3 sleep) in your loop.

Have fun,
chris

f4_yellowjacket
03-01-2003, 10:04 PM
hi.. it doesn't exit the loop after 5 seconds for me. I'm trying to sample form the sound card for 5 seconds. It works here at home on my Redhat machine but not at school on a Mandrake machine.

I guess maybe that machines screwed.. I'll try it on a different machine at school.

Thanks
Mike

f4_yellowjacket
03-06-2003, 02:57 PM
Thanks for your help. It was a user error... big surprise.