how to implement timer

P

paresh

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
 
R

Rod Pemberton

paresh said:
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
 
F

Flash Gordon

Rod said:
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.
 
K

Keith Thompson

Rod Pemberton said:
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.
 
M

Morris Dovey

paresh (in (e-mail address removed))
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...
 
R

Rod Pemberton

Keith Thompson said:
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
 
K

Keith Thompson

Rod Pemberton said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top