473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How To Implement Timer in C

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.

Feb 28 '07 #1
19 40754
UG wrote:
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.

--
Ian Collins.
Feb 28 '07 #2
UG wrote:
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.
<snip>
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.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Feb 28 '07 #3
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
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.

Feb 28 '07 #4
On 2月28日, 下午1时15分, "UG" <unmeshgh...@gmail.comwrote:
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;
}

Feb 28 '07 #5
On Feb 28, 9:33 am, "yunyua...@gmail.com" <yunyua...@gmail.comwrote:
On 2鏈28鏃, 涓嬪崍1鏃15鍒, "UG" <unmeshgh...@gmail.comwrote:
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;

}
<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>

Feb 28 '07 #6
In article <54*************@mid.individual.net>,
Ian Collins <ia******@hotmail.comwrote:
>UG wrote:
>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.
IOW:

Off topic. Not portable. Cant discuss it here. Blah, blah, blah.

Useful clc-related links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Feb 28 '07 #7
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
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


Feb 28 '07 #8
On Feb 28, 4:08 pm, "achiever" <friends.vik...@gmail.comwrote:
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
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
Which on my conforming C implementations is sadly missing. Should I
complain to the suppliers?

Feb 28 '07 #9
"achiever" <fr************@gmail.comwrote:
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
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
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
Feb 28 '07 #10

achiever wrote:
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
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
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.

Feb 28 '07 #11
achiever wrote:
On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
>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
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".

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Mar 1 '07 #12
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>achiever wrote:
>On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:
>>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

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).

Mar 1 '07 #13
On Thu, 1 Mar 2007 00:57:30 +0000 (UTC), ga*****@xmission.xmission.com
(Kenny McCormack) wrote:
>In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>>achiever wrote:
>>On Feb 27, 9:15 pm, "UG" <unmeshgh...@gmail.comwrote:

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

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).
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
--
jay

Mar 1 '07 #14
jaysome <ja*****@hotmail.comwrote:
On Thu, 1 Mar 2007 00:57:30 +0000 (UTC), ga*****@xmission.xmission.com
(Kenny McCormack) wrote:
CBFalconer <cb********@maineline.netwrote:
>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).

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
Mar 1 '07 #15
In article <5b********************************@4ax.com>,
jaysome <ja*****@hotmail.comwrote:
....
>>>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".

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.
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?
>Chuck was entirely correct, blah, blah, blah...
Mar 1 '07 #16
In article <45****************@news.xs4all.nl>,
Richard Bos <rl*@hoekstra-uitgeverij.nlwrote:
....
>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.

Mar 1 '07 #17
UG

UG wrote:
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.

Mar 3 '07 #18
UG wrote:
>
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.

--
Ian Collins.
Mar 3 '07 #19
UG wrote:
UG wrote:
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.
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.

Mar 3 '07 #20

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

Similar topics

8
by: Djanvk | last post by:
Ok I'm working on a program where you have say x number of seconds to do something. I'm at a loss on how to go about running a timer function. Can anyone point me in the right direction or post a...
1
by: wukexin | last post by:
I write my own class Cfile, I want to know what about implement ctime().Who help me? My use function ctime, I sign it with $$$. my class Cfile: #------------------------ file.h...
3
by: rhaley | last post by:
ugh. Okay, I need to figure out the number of milliseconds between DateTime.Now and the end of the day so that I can make changes based on the date roll-over. I need to implement a Timer so that I...
2
by: linesh.gajera | last post by:
Hi Guys, I am creating a Windows service that call a routine at given interval. Once routine is complete, windows service should wait for 5 minutes and then call the routine again. I was using...
3
by: John | last post by:
Hi I am writing a windows service and don't want a form or any other UI element in it. How can I implement timer in this case to run a piece of code every hour? Thanks Regards
5
by: archana | last post by:
Hi all, I am using timer to do some functionality on user specified time. I am using system.timers.timer class and its timer to do this functionality. What i am doing is i set autoreset to...
6
by: paresh | last post by:
Hi all, I need to set timer in C/linux like alram, such that i will get a timeout signal after specific timeout and my process remain executing as is it. I can use signal(SIGALRM, xyz) and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.