Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Timer in C (like in Java)

Reply
Thread Tools

Timer in C (like in Java)

 
 
Alexander Mahone
Guest
Posts: n/a
 
      04-23-2008
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
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      04-23-2008
Alexander Mahone <(E-Mail Removed)> writes:
> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      04-23-2008
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>

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-23-2008
In article <funo2n$pu$(E-Mail Removed)>,
santosh <(E-Mail Removed)> 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.


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
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      04-23-2008
Walter Roberson wrote:

> In article <funo2n$pu$(E-Mail Removed)>,
> santosh <(E-Mail Removed)> 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.

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

 
Reply With Quote
 
Anonymous
Guest
Posts: n/a
 
      04-23-2008
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.
 
Reply With Quote
 
jon
Guest
Posts: n/a
 
      04-23-2008
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
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      04-23-2008
In article <(E-Mail Removed)>,
jon <(E-Mail Removed)> wrote:
>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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      04-23-2008
http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> In article <(E-Mail Removed)>,
> jon <(E-Mail Removed)> wrote:
>>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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      04-24-2008
On 23 Apr 2008 at 22:34, Keith Thompson wrote:
> (E-Mail Removed)-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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I know about the shutdown timer, is there a "switch on" timer... Mal Carter Computer Support 3 01-12-2008 01:58 AM
page timer, or redirect timer Simon ASP .Net 1 11-04-2005 07:14 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com ASP .Net 1 02-22-2005 09:41 PM
old timer on net but Mozilla newbie carlton hunter Firefox 1 01-31-2004 04:53 AM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM



Advertisments