?
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
15
Medium Priority
?
332 Views
Last Modified: 2010-04-15
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
Comment
Question by:snuffucla
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 46

Accepted Solution

by:
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

by:gj62
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

by:Kocil
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Expert Comment

by:plushey
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

by:akshayxx
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

by:eranborovik
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

by:spauljoseph
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

by:akshayxx
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

by:oferh
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

by:ssnkumar
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

by:akshayxx
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

by:ssnkumar
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

by:akshayxx
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

by:jmcg
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question