473,770 Members | 5,569 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pausing/Waiting in C

Hi everyone,
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

TIA

Paul

--
----
Home: http://www.paullee.com
Woes: http://www.dr_paul_lee.btinternet.co.uk/zzq.shtml
Jan 10 '07 #1
25 2315
Kwebway Konongo <pa**@pNaOuSlPl AeMe.comwrites:
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible
There is no good portable way to do this.

(The clock() function returns an indication of the amount of CPU time
your program has consumed. You might be tempted to write a loop that
executes until the result of the clock() function reaches a certain
value. Resist this temptation. Though it uses only standard C
features, it has at least two major drawbacks: it measures CPU time,
not wall clock time, and this kind of busy loop causes your program to
waste CPU time, possibly affecting other programs on the system.)

However, most operating systems will provide a good way to do this.
Ask in a newsgroup that's specific to whatever OS you're using, such
as comp.unix.progr ammer or comp.os.ms-windows.program mer.win32 -- but
see if you can find an answer in the newsgroup's FAQ first.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 11 '07 #2
Kwebway Konongo wrote:
Hi everyone,
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

TIA

Paul
Maybe you can use something like sleep().
Jan 11 '07 #3
Kwebway Konongo wrote:
Hi everyone,
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible
>From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you're trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.

Jan 11 '07 #4
Victor Silva <vf******@gmail .comwrites:
Kwebway Konongo wrote:
>I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

Maybe you can use something like sleep().
Maybe he can, but there is no sleep() function in the C standard
library (and the system-specific sleep() functions I'm familiar with
don't meet his requirements).

If the phrase "something like sleep()" is intended to exclude sleep()
itself, then you're probably right, but it's still system-specific.
(Hint: a system's documentation for sleep() might have links to other
similar functions.)

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 11 '07 #5

"user923005 " <dc*****@connx. comwrote in message
news:11******** **************@ i56g2000hsf.goo glegroups.com.. .
Kwebway Konongo wrote:
Hi everyone,
I'm developing an application in C; basically a linked list, with a
series
of "events" to be popped off, separated by a command to pause reading
off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible
From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
sub-
second resolution?

A: Unfortunately, there is no portable way. V7 Unix, and derived
systems, provided a fairly useful ftime() function with
resolution up to a millisecond, but it has disappeared from
System V and POSIX. Other routines you might look for on your
system include clock(), delay(), gettimeofday(), msleep(),
nap(), napms(), nanosleep(), setitimer(), sleep(), times(), and
usleep(). (A function called wait(), however, is at least under
Unix *not* what you want.) The select() and poll() calls (if
available) can be pressed into service to implement simple
delays. On MS-DOS machines, it is possible to reprogram the
system timer and timer interrupts.

Of these, only clock() is part of the ANSI Standard. The
difference between two calls to clock() gives elapsed execution
time, and may even have subsecond resolution, if CLOCKS_PER_SEC
is greater than 1. However, clock() gives elapsed processor time
used by the current program, which on a multitasking system may
differ considerably from real time.

If you're trying to implement a delay and all you have available
is a time-reporting function, you can implement a CPU-intensive
busy-wait, but this is only an option on a single-user, single-
tasking machine as it is terribly antisocial to any other
processes. Under a multitasking operating system, be sure to
use a call which puts your process to sleep for the duration,
such as sleep() or select(), or pause() in conjunction with
alarm() or setitimer().

For really brief delays, it's tempting to use a do-nothing loop
like

long int i;
for(i = 0; i < 1000000; i++)
;

but resist this temptation if at all possible! For one thing,
your carefully-calculated delay loops will stop working properly
next month when a faster processor comes out. Perhaps worse, a
clever compiler may notice that the loop does nothing and
optimize it away completely.

References: H&S Sec. 18.1 pp. 398-9; PCS Sec. 12 pp. 197-8,215-
6; POSIX Sec. 4.5.2.
Most of your response has nothing to do with C. You should have
just referred the OP to an appropriate news group.

Instead you posed a response irrelevant to C, and lacking of
many proper solutions.
Jan 11 '07 #6
Keith Thompson <ks***@mib.orgw rote:
Victor Silva <vf******@gmail .comwrites:
>Kwebway Konongo wrote:
>>I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

Maybe you can use something like sleep().

Maybe he can, but there is no sleep() function in the C standard
library (and the system-specific sleep() functions I'm familiar with
don't meet his requirements).

If the phrase "something like sleep()" is intended to exclude sleep()
itself, then you're probably right, but it's still system-specific.
(Hint: a system's documentation for sleep() might have links to other
similar functions.)
Is it ok to use stdin like this:

int abuseSTDIN() {
char a[2];
if(EOF==(ungetc ('\n',stdin)||E OF==ungetc('a', stdin))) {
return EOF;
}
if(NULL==fgets( a,2,stdin)) {
return EOF;
}
return !EOF;
}

?

If yes, this can be run for a number of seconds, in a loop with calls
to mktime and difftime to get some kind of sub-second resolution (like
CLOCKS_PER_SEC) . That aproximation can be used to implement a kind of
a sleep function using milliseconds later (unless the function returns
EOF in which case the function may not work).
Also, by using the I/O system it may be more CPU friendly although
it will by no means replace a system-specific sleep function.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 11 '07 #7
"Kwebway Konongo" <pa**@pNaOuSlPl AeMe.comwrote in message
news:97******** *************@b t.com...
Hi everyone,
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible
OS-dependent question.

Any form of spin-wait is bad programming practice (but I suppose it would
work).

In Linux it is usleep():

http://www.hmug.org/man/3/usleep.php

In Windows, I'm not sure, but there are various references on the Microsoft
website to sleep(). Since Microsoft tries to support straightforward Unix
applications, there is a good chance you'll find sleep() or usleep() as a
Windows API system call.
Jan 11 '07 #8
Nelu <sp*******@gmai l.comwrites:
Keith Thompson <ks***@mib.orgw rote:
>Victor Silva <vf******@gmail .comwrites:
>>Kwebway Konongo wrote:
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

Maybe you can use something like sleep().

Maybe he can, but there is no sleep() function in the C standard
library (and the system-specific sleep() functions I'm familiar with
don't meet his requirements).

If the phrase "something like sleep()" is intended to exclude sleep()
itself, then you're probably right, but it's still system-specific.
(Hint: a system's documentation for sleep() might have links to other
similar functions.)

Is it ok to use stdin like this:

int abuseSTDIN() {
char a[2];
if(EOF==(ungetc ('\n',stdin)||E OF==ungetc('a', stdin))) {
return EOF;
}
if(NULL==fgets( a,2,stdin)) {
return EOF;
}
return !EOF;
}

?
Is it ok? I'd say definitely not (which isn't *necessarily* meant to
imply that it wouldn't work).

The standard guarantees only one character of pushback. I suppose you
could ungetc() a single '\n' character and read it back with fgets().

What is the result supposed to indicate? Since EOF is non-zero, !EOF
is just 0.
If yes, this can be run for a number of seconds, in a loop with calls
to mktime and difftime to get some kind of sub-second resolution (like
CLOCKS_PER_SEC) . That aproximation can be used to implement a kind of
a sleep function using milliseconds later (unless the function returns
EOF in which case the function may not work).
Also, by using the I/O system it may be more CPU friendly although
it will by no means replace a system-specific sleep function.
I think your idea is to execute this function a large number of times,
checking the value of time() before and after the loop, and using the
results for calibration, to estimate the time the function takes to
execute. I don't see how mktime() applies here. There's no guarantee
that the function will take the same time to execute each time you
call it.

Pushing back a character with ungetc() and then reading it with
fgets() is not likely to cause any physical I/O to take place, so this
method is likely to be as antisocially CPU-intensive as any other busy
loop.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 11 '07 #9
Keith Thompson <ks***@mib.orgw rote:
Nelu <sp*******@gmai l.comwrites:
>Keith Thompson <ks***@mib.orgw rote:
>>Victor Silva <vf******@gmail .comwrites:
Kwebway Konongo wrote:
I'm developing an application in C; basically a linked list, with a series
of "events" to be popped off, separated by a command to pause reading off
the next event in the list. It has been sometime since I last did C, and
that was the first K&R version! Is there a command to pause an app for
a period of time, as all the commands I am familiar with specify pauses
for integer numbers of seconds, and what I would like is fractions of a
second, preferably milliseconds if possible

Maybe you can use something like sleep().

Maybe he can, but there is no sleep() function in the C standard
library (and the system-specific sleep() functions I'm familiar with
don't meet his requirements).

If the phrase "something like sleep()" is intended to exclude sleep()
itself, then you're probably right, but it's still system-specific.
(Hint: a system's documentation for sleep() might have links to other
similar functions.)

Is it ok to use stdin like this:

int abuseSTDIN() {
char a[2];
if(EOF==(ungetc ('\n',stdin)||E OF==ungetc('a', stdin))) {
return EOF;
}
if(NULL==fgets( a,2,stdin)) {
return EOF;
}
return !EOF;
}

?

Is it ok? I'd say definitely not (which isn't *necessarily* meant to
imply that it wouldn't work).

The standard guarantees only one character of pushback. I suppose you
could ungetc() a single '\n' character and read it back with fgets().
I wasn't sure whether pushing '\n' back to stdin would make fgets
return.
>
What is the result supposed to indicate? Since EOF is non-zero, !EOF
is just 0.
Just EOF if it failed. I could've used 0, I thought it was more
suggestive this way.
>If yes, this can be run for a number of seconds, in a loop with calls
to mktime and difftime to get some kind of sub-second resolution (like
CLOCKS_PER_SEC ). That aproximation can be used to implement a kind of
a sleep function using milliseconds later (unless the function returns
EOF in which case the function may not work).
Also, by using the I/O system it may be more CPU friendly although
it will by no means replace a system-specific sleep function.

I think your idea is to execute this function a large number of times,
checking the value of time() before and after the loop, and using the
results for calibration, to estimate the time the function takes to
execute. I don't see how mktime() applies here. There's no guarantee
that the function will take the same time to execute each time you
call it.
I meant to say time(). I know there are no guarantees that's why I said
approximation.
Pushing back a character with ungetc() and then reading it with
fgets() is not likely to cause any physical I/O to take place, so this
method is likely to be as antisocially CPU-intensive as any other busy
loop.
Yes, you're right.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
4169
by: Simon John | last post by:
I'm writing a PyQt network client for XMMS, using the InetCtrl plugin, that on connection receives a track length. To save on bandwidth, I don't want to be continually querying the server for updates (e.g. has the current track finished yet?) so I figured the best thing to do is just update after the track length has expired. So, how would I make a Python program automatically call a function after a preset period of time, without the...
7
7615
by: Dr. Know | last post by:
I am working on an ASP page that writes to several databases, ranging from MDBs to x-base. One of the tasks involves using an existing highest value from the DB and incrementing it before inserting a new record. I am using Application.Lock and .Unlock together with an application variable to negotiate access to the DB routine to one session (user) at a time. This is to ensure that the ID numbers are cleanly incremented, and that no...
2
1803
by: Dave Harris | last post by:
I am having a bit of trouble with form manipulation. I am working on a program where the user opens one form and when all the processes are done, he/she clicks on "Continue" which opens the next form in the program's sequence where more data processing takes place. Here is a snippet of code from the form I am currently working on. I need the application to pause after the "objReader.Close()" statement until the user clicks a button on the...
7
2695
by: Charles Law | last post by:
My first thought was to call WorkerThread.Suspend but the help cautions against this (for good reason) because the caller has no control over where the thread actually stops, and it might have a lock pending, for example. I want to be able to stop a thread temporarily, and then optionally resume it or stop it for good.
2
5254
by: BLUE | last post by:
I would like to pause an application while the GUI display a Label saying "Logging in...". System.Timers System.Windows.Forms.Timer System.Threading.Timer System.Threading ==Thread.Sleep Which one? In the last case Sleep must be applied to what(Application, this or what)?
0
2172
by: Grayzag | last post by:
Hi there, As part of my Software course, i have to create a game. Since I originally started out with python, I was used to it being really easy to create a main loop to control the game with a simple while statement, pausing where input is needed from the user. Now that I have moved on to VB, I can seem to find a way to pause the while statements until the user does something (e.g. clicks a button). When i start my app, if just throws...
12
3330
by: greg | last post by:
Hi, Can anyone help me with the following issue: How can I pause the execution of a program until a given file is created (by another process) in a specified directory? Any ideas would be highly appreciated. Thanks!
3
3903
by: Lucress Carol | last post by:
Hi everyone, I'm having troubles with pausing and continuing MFC Thread.For test purposes I've created in my MFC Dialog application a progress Bar Control, a Start Button and a Stop Button.The idea is to start the progress Bar Control by clicking on the Start Button and I would like to pause (Stop Button) the process at anytime and continue it when I click on the Start Button.I had a look at the following homepage:...
0
1313
by: thesti | last post by:
hello, i have some jbuttons in my frame. and i have a recursive method, which will check a certain condition and if satisfied, will move one of the jbuttons location to somewhere else in the frame and then remove the jbutton from the frame, then the process is repeated. (since i use null layout for my frame, i can call the setBounds method) the problem is, the method will keep running without pausing, so i couldn't see which...
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10231
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.