473,466 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Create 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 2243
Kwebway Konongo <pa**@pNaOuSlPlAeMe.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.programmer or comp.os.ms-windows.programmer.win32 -- but
see if you can find an answer in the newsgroup's FAQ first.

--
Keith Thompson (The_Other_Keith) 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_Keith) 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.googlegr oups.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.orgwrote:
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)||EOF==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**@pNaOuSlPlAeMe.comwrote in message
news:97*********************@bt.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*******@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrote:
>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)||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) 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.orgwrote:
Nelu <sp*******@gmail.comwrites:
>Keith Thompson <ks***@mib.orgwrote:
>>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)||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().
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

"Nelu" <sp*******@gmail.com>
<snip>
int abuseSTDIN()
No, do not abuse stdin. LS

Jan 11 '07 #11
On Wed, 10 Jan 2007 16:06:01 -0800, Keith Thompson <ks***@mib.org>
wrote:
>Kwebway Konongo <pa**@pNaOuSlPlAeMe.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.programmer or comp.os.ms-windows.programmer.win32 -- but
see if you can find an answer in the newsgroup's FAQ first.
s/most operating systems/most implementations/

The OP mentioned nothing about using an OS, and lack of use of an OS
is entirely conformant to the C Standard. Many or most implementations
targeted for embedded processors in which there is no OS running even
support good ways to do this.

Best regards
--
jay
Jan 11 '07 #12

"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
Keith Thompson <ks***@mib.orgwrote:
Nelu <sp*******@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrote:
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)||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().

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.
The value returned by time() on most(?) implementations is in seconds.
Half of your statement seems to be about time(), the other half about
clock().
>
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 #13
Barry <ba****@nullhighstream.netwrote:
>
"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
>Keith Thompson <ks***@mib.orgwrote:
Nelu <sp*******@gmail.comwrites:
<snip>
>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.

The value returned by time() on most(?) implementations is in seconds.
Half of your statement seems to be about time(), the other half about
clock().
No, I was talking about counting how many times the function gets called
in a number of seconds. It will likely be called a lot more times than
the number of seconds so you get under a second approximations for a
call or a number of calls and it will give you something similar to
CLOCKS_PER_SEC, you can name it CALLS_PER_SEC, although it's just a very
bad approximation.
--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)

Jan 11 '07 #14

David T. Ashley wrote:
"Kwebway Konongo" <pa**@pNaOuSlPlAeMe.comwrote in message
news:97*********************@bt.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).
Sounds like the OP is asking about something I'm trying to do...

Incidentally, why is "spin-wait" bad? In my case, I'm trying to do a
pseudo-realtime application on a dedicated server, with latencies until
the next event is due to occur.
Sadly, the boss doesn't realise that my realtime app development
experience is precisely zero!

Jan 11 '07 #15
"Trev" <t.*********@btinternet.comwrote in message
news:11**********************@77g2000hsv.googlegro ups.com...
>
David T. Ashley wrote:
>"Kwebway Konongo" <pa**@pNaOuSlPlAeMe.comwrote in message
news:97*********************@bt.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).

Sounds like the OP is asking about something I'm trying to do...

Incidentally, why is "spin-wait" bad? In my case, I'm trying to do a
pseudo-realtime application on a dedicated server, with latencies until
the next event is due to occur.
Sadly, the boss doesn't realise that my realtime app development
experience is precisely zero!
Spin-wait is bad on a server because it chews up (i.e. consumes towards no
productive purpose) CPU bandwidth that would best be returned to the
operating system.

For example, on Linux, there might be a daemon that needs to wait for 10
minutes. "sleep(600);" allows the OS to do other things for 10 minutes. A
spin-wait will consume a large fraction of CPU bandwidth--potentially as
much as 100%--to do nothing but repeatedly check the time. On a shared
system -- and any server is shared, at least between your process and the
operating system -- it is horribly inefficient.

Now, on an embedded system, spin-wait may be valid. In fact, the most
common software architecture for small systems is just to spin-wait until
the next time tick and then do the things you need to do. That is OK
because you're the only process on the system, and there is nobody else who
can make better use of the CPU.

If you have any further questions or observations, please write me directly
at dt*@e3ft.com and answer my SPAM filtering system's automatic reply. I
might know one or two things about small embedded systems.
Jan 11 '07 #16

"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
Barry <ba****@nullhighstream.netwrote:

"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
Keith Thompson <ks***@mib.orgwrote:
Nelu <sp*******@gmail.comwrites:
<snip>
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.
The value returned by time() on most(?) implementations is in seconds.
Half of your statement seems to be about time(), the other half about
clock().

No, I was talking about counting how many times the function gets called
in a number of seconds. It will likely be called a lot more times than
the number of seconds so you get under a second approximations for a
call or a number of calls and it will give you something similar to
CLOCKS_PER_SEC, you can name it CALLS_PER_SEC, although it's just a very
bad approximation.
I only read your post and gave you too much
credit. Your explanation is worse than what I thought
you were doing.
Jan 11 '07 #17
Barry <ba****@nullhighstream.netwrote:
>
"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
>Barry <ba****@nullhighstream.netwrote:
>
"Nelu" <sp*******@gmail.comwrote in message
news:50*************@mid.individual.net...
Keith Thompson <ks***@mib.orgwrote:
Nelu <sp*******@gmail.comwrites:
<snip>
>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.

The value returned by time() on most(?) implementations is in seconds.
Half of your statement seems to be about time(), the other half about
clock().

No, I was talking about counting how many times the function gets called
in a number of seconds. It will likely be called a lot more times than
the number of seconds so you get under a second approximations for a
call or a number of calls and it will give you something similar to
CLOCKS_PER_SEC, you can name it CALLS_PER_SEC, although it's just a very
bad approximation.

I only read your post and gave you too much
credit. Your explanation is worse than what I thought
you were doing.
What did you think I was doing?

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

Jan 11 '07 #18

"David T. Ashley" <dt*@e3ft.comwrote in message
news:Wb******************************@giganews.com ...
"Trev" <t.*********@btinternet.comwrote in message
news:11**********************@77g2000hsv.googlegro ups.com...

David T. Ashley wrote:
"Kwebway Konongo" <pa**@pNaOuSlPlAeMe.comwrote in message
news:97*********************@bt.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).
Sounds like the OP is asking about something I'm trying to do...

Incidentally, why is "spin-wait" bad? In my case, I'm trying to do a
pseudo-realtime application on a dedicated server, with latencies until
the next event is due to occur.
Sadly, the boss doesn't realise that my realtime app development
experience is precisely zero!

Spin-wait is bad on a server because it chews up (i.e. consumes towards no
productive purpose) CPU bandwidth that would best be returned to the
operating system.

For example, on Linux, there might be a daemon that needs to wait for 10
minutes. "sleep(600);" allows the OS to do other things for 10 minutes.
A
spin-wait will consume a large fraction of CPU bandwidth--potentially as
much as 100%--to do nothing but repeatedly check the time. On a shared
system -- and any server is shared, at least between your process and the
operating system -- it is horribly inefficient.

Now, on an embedded system, spin-wait may be valid. In fact, the most
common software architecture for small systems is just to spin-wait until
the next time tick and then do the things you need to do. That is OK
because you're the only process on the system, and there is nobody else
who
can make better use of the CPU.
Of course we have gotten so far off topic for clc it doesn't matter.
But, every embedded system I have worked on (and you have used all
of them, directly or indirectly :-)) also respond to hardware interrupts.
Jan 11 '07 #19
Barry wrote:
>
"user923005" <dc*****@connx.comwrote in message
news:11**********************@i56g2000hsf.googlegr oups.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:
[snip]
Most of your response has nothing to do with C. You should have
just referred the OP to an appropriate news group.
He gave the answer that's in the comp.lang.c FAQ on the matter. Try to
pay attention.


Brian

Jan 11 '07 #20
"Barry" <ba****@nullhighstream.netwrote in message
news:12*************@corp.supernews.com...
>
Of course we have gotten so far off topic for clc it doesn't matter.
But, every embedded system I have worked on (and you have used all
of them, directly or indirectly :-)) also respond to hardware interrupts.
That has been my experience, too. I work in the auto industry, and typical
interrupts are timekeeping (i.e. periodic interrupt) and vehicle network
receive.

I think in the abstract it would be possible to find a small embedded system
without interrupts ... just that I've never encountered one.

I thought about this briefly as I was typing my post, but decided it would
just add unnecessary complexity. The important point for the individual I
was responding to is that in most applications (except small embedded
systems), spin-locking the CPU to get a time delay is usually a bad thing to
do.
Jan 11 '07 #21
On Wed, 10 Jan 2007 21:20:40 -0600, David T. Ashley wrote
(in article <Uu******************************@giganews.com>) :
"Kwebway Konongo" <pa**@pNaOuSlPlAeMe.comwrote in message
news:97*********************@bt.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():
Maybe, maybe not. In some cases, nanosleep() is better. Yet another
example of why it's better to just redirect them to an appropriate
group.
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.
And you also find Sleep() [note the upper case S]

Ditto above...
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Jan 11 '07 #22
"David T. Ashley" <dt*@e3ft.comwrites:
[...]
If you have any further questions or observations, please write me directly
at dt*@e3ft.com and answer my SPAM filtering system's automatic reply. I
might know one or two things about small embedded systems.
Or take it to comp.arch.embedded.

--
Keith Thompson (The_Other_Keith) 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 #23
On Wed, 10 Jan 2007 19:58:25 -0600, in comp.lang.c , "Barry"
<ba****@nullhighstream.netwrote:
>
"user923005" <dc*****@connx.comwrote in message
news:11**********************@i56g2000hsf.googleg roups.com...
>Kwebway Konongo wrote:
that was the first K&R version! Is there a command to pause an app for
a period of time,
>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.
(snip rest of quote from FAQ)
>Most of your response has nothing to do with C.
You do know it was a quote from this groups FAQ don't you?
>Instead you posed a response irrelevant to C, and lacking of
many proper solutions.
If you think the FAQ wrong, suggest some corrections to Steve Summit,
who owns it.

"The lusers I know are so clueless, that if they were dipped in clue
musk and dropped in the middle of pack of horny clues, on clue prom
night during clue happy hour, they still couldn't get a clue."

--
Mark McIntyre
Jan 11 '07 #24
Barry wrote:
"user923005" <dc*****@connx.comwrote in message news:11**********************@i56g2000hsf.googlegr oups.com...
>From the C-FAQ:
19.37: How can I implement a delay, or time a user's response, with
....
[snip]
....
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.
The post came straight from *this group's FAQ*. I can't imagine a more
on-topic or relevant-to-C source of information for this group.
--
Clark S. Cox III
cl*******@gmail.com
Jan 11 '07 #25
Barry wrote:
"user923005" <dc*****@connx.comwrote in message
[snip]
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.
I assume that your response is a joke.
Otherwise, I guess that you are a troll.
Do you know what the C-FAQ is? If so, next time add a smiley for me:
;-)
I'm smiley imparied sometimes.

Jan 12 '07 #26

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

Similar topics

12
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...
7
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...
2
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...
7
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...
2
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 ...
0
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...
12
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...
3
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...
0
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.