On Fri, Apr 13, 2001 at 09:22:14PM +0200, Ivan Martinez wrote:
> Hello all:
> I'm trying to make DSLib v2 generate adecuated system calls depending on it being
>Linux, RTLinux, or RTAI. I'm looking in the Linux threads functions and I don't see
>any equivalent to pthread_make_periodic_np and pthread_suspend_np. Is there any?. If
>not, how can I perform such operations?.
> Thank you.
> Ivan Martinez
It can be duplicated using something similar to the attached
file.
dave...
/* Example of a periodic task using straight POSIX calls. */
/* Copyright 2001 David Schleef <[EMAIL PROTECTED]> */
/* Use, distribution, and modification are allowed under the LGPL. */
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#define PERIOD 100000 /* 100 ms */
void tv_add_usec(struct timeval *tv,unsigned int usec);
int tv_compare(struct timeval *a,struct timeval *b);
void tv_sub(struct timeval *a,struct timeval *b);
int main(int argc,char *argv[])
{
struct timeval now;
struct timeval next_tick;
gettimeofday(&next_tick,NULL);
tv_add_usec(&next_tick,PERIOD);
while(1){
printf("tick\n");
gettimeofday(&now,NULL);
if(tv_compare(&next_tick,&now)<0){
struct timeval timeout;
timeout = next_tick;
tv_sub(&timeout,&now);
select(0,NULL,NULL,NULL,&timeout);
}
tv_add_usec(&next_tick,PERIOD);
}
}
void tv_add_usec(struct timeval *tv,unsigned int usec)
{
tv->tv_usec += usec;
while(tv->tv_usec>=1000000){
tv->tv_usec -= 1000000;
tv->tv_sec ++;
}
}
int tv_compare(struct timeval *a,struct timeval *b)
{
if(a->tv_sec==b->tv_sec)return b->tv_usec-a->tv_usec;
return b->tv_sec-a->tv_sec;
}
void tv_sub(struct timeval *a,struct timeval *b)
{
a->tv_sec -= b->tv_sec;
if(a->tv_usec < b->tv_usec){
a->tv_usec += 1000000;
a->tv_sec --;
}
a->tv_usec -= b->tv_usec;
}