473,403 Members | 2,338 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,403 software developers and data experts.

Timer in C (like in Java)

Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Thanks a lot
Jun 27 '08 #1
14 3006
Alexander Mahone <sa******************@gmail.comwrites:
Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Not in standard C. Try a newsgroup that deals with your operating
system.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #2
Alexander Mahone wrote:
Hello, I need a facility in C, like a set of functions, to manage a
timer: I should be able to initialize it with a certain period of time
(like x msec), start it, and every x msec it should execute a certain
function. In a few words, I need something like the java.util.Timer
class, but for C (of course not a class).
Do you know of any implementation of something like that (if possible
real-time)?
Thanks a lot
Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.

<http://www.mkssoftware.com/docs/man3/ualarm.3.asp>
<http://www.mkssoftware.com/docs/man3/setitimer.3.asp>

Jun 27 '08 #3
In article <fu*********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
>Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.
Neither function was part of POSIX.1-1990.

ualarm() was added to POSIX as of Issue 4, Version 2, and moved from
X/OPEN UNIX extension to BASE of of issue 5 (which I think was 2002.)
As of issue 6 (2004), ualarm() is marked obsolescent.
http://www.opengroup.org/onlinepubs/...ns/ualarm.html

setitimer() was also added and moved at the same issues as for ualarm(),
but setitimer() is not marked obsolescent.
http://www.opengroup.org/onlinepubs/...getitimer.html

For information about what the -recommended- POSIX timer functions are
and their various trade-offs, a unix programming newsgroup should
be consulted.
--
"Walter exemplified class." -- Paul Tagliabue
Jun 27 '08 #4
Walter Roberson wrote:
In article <fu*********@registered.motzarella.org>,
santosh <sa*********@gmail.comwrote:
>>Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.

Neither function was part of POSIX.1-1990.

ualarm() was added to POSIX as of Issue 4, Version 2, and moved from
X/OPEN UNIX extension to BASE of of issue 5 (which I think was 2002.)
As of issue 6 (2004), ualarm() is marked obsolescent.
http://www.opengroup.org/onlinepubs/...ns/ualarm.html

setitimer() was also added and moved at the same issues as for
ualarm(), but setitimer() is not marked obsolescent.
http://www.opengroup.org/onlinepubs/...getitimer.html

For information about what the -recommended- POSIX timer functions are
and their various trade-offs, a unix programming newsgroup should
be consulted.
You're right. Apologies to the OP for mentioning an obsolescent
function.

Jun 27 '08 #5
On Wed, 23 Apr 2008 21:58:05 +0530, santosh wrote:
Standard C has no facilities for what you want to do. However POSIX
specifies ualarm() and setitimer() for interrupting a process after a
specified period. A signal handler can then call your function.
Only if you're really careful. Only some functions are safe to call in a
signal handler, or functions called from a signal handler. your system's
'man 7 signal()' should give you a list of safe functions to call from a
signal handler.

A unix newsgroup should give more information.
Jun 27 '08 #6
jon
there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):

sleep(int amount_of_milliseconds);

in windows programing there are ways to create a separate process to
control the timer:

here is a link to a msdn site:

http://msdn2.microsoft.com/en-us/library/ms632592.aspx

(if it doesn't work, try searching (on the same page) for timers and
windows)

// Set two timers.

SetTimer(hwnd, // handle to main window
IDT_TIMER1, // timer identifier
10000, // 10-second interval
(TIMERPROC) NULL); // no timer callback

SetTimer(hwnd, // handle to main window
IDT_TIMER2, // timer identifier
300000, // five-minute interval
(TIMERPROC) NULL); // no timer callback
Jun 27 '08 #7
In article <3b**********************************@2g2000hsn.go oglegroups.com>,
jon <jm******@hotmail.comwrote:
>there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):
>sleep(int amount_of_milliseconds);
sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.
--
"Allegories are in the realm of thoughts, what ruins are in
the realm of things." -- Walter Benjamin
Jun 27 '08 #8
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <3b**********************************@2g2000hsn.go oglegroups.com>,
jon <jm******@hotmail.comwrote:
>>there is a simple timer, but it works on the same process (which means
that the program will stop till the timer is done):
>>sleep(int amount_of_milliseconds);

sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.
Furthermore, it's defined differently on different systems. On POSIX
systems, for example, the argument is a number of seconds.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #9
On 23 Apr 2008 at 22:34, Keith Thompson wrote:
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.

Furthermore, it's defined differently on different systems. On POSIX
systems, for example, the argument is a number of seconds.
You're right, but there are also usleep and nanosleep, for shorter
intervals.

Jun 27 '08 #10
Antoninus Twink wrote:
On 23 Apr 2008 at 22:34, Keith Thompson wrote:
>ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
>>sleep() is not part of standard C. It is a common operating system
extension, but the original poster did not specify an OS.

Furthermore, it's defined differently on different systems. On POSIX
systems, for example, the argument is a number of seconds.

You're right, but there are also usleep and nanosleep, for shorter
intervals.
I think the OP wants something that will interrupt the program
periodically. I think some versions of nanosleep will do this, but as
Walter Roberson has rightly observed, the OP is better of consulting in
a platform specific group. His best bet might be setitimer/getitimer or
the timer_* group of functions.

Jun 27 '08 #11
On 24 Apr 2008 at 19:17, santosh wrote:
Antoninus Twink wrote:
>You're right, but there are also usleep and nanosleep, for shorter
intervals.

I think the OP wants something that will interrupt the program
periodically. I think some versions of nanosleep will do this
[snip]
His best bet might be setitimer/getitimer or the timer_* group of
functions.
Oops, sorry, I lost sight of what the OP had asked amidst all the
bluster.

Yes, setitimer should be just the ticket.

Jun 27 '08 #12
On 24 Apr, 22:08, Antoninus Twink <nos...@nospam.invalidwrote:
On 24 Apr 2008 at 19:17, santosh wrote:
Antoninus Twink wrote:
You're right, but there are also usleep and nanosleep, for shorter
intervals.
I think the OP wants something that will interrupt the program
periodically. I think some versions of nanosleep will do this
[snip]
His best bet might be setitimer/getitimer or the timer_* group of
functions.

Oops, sorry, I lost sight of what the OP had asked amidst all the
bluster.

Yes, setitimer should be just the ticket.
OK, I read various documentation on the Internet, but I think neither
ualarm() or setitimer() satisfy me completely. What I need to do, in
fact, is create a series of different timers, like timer objects, each
one referrable, with some kind of reference (a pointer or any other
way to refer to it in the future). Then, under certain conditions, I
need to reset one or more of them (that's why I need some way to refer
to every single timer specificly), to prevent them sending the
SIGALARM.
From what I've read, both ualarm() and setitimer() (the first of based
on the second) allows you to manage one single global timer, the one
of the caller process...
So, in my opinion (correct me if I'm wrong, my knownledge of C is
quite limited) the only 2 ways to do this would be:
1 - Create a series of threads, one for each timer I want to create,
and inside each thread call ualarm()...Or maybe I'd need to create
child processes, instead of threads?
2 - Use a series of timer_create(), without the need to create any per-
timer thread...
Is there a simpler and more lightweight way to do this?
Thanks a lot
Jun 27 '08 #13
Alexander Mahone wrote:
On 24 Apr, 22:08, Antoninus Twink <nos...@nospam.invalidwrote:
>On 24 Apr 2008 at 19:17, santosh wrote:
Antoninus Twink wrote:
You're right, but there are also usleep and nanosleep, for shorter
intervals.
I think the OP wants something that will interrupt the program
periodically. I think some versions of nanosleep will do this
[snip]
His best bet might be setitimer/getitimer or the timer_* group of
functions.

Oops, sorry, I lost sight of what the OP had asked amidst all the
bluster.

Yes, setitimer should be just the ticket.

OK, I read various documentation on the Internet, but I think neither
ualarm() or setitimer() satisfy me completely. What I need to do, in
fact, is create a series of different timers, like timer objects, each
one referrable, with some kind of reference (a pointer or any other
way to refer to it in the future). Then, under certain conditions, I
need to reset one or more of them (that's why I need some way to refer
to every single timer specificly), to prevent them sending the
SIGALARM.
From what I've read, both ualarm() and setitimer() (the first of based
on the second) allows you to manage one single global timer, the one
of the caller process...
So, in my opinion (correct me if I'm wrong, my knownledge of C is
quite limited) the only 2 ways to do this would be:
1 - Create a series of threads, one for each timer I want to create,
and inside each thread call ualarm()...Or maybe I'd need to create
child processes, instead of threads?
2 - Use a series of timer_create(), without the need to create any
per- timer thread...
Is there a simpler and more lightweight way to do this?
Thanks a lot
Please ask this <news:comp.unix.programmerwhere you will surely
receive much better responses than here. Standard C has no facilities
for creating timers.

Jun 27 '08 #14
On 28 Apr 2008 at 13:21, Alexander Mahone wrote:
OK, I read various documentation on the Internet, but I think neither
ualarm() or setitimer() satisfy me completely. What I need to do, in
fact, is create a series of different timers, like timer objects, each
one referrable, with some kind of reference (a pointer or any other
way to refer to it in the future). Then, under certain conditions, I
need to reset one or more of them (that's why I need some way to refer
to every single timer specificly), to prevent them sending the
SIGALARM.
From what I've read, both ualarm() and setitimer() (the first of based
on the second) allows you to manage one single global timer, the one
of the caller process...
Well, you can squeeze out three, producing the different signals
SIGALRM, SIGVTALRM and SIGPROF, but yes, there's a strictly limited
number of timers available to each process.

The usual way around this is to implement multiple timers by hand: set
up an ordered queue of timers, and as one expires initialize the next
one along.

Of course, that's a pain, and so people have already done it and put it
into libraries for you to use - a good one is libevent
(http://monkey.org/~provos/libevent/).

Jun 27 '08 #15

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

Similar topics

1
by: Geoff | last post by:
Hey.. I'm having problems with stopping this timer from outside the 'runTimer()' class.. i have no problems calling 'cancel()' from inside the run() method but I want to be able to stop the Timer...
3
by: Mr. B | last post by:
My current app has a timer that I kick ON in my Form1_Load as follows: ' Set Up the Timer Function Dim t As New System.Timers.Timer(12000) ' 1000 = 1 Second t.Enabled = True ' False to Turn OFF...
0
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it...
8
by: bearophileHUGS | last post by:
Hello, I have four things to ask or to suggest, sorry if they seem basic or already discussed. ------------------- I am still ignorant about Tkinter. This little program, after pressing the...
3
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it...
1
by: enzoJava | last post by:
Hii, I have this program and i need help on printing out the total time, can anyone help me on this? If there is a better way to do this let me know Thnx import javax.swing.*; import...
4
by: puntino | last post by:
Hi I have created my Alarm, at compile time I don't have any problem, but at run time I receive the follo wing messages: Exception in thread "main" java.lang.NullPointerException at...
8
by: sip.address | last post by:
Hello, I'm trying to find some existing (and simple if possible) timer queue implementation. Does anybody know a simple skeleton to use as example? I just need to send simple (relative)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
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...
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...

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.