|
Home > Archive > Unix Programming > May 2005 > Highres delay help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Highres delay help
|
|
| Carl W 2005-05-27, 7:57 am |
| Dear all.
I need a way to implement a high resolution delay with at least a
resolution of 1 ms. Preferabely less. nanosleep cant help me, as the
tickrate is 100 Hz, which gives me a granularity of 10 ms, which is
way too rough for me.
I have seen a lot of hints on the web using some kind of highres timer
timer_create(CLOCK_HIGHRES...)
On my workstation (running solaris 8) i get a granularity of 3 ns
which is more than enough.
What I really want to do is to write a loop, which loops at a
predefined frequency.
loop
{
/*Do someting*/
delay(~1 ms)
}
I dont have very extensive C programming experience and this i why I
wonder how the highres timer works and how I could use it to implement
my program.
I'd be glad if someone could give me real code examples which shows me
in a straightforward way.
I hope there is anyone ont there who can help me. Time is a crucial
matter right now.
Best wishes,
Carl
| |
| A. Melinte 2005-05-27, 5:56 pm |
|
"Carl W" <calle79@inorbit.com> wrote in message
news:c6416781.0505270421.5ff3ef02@posting.google.com...
> Dear all.
>
> I need a way to implement a high resolution delay with at least a
> resolution of 1 ms.
You can use select() to have a microsec resolution in a portable way:
/* restart select if call interrupted */
static int
rselect( int n, fd_set *rds, fd_set *wds, fd_set *eds, struct timeval
*tout )
{
int rc;
do {
rc = select( n, rds, wds, eds, tout );
} while ( rc == -1 && errno == EINTR );
return rc;
}
int
microsleep( int sock, int microsec )
{
struct timeval tv;
fd_set rfs, wfs, efs;
int ret;
tv.tv_sec = 0;
tv.tv_usec = microsec;
if ( sock == CFGST_INVALID_SOCKET ) {
ret = rselect( 0, NULL, NULL, NULL, &tv );
} else {
FD_ZERO( &rfs ); FD_SET( sock, &rfs );
FD_ZERO( &wfs ); //FD_SET( sock, &wfs );
FD_ZERO( &efs ); //FD_SET( sock, &efs );
ret = rselect( sock+1, &rfs, &wfs, &efs, &tv );
}
return ret;
}
| |
| David Schwartz 2005-05-27, 5:56 pm |
|
"Carl W" <calle79@inorbit.com> wrote in message
news:c6416781.0505270421.5ff3ef02@posting.google.com...
> What I really want to do is to write a loop, which loops at a
> predefined frequency.
>
> loop
> {
> /*Do someting*/
> delay(~1 ms)
> }
You probably need a real-time operating system. It would help us to
answer you better if you explained what the "something" was. The problem is
that there is very little the system can usefully do if it must be certain
that it can get back to you in one millisecond. So you will effectively
monopolize the system such that it can't even talk to its disks.
DS
| |
| Carl W 2005-05-31, 7:48 am |
| The "something" i meant was to send a data packet.
With the "select" routine, proposed in a previous message in this
thread I still cannot get past the 100 Hz resolution limit. (Exactly
100 Hz)
This test program I am writing is for simulation only. I need to send
a small UDP data packet with different frequencies of up to 1000-1200
Hz...
> So you will effectively monopolize the system such that
> it can't even talk to its disks.
I dont need to run anything in paralell with the test and the packet
is coded as a textstring.
/Carl W
"David Schwartz" <davids@webmaster.com> wrote in message news:<d77s2u$hmn$1@nntp.webmaster.com>...
> "Carl W" <calle79@inorbit.com> wrote in message
> news:c6416781.0505270421.5ff3ef02@posting.google.com...
>
>
" You probably need a real-time operating system. It would help us to
> answer you better if you explained what the "something" was. The problem is
> that there is very little the system can usefully do if it must be certain
> that it can get back to you in one millisecond. So you will effectively
> monopolize the system such that it can't even talk to its disks.
>
> DS
|
|
|
|
|