473,651 Members | 2,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 40805
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...@gm ail.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...@gm ail.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...@gmai l.com" <yunyua...@gmai l.comwrote:
On 2鏈28鏃, 涓嬪崍1鏃15鍒 , "UG" <unmeshgh...@gm ail.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.individua l.net>,
Ian Collins <ia******@hotma il.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...@gm ail.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...@gm ail.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...@gm ail.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

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

Similar topics

8
1745
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 snippet of code for me to look at? The program I'm trying to write is a 2nd grade math program to help my daughter with her addition and subtraction. Thanks
1
574
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 #--------------------------- #include <io.h> #include <ctime> #include <string>
3
1855
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 can process things on the Timer.Elapsed event that would happen right at the date change (unless there is a better suggestion to implement an event on the date change). DateTime isn't so friendly to this calculation. Please, any suggestions are...
2
13197
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 System.Timers.Timer but i had to remove it because of known bug(842739). Now i am using System.Threading.Timer. It executes routine fine but the delay time is sporadic, sometimes it executes routine after 5 minutes, sometimes 10 minuete, 13...
3
2219
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
1794
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 false as i want to start processing only on user specified time. I am setting interval as difference between user sepcified time and current time. And when that elapsed event occured i am again setting
6
10239
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 alarm(some value in sec), there is a constraint in this as i can pass timeout only in seconds and i need in milli sec. Any idea how todo this.
0
8357
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抣l explore What is ONU, What Is Router, ONU & Router抯 main usage, and What is the difference between ONU and Router. Let抯 take a closer look ! Part I. Meaning of...
0
8700
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
8465
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
7298
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梡lanning, coding, testing, and deployment梬ithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4144
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...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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 we have to send another system
2
1588
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.