473,387 Members | 1,529 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,387 software developers and data experts.

how to implement timer

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.

Regards,
Paresh

Aug 19 '06 #1
6 10213

"paresh" <pa************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.
This is a frequently asked question. C doesn't have any high resolution
timer functions. What you need is assembly code for your OS. The 'rdtsc'
instruction will work for IA-32 platforms.

You'll want some GCC code (similar) to this:

unsigned long long rdtsc(void)
{
unsigned long long cycles=0;
__asm__ __volatile__(
"rdtsc\n"
:"=A"(cycles)
);
return cycles;
}
Rod Pemberton
Aug 19 '06 #2
Rod Pemberton wrote:
"paresh" <pa************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>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.

This is a frequently asked question. C doesn't have any high resolution
timer functions. What you need is assembly code for your OS.
Or a system call of some kind. In any case, the questions should be
asked and answered in a group dedicated to the system of interest, not here.

paresh, please ask in one of the Linux groups, after checking the FAQ
and charter for the group you select, since then there will be a chance
of you actually getting some decent advice for this problem.

It is acceptable to ask if something is possible in standard C, but if
the answer is no you should ask in an appropriate system specific group.
The 'rdtsc'
instruction will work for IA-32 platforms.
<snip>

How will reading the time allow a timeout *signal* to be generated? The
OP specifically wanted a signal with his code continuing to run as it
is, not having to scatter checks through his code for the time.

There are other problems with rdtsc which I won't go in to since the
experts on it would be on another group not here.

Please redirect posts where a system specific answer is required to an
appropriate group, i.e. a linux group in this case (my Linux server is
PPC based making your answer even less useful!) where the people who
know will know all the intricacies of the errors in your answer will
hang out.
--
Flash Gordon
Still sigless on this computer.
Aug 19 '06 #3
"Rod Pemberton" <do*********@bitfoad.cmmwrites:
"paresh" <pa************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
>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.

This is a frequently asked question. C doesn't have any high resolution
timer functions.
True.
What you need is assembly code for your OS. The 'rdtsc'
instruction will work for IA-32 platforms.
What makes you think assembly code is the answer?
You'll want some GCC code (similar) to this:
[snip]

I seriously doubt it.

Ask in a comp.unix.programmer or in a Linix group.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 19 '06 #4
paresh (in 11**********************@m73g2000cwd.googlegroups. com)
said:

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

Paresh...

If you're looking for small millisecond delays, I suggest you
investigate the amount of delay associated with kernel calls on your
system...

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto
Aug 19 '06 #5

"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
"Rod Pemberton" <do*********@bitfoad.cmmwrites:
"paresh" <pa************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.
This is a frequently asked question. C doesn't have any high resolution
timer functions.

True.
What you need is assembly code for your OS. The
'rdtsc'
instruction will work for IA-32 platforms.

What makes you think assembly code is the answer?
You've asked me that twice in the past. I answered twice. Try to remember.
You'll want some GCC code (similar) to this:
[snip]

I seriously doubt it.
And, one again you show your abundant ignorance of anything immediately
outside the boundaries of the C specification...
Rod Pemberton
Aug 20 '06 #6
"Rod Pemberton" <do*********@bitfoad.cmmwrites:
"Keith Thompson" <ks***@mib.orgwrote in message
news:ln************@nuthaus.mib.org...
>"Rod Pemberton" <do*********@bitfoad.cmmwrites:
"paresh" <pa************@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.
This is a frequently asked question. C doesn't have any high resolution
timer functions.

True.
What you need is assembly code for your OS. The 'rdtsc'
instruction will work for IA-32 platforms.

What makes you think assembly code is the answer?

You've asked me that twice in the past. I answered twice. Try to
remember.
Sorry, I've forgotten. A Google Groups search indicates that you've
discussed the rdtsc instruction here before; apparently I didn't find
any of those discussions very memorable.
You'll want some GCC code (similar) to this:
[snip]

I seriously doubt it.

And, one again you show your abundant ignorance of anything
immediately outside the boundaries of the C specification...
If it's outside the boundaries of the C specification, it's off-topic
for this newsgroup. I admit that I don't know much about x86 assembly
language. That's why I don't post to comp.lang.asm.x86.

In any case, if I understand the code you posted correctly, it uses
the rdtsc instruction to load the value of the system's high
resolution time-stamp counter. The original poster was looking for a
high-resolution timer that would send his program a signal after a
specific timeout. Your code does nothing like that. You've provided
a chunk of code that (a) is entirely off-topic in this newsgroup, (b)
is extremely system-specific (and compiler-specific), and (c) doesn't
even address what the OP was asking about.

To the original poster: There is no way to do what you're trying to do
in portable C. There most likely is a system-specific way to do it,
*without* resorting to assembly language. I suggest that the
system-specific "setitimer" function might or might not be a good
starting point. (I make no specific claims about that; I've never
used it myself, and it's off-topic here.) Consult your system's
documentation. If you have any further questions, please post to
comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 20 '06 #7

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

Similar topics

0
by: Aaron Leung | last post by:
Hi everyone, I want to take a shot at implementing a simple framework for functional reactive programming in Python. (It will be loosely based on a paper I read about a system called FrTime for...
2
by: fnord | last post by:
I've been teaching myself perl today by writing a simple word game. It's going quite well, except I've hit a wall on the timer. At the moment it's a simple CLI interface, and what I need to do is...
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...
7
by: Crirus | last post by:
I have a server and I need to implement a automate process that occur at a certain interval, based on current time. I need to "tick" at every 10 minutes or (another version) when time is 30 minutes...
5
by: Flack | last post by:
Hey guys, Here is what I am trying to achieve: I have a grid, and every once in a while the grid will receive a message to add a new row and highlight it (change the backcolor) for five...
4
by: rockkyy | last post by:
hi, i am storing a random token and userid,clientdata etc on a STL map container with the unique token as the KEY and the structure containg userid,clientdata etc as the VALUE. Now i want to...
2
by: Thomas Ploch | last post by:
Hello folks, I am having troubles with implementing a timed queue. I am using the 'Queue' module to manage several queues. But I want a timed access, i.e. only 2 fetches per second max. I am...
19
by: UG | last post by:
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...
10
by: Michele Simionato | last post by:
I have always been curious about how people implement mainloops (or, in Twisted terminology, reactors). So I sit down and I wrote the following simple implementation: import itertools class...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.