How To Implement Timer in C

U

UG

I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.
 
I

Ian Collins

UG said:
I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.
Not in standard C, but it may be possible on your target platform, so
try asking on a group for that environment.
 
N

Nelu

UG said:
I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft.
No.


Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.

I assume it uses the default value on time out. Still, the answer
is no.

You may want to look into either threads or processes but none of
them is topical here.
 
U

user923005

I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.
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.
 
Y

yunyuaner

I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.

The above description is clear.
Many platforms provide timer, such as win32, in which you can use func
settimer
Anyway, there is a poor implementation of timer in linux
void sig_alrm(int signo)
{
return;
}

int main()
{
signal(SIGARLM, sig_alrm);
alarm(1);
scanf(...);
/* time up, your can capture the signal by checking errno */
return 0;
}
 
M

mark_bluemel

The above description is clear.
Many platforms provide timer, such as win32, in which you can use func
settimer
Anyway, there is a poor implementation of timer in linux
void sig_alrm(int signo)
{
return;

}

int main()
{
signal(SIGARLM, sig_alrm);
alarm(1);
scanf(...);
/* time up, your can capture the signal by checking errno */
return 0;

}

<OffTopic>

The usual method I've seen for this sort of thing is to use either
select() or poll(), neither of which are part of the C standard, but
are common on Unix-like platforms.

Much better than the alarm-based approach, IMHO.

</Offtopic>
 
A

achiever

I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.

yes their exists a time delaying facility under c
the
*delay()* function which delay the output on the the screen
by
the time of declared in side bracesthis function is defined in dos.h
header file
 
M

mark_bluemel

yes their exists a time delaying facility under c
the
*delay()* function which delay the output on the the screen
by
the time of declared in side bracesthis function is defined in dos.h
header file

Which on my conforming C implementations is sadly missing. Should I
complain to the suppliers?
 
R

Richard Bos

achiever said:
yes their exists a time delaying facility under c

No, there doesn't.
the *delay()* function which delay the output on the the screen

....does not exist in C.
the time of declared in side bracesthis function is defined in dos.h

....which also does not exist in C.

Your toy compiler may include it, but that doesn't mean it will work on
a real computer. Especially when it's called "dos.h".

Richard
 
S

santosh

achiever said:
yes their exists a time delaying facility under c
the *delay()* function which delay the output on the the screen by
the time of declared in side bracesthis function is defined in dos.h
header file

The said function and the header file are not a part of Standard C. It
may be offered by your compiler, but it's likely to be unavailable
elsewhere.
 
C

CBFalconer

achiever said:
yes their exists a time delaying facility under c the *delay()*
function which delay the output on the the screen by the time of
declared in side bracesthis function is defined in dos.h header
file

Please check your facts before giving flawed advice. There is no
"delay" function in the ISO standard C language, nor is there any
such include file as "dos.h".
 
K

Kenny McCormack

Please check your facts before giving flawed advice. There is no
"delay" function in the ISO standard C language, nor is there any
such include file as "dos.h".

An observation which is equal in value to that of noting that a go-kart
does not have a heater (among other naturally expected amenities in a
personal transportation vehicle).
 
J

jaysome

An observation which is equal in value to that of noting that a go-kart
does not have a heater (among other naturally expected amenities in a
personal transportation vehicle).

Your observation would make sense if there was a go-kart Standard that
did not mandate a heater. I know of no such standard, let alone one
that does not mandate a heater. FWIW, my go-kart has a heater :^)

Chuck was entirely correct in his response--there is no "delay"
function in the ISO standard C language, nor is there any such include
file as <dos.h>. That does not mean that such a function is not useful
in C (which is what I suspect struck a nerve in you, as it did in me).
On the contrary, "delay" functions are both common and useful in
C--just not in the Standard C library (note that does not imply "not
in Standard C").

That shouldn't prevent you from using "delay" functions. There's
nothing wrong with including a non-standard header like <dos.h>, if
you need to include it for a prototype for a "delay" function that is
not provided by the Standard C library. Sacrificing portability in
this regard is, inter alia, prima facie--we embedded developers do it
all the time :^)

Best regards
 
K

Kenny McCormack

An observation which is equal in value to that of noting that a go-kart
does not have a heater (among other naturally expected amenities in a
personal transportation vehicle).

Your observation would make sense if there was a go-kart Standard that
did not mandate a heater. I know of no such standard, let alone one
that does not mandate a heater.[/QUOTE]

What are you smoking? The C standard is precisely that. A
specification for a minimalist and useless (in and of itself) language,
just as a go-kart is a minimalist and useless (in and of itself) form of
personal transportation.
FWIW, my go-kart has a heater :^)

I doubt it. Again, what are you smoking?
 
R

Richard Bos

jaysome said:
Your observation would make sense if there was a go-kart Standard that
did not mandate a heater. I know of no such standard, let alone one
that does not mandate a heater. FWIW, my go-kart has a heater :^)

IME all go-karts have heaters. They're placed right behind the back of
your seat, and they're of a special kind called "engines". Don't reach
back now...

Richard
 
K

Kenny McCormack

Richard Bos said:
IME all go-karts have heaters. They're placed right behind the back of
your seat, and they're of a special kind called "engines". Don't reach
back now...

Have fun driving your go-kart across North Dakota on the interstate in
winter. You will probably be tempted to "reach back". Don't blame me
for the results, however.

That's about how well equipped "strictly conforming C" (alas, the only
thing that is "on topic" here according to the nutcases) is for dealing
with real world problems.
 
U

UG

UG said:
I just wanted to know whether any timer facility exists in C, as it is
not mentioned in K&R 2, or in the ISO Draft. By timer function i mean
that when we use standard input function like scanf() or getch() or
any other function, the interface stops to take input from user but
what if user doesn't give input for hours, the program will still be
waiting. Is there any way to circumvent the scanf() (or any other
input function for that matter) so that it takes some default input or
no input and just proceeds to the next line of the execution assuming
input from user as nothing and acts accordingly.

It is sad but most of you got me wrong, i am not asking for the
delay() function or function that would suspend execution of code but
i am asking how can we circumvent input fucntions like getchar(),
getch(), scanf() so that they accept input from user in some time
duration and not otherwise, it has nothing to do with delay() function
outside the scanf() as still the execution would stop at scanf() for
the unspecified time, how can we circumvent that.
 
I

Ian Collins

UG said:
It is sad but most of you got me wrong, i am not asking for the
delay() function or function that would suspend execution of code but
i am asking how can we circumvent input fucntions like getchar(),
getch(), scanf() so that they accept input from user in some time
duration and not otherwise, it has nothing to do with delay() function
outside the scanf() as still the execution would stop at scanf() for
the unspecified time, how can we circumvent that.
You may be able to do this on your target platform, but the method will
be platform specific, so you will be better off asking on a platform
specific group.
 
S

santosh

UG said:
It is sad but most of you got me wrong, i am not asking for the
delay() function or function that would suspend execution of code but
i am asking how can we circumvent input fucntions like getchar(),
getch(), scanf() so that they accept input from user in some time
duration and not otherwise, it has nothing to do with delay() function
outside the scanf() as still the execution would stop at scanf() for
the unspecified time, how can we circumvent that.

By writing your own input routines. Standard C is useless for what you
want to do. It may even be impossible in some systems, without writing
some privileged code.

Since this is very system specific, you'll have to follow-up in a
group for your system.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top