Re: Pausing/Waiting in C
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Wed, 10 Jan 2007 20:40:26 -0800
Nelu <spamahead@xxxxxxxxx> writes:
Keith Thompson <kst-u@xxxxxxx> wrote:
Victor Silva <vfbsilva@xxxxxxxxx> writes:
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)||EOF==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_Keith) kst-u@xxxxxxx <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.
.
- Follow-Ups:
- Re: Pausing/Waiting in C
- From: Nelu
- Re: Pausing/Waiting in C
- References:
- Pausing/Waiting in C
- From: Kwebway Konongo
- Re: Pausing/Waiting in C
- From: Victor Silva
- Re: Pausing/Waiting in C
- From: Keith Thompson
- Re: Pausing/Waiting in C
- From: Nelu
- Pausing/Waiting in C
- Prev by Date: Re: panic error
- Next by Date: Re: Pausing/Waiting in C
- Previous by thread: Re: Pausing/Waiting in C
- Next by thread: Re: Pausing/Waiting in C
- Index(es):
Relevant Pages
|