Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Is it possible to run a while loop for an amout of time (i.e. 1s, 0.5s, 0.1s)

Posted on 2003-03-11
Medium Priority
335 Views
I know this is vague, but does anybody know how to execute a timed while loop for a hardcoded amount of time (i.e. 1s, 0.5s, 0.1s).  I want to be able to jump out of the while loop after that set amount of time.  Needs to be in C.   Thanks.
0
Question by:snuffucla

LVL 46

Accepted Solution

Kent Olsen earned 100 total points
ID: 8115518
Query the system clock and exist when your interval has expired.

Kdo
0

LVL 6

Expert Comment

ID: 8115700
<grin>I tend to not exist when my interval is expired<grin>

Kdo's idea will get you close (assuming you want to exit, and not exist <grin again, couldn't help myself), but if it has to be EXACT, you have to work much harder - let us know...
0

LVL 5

Assisted Solution

Kocil earned 100 total points
ID: 8115717
// Borland C++
// use GetTickCount for VC++
#include <time.h>

main()
{
clock_t finish, duration;

duration = 10; /* seconds */
finish = clock() + duration * CLK_TCK;
while {finish < clock()) {
/* operation(); */
}
}
0

Expert Comment

ID: 8115966
If you were only worried about seconds then the best way to do this is to use alarm()

You may find the following link useful:
http://www.gnu.org/manual/glibc-2.0.6/html_chapter/libc_17.html
0

LVL 8

Expert Comment

ID: 8116355
if u use alarm .. u'll get into the alarm handler .. but u wont be able to tell in that function that ... get out of the while loop ..

yes u can set a global variable and keep checking it in the while loop .. :).. and in the alarm handler u can set the time-expired..

the benefit of alarm is that u wont have to calculate time everytime in the loop..
whenever alarm is raised .. in the signal handler you shud set the global vaiable as time-expired.. but u still will have to check in each iteration of the loop , that go ahead only if the time isnt expired..

there is one catch .. supppose u have a lot of processing to be done in the loop .. and alarm is raised, while u r inthe middle of the loop , then u cant just abort the rest of the operations remaining in the particular iteration.. but i guess thats good for u and ur loop .. aborting might cause unwanted consequences..

btw i have another way to go on this , using longjmp and setjmp in combination with alarm.  and in that u wont even need to check the global variable sort of thing .. in each iteration of the loop .. in that u can abort at any point..
0

Expert Comment

ID: 8117509
Please tell the environment you are running at.
For examples, in solaris and windows kernel, there is
a simple function that does exactly what you want.
0

LVL 2

Expert Comment

ID: 8117620
Why wasting a while loop code for delay use the delay function in process.h

#include <process.h>

void main()
{

printf("I am in fun!");
delay(500); /* 500 milliseconds */
printf("I am back after .5 seconds");
}
0

LVL 8

Expert Comment

ID: 8117654
spauljoseph :
>>>Why wasting a while loop code for delay use the delay function in process.h

if u read the question properly .. he is not asking for delay .. he is asking for timeout ..  his while loop keep doing the work always
and at given intervals of time .. the work done by the while loop and rest of the program is saved periodically..
how are you planning to acheive that with
delay();
0

LVL 1

Expert Comment

ID: 8119571
you can use signals or timers to that
but the implementation is OS specific so I can only further assist you if I know what is your OS.
0

LVL 8

Expert Comment

ID: 8133943
snuffucla,

A combination of signal handler for alarm, alarm signal and setjmp/longjp can be used to aceive this.

-Narendra
0

LVL 8

Expert Comment

ID: 8133980
ssnkumar::
here is the last line of of my fist comment..

>>>>>btw i have another way to go on this , using longjmp and setjmp in combination with alarm.  and in that u wont even need to check the global variable sort of thing .. in each iteration of the loop .. in that u can abort at any point..

incidently we wrote strikingly similar thing but in different time-space in same context(question), which EE doesnt appreciate much, as it takes more of their hard disk space.

0

LVL 8

Expert Comment

ID: 8134102
akshayxx,

I am very sorry. I tried to read all the comments before posting my solution. I think, I overlooked that line of yours and posted the same again!
Anyway, thanks for pointing out this. I will be more careful from now on...:-)

-Narendra
0

LVL 8

Expert Comment

ID: 8134190
i m also sorry that at the end of my last ( previous to this) comment i forgot to put three characters

:-)
0

LVL 20

Expert Comment

ID: 10121150
Nothing has happened on this question in more than 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
split points between Kdo and Kocil.

jmcg
EE Cleanup Volunteer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ouâ€¦
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see soâ€¦
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
###### Suggested Courses
Course of the Month11 days, 17 hours left to enroll