linux - native task api 测试
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/mman.h>
#include <native/task.h>
#include <native/timer.h>
RT_TASK demo_task;
/* NOTE: error handling omitted. */
void demo(void *arg)
{
RTIME now, previous;
/*
* Arguments: &task (NULL=self),
* start time,
* period (here: 1 ms)
*/
rt_task_set_periodic(NULL, TM_NOW, 1000000);
previous = rt_timer_read();
while (1) {
rt_task_wait_period(NULL);
now = rt_timer_read();
/*
* NOTE: printf may have unexpected impact on the timing of
* your program. It is used here in the critical loop
* only for demonstration purposes.
*/
printf("Time since last turn: %ld.%06ld ms\n",
(long)(now - previous) / 1000000,
(long)(now - previous) % 1000000);
previous = now;
}
}
void catch_signal(int sig)
{
}
int main(int argc, char* argv[])
{
signal(SIGTERM, catch_signal);
signal(SIGINT, catch_signal);
/* Avoids memory swapping for this program */
mlockall(MCL_CURRENT|MCL_FUTURE);
/*
* Arguments: &task,
* name,
* stack size (0=default),
* priority,
* mode (FPU, start suspended, ...)
*/
rt_task_create(&demo_task, "trivial", 0, 99, 0);
/*
* Arguments: &task,
* task function,
* function argument
*/
rt_task_start(&demo_task, &demo, NULL);
pause();
rt_task_delete(&demo_task);
return 0;
}
2 测试结果:
Time since last turn: 0.997940 ms
Time since last turn: 1.001939 ms
Time since last turn: 0.996909 ms
Time since last turn: 1.000727 ms
Time since last turn: 1.003576 ms
Time since last turn: 1.000606 ms
Time since last turn: 0.998909 ms
Time since last turn: 0.998243 ms
Time since last turn: 1.001333 ms
Time since last turn: 0.997636 ms
Time since last turn: 1.003152 ms
Time since last turn: 0.996788 ms
Time since last turn: 1.000060 ms
Time since last turn: 0.998667 ms
Time since last turn: 1.005030 ms
Time since last turn: 0.995031 ms
Time since last turn: 1.002545 ms
Time since last turn: 1.002303 ms
Time since last turn: 0.995515 ms
Time since last turn: 1.002849 ms
Time since last turn: 0.997878 ms
Time since last turn: 1.002000 ms
Time since last turn: 0.998546 ms
Time since last turn: 0.999576 ms
Time since last turn: 1.002363 ms
Time since last turn: 0.998425 ms
Time since last turn: 0.998969 ms
Time since last turn: 1.000061 ms
Time since last turn: 1.002788 ms
Time since last turn: 0.996848 ms
Time since last turn: 1.002182 ms
Time since last turn: 0.999212 ms
Time since last turn: 1.001455 ms
Time since last turn: 0.997636 ms
Time since last turn: 1.004546 ms
Time since last turn: 0.995757 ms
Time since last turn: 1.000788 ms
Time since last turn: 0.998485 ms
Time since last turn: 1.001333 ms
Time since last turn: 0.998485 ms
Time since last turn: 1.001152 ms
Time since last turn: 0.999878 ms
Time since last turn: 0.999334 ms
Time since last turn: 1.002121 ms
Time since last turn: 0.999697 ms
Time since last turn: 1.000242 ms
Time since last turn: 0.997697 ms
Time since last turn: 1.000303 ms
Time since last turn: 0.999879 ms
Time since last turn: 1.000788 ms
Time since last turn: 1.000061 ms
Time since last turn: 0.999636 ms
Time since last turn: 1.001454 ms
Time since last turn: 0.997334 ms
Time since last turn: 1.004666 ms
Time since last turn: 0.996910 ms
Time since last turn: 1.002545 ms
Time since last turn: 0.996485 ms
Time since last turn: 1.001273 ms
Time since last turn: 0.998666 ms
Time since last turn: 1.000788 ms
Time since last turn: 0.999758 ms
Time since last turn: 1.000242 ms
Time since last turn: 0.999091 ms
Time since last turn: 1.002909 ms
Time since last turn: 1.000485 ms
Time since last turn: 0.996424 ms
Time since last turn: 1.001818 ms